macos 我如何编写 Applescript,它会给我一个每小时弹出警报

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3718520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:13:12  来源:igfitidea点击:

How do I write Applescript that will give me an hourly popup alert

macosapplescript

提问by Kramer

I am wondering how to have a popup alert on my computer (Mac OS X) every hour. I figured writing this in Applescript would be pretty simple but I have no Applescript experience. Thanks

我想知道如何每小时在我的计算机(Mac OS X)上弹出一个警报。我认为用 Applescript 写这个会很简单,但我没有 Applescript 经验。谢谢

回答by Jerry Stratton

The basic handler for doing things on a regular basis in AppleScript is the idle handler.

在 AppleScript 中,用于定期执行操作的基本处理程序是空闲处理程序。

on idle
 display dialog "Go back to work" buttons "Work Harder" default button "Work Harder"
 return 3600
end idle

This script will pop up a dialog box when you start the application and then every 3,600 seconds after pressing the button. Whatever number the idle handler returns will be the number of seconds before the next idle event is triggered.

该脚本将在您启动应用程序时弹出一个对话框,然后在按下按钮后每 3,600 秒弹出一个对话框。空闲处理程序返回的任何数字都将是触发下一个空闲事件之前的秒数。

If you want it to be on the half-hour and not every sixty minutes, you'd want the idle script to return a different number of seconds, perhaps 60, and then check to see if you're at the right part of the hour.

如果您希望它在半小时而不是每 60 分钟一次,您希望空闲脚本返回不同的秒数,可能是 60,然后检查您是否在正确的部分小时。

on idle
 if the minutes of the (current date) is 30 then
  display dialog "Go back to work" buttons "Work Harder" default button "Work Harder"
 end if
 return 60
end idle

This will only display the dialog at half past. (Like Unix, AppleScript's concept of the current date includes the current time.)

这只会在半点显示对话框。(与 Unix 一样,AppleScript 的当前日期概念也包括当前时间。)

In each case, you want to save in AppleScript Editor as “Application” and “Stay Open” in order to have it respond to idle events instead of just quitting after running. And you can add the application to your list of “Login Items” in the “Accounts” system preference to make it run automatically when you log in.

在每种情况下,您都希望在 AppleScript 编辑器中保存为“应用程序”和“保持打开状态”,以便让它响应空闲事件而不是在运行后退出。您可以将应用程序添加到“帐户”系统首选项中的“登录项目”列表中,使其在您登录时自动运行。

回答by Peter

After testing Jerry solution, I found a problem. If your AppleScript App is not "topped" at the time of the event, you may get an AppleScript error and the only think that will call you is the blinking of the icone in the doc. So I've made a small change: Open the AppleScript editor and type this:

在测试了 Jerry 的解决方案后,我发现了一个问题。如果您的 AppleScript 应用程序在事件发生时没有“置顶”,您可能会收到 AppleScript 错误,唯一会提示您的是文档中图标的闪烁。所以我做了一个小改动:打开 AppleScript 编辑器并输入:

on idle
  tell application "call_me"
    activate
  end tell
  display dialog "Back to work" buttons "Work!" default button "Work!"
  return 3600
end idle

Save the script while selecting "Aplication" in the bottom menu of the file selector and then checking "Don't quit" (in French "Ne pas quitter aprés l'exécution") so the App will stay active even after the event. You must give the name "call_me" to your app or, if you change the name, change also this name in the code (tell application "xxxx" activate) Each 3600 seconds, the App will be topped and then will display the alert box.

在文件选择器底部菜单中选择“应用程序”的同时保存脚本,然后选中“不要退出”(法语“Ne pas quitter aprés l'execution”),这样应用程序即使在事件发生后也能保持活动状态。您必须为您的应用程序命名“call_me”,或者,如果您更改名称,请在代码中更改此名称(告诉应用程序“xxxx”激活)每 3600 秒,应用程序将被置顶,然后将显示警告框.

Just one point: the first time you'll save the App, Applescript will show you a selector, and ask you "Where is call_me.app?" Just cancel this selector and save.

只是一点:第一次保存应用程序时,Applescript 会向您显示一个选择器,并询问您“call_me.app 在哪里?” 只需取消此选择器并保存即可。

Oh!! I've to go back to work!!

哦!!我要回去工作了!!