Perform tasks on sleep with AppleScript

Jan 28, '05 08:29:00AM

Contributed by: leenoble_uk

I generally leave my computer logged in, but at the login screen. I might stop working any time of the day or evening and may have many applications open. The problem was Mail. Because it checks periodically for email, it would on occasion receive something in the middle of the night, and the alert would be loud enough to disturb sleep in the next room. Rather than have to remember every time to quit Mail or reduce the system volume I wanted a script which would quit Mail at 11pm UNLESS I was still up working late.

The crux of this hint (finding the system idle time) was not my doing. I don't claim credit for it, but I forget where I found it. I think it was on an AppleScript bulletin board or mailing list. I'm sure there are multiple uses for the basis of this script, but I wanted it for the reason stated above. Copy and paste the script into Script Editor. Then save the script, and you'll then need to either set an iCal event or a cron task to run the script at one or two times a day. Here's my cron entry:

0 8,23 * * * /path/to/script.scpt
This runs the script at 8am, which will launch Mail, and again at 11pm, when the idle time is monitored and Mail is quit if I'm not using the computer. This is a better solution for me than the screensaver option I have seen. That solution only activates at the time I specify and not every time I let the computer go to sleep.

Comments (13)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050126064329159