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
2 Answers
The basic handler for doing things on a regular basis in AppleScript is the idle handler.
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.
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.
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.)
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.
-
If I’d been more awake they’d have been more like Animal Farm. Sep 16, 2010 at 4:48
-
Wouldn't it be easier to just subtract the minutes of the current date from sixty, multiply that by 60 and return that in the second version? That way, you don't need to check every minute.– 11684May 16, 2015 at 21:03
-
@11684, yes, that would be fine. I wanted to give an example that involved checking every minute, however, in case someone has needs that aren’t as simple as just a regular pop-up. Note that the run-up to the second example has a typo—it should have said “on the half-hour” rather than “on the hour”; that’s why it checks the minute against 30 rather than zero. (Typo now fixed.) May 17, 2015 at 19:48
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:
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.
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.
Oh!! I've to go back to work!!