Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Perform tasks on sleep with AppleScript System
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.
    •    
  • Currently 2.75 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[21,338 views]  

Perform tasks on sleep with AppleScript | 13 comments | Create New Account
Click here to return to the 'Perform tasks on sleep with AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Perform tasks on sleep with AppleScript
Authored by: diablo943 on Jan 28, '05 10:52:54AM
I tweaked your great script so it can be applied to any application by just changing one variable. I added: property targetApp : "any application" -- The application you want to control and then substituted: targetApp where ever the script called "Mail". I am using it to open and quit iTunes on my music server on a schedule triggered by iCal. Fun! Diablo943

[ Reply to This | # ]
Another solution
Authored by: jecwobble on Jan 28, '05 11:02:37AM

I had the same problem with Mail. I use WinSwitch in conjunction with the scripts below to effectively 'mute' volume whenever someone switches out (like to the login screen) and sets the volume to 2.5 when someone switches in. It doesn't work on login or logout, but my family typically stays logged in and switches to the login screen when not using the computer.

setvolume in each user's "Switch-In Items" folder:


#!/bin/sh

/bin/sleep 4
/usr/bin/osascript -e 'set volume 2.5'

The 'sleep' line is necessary for switching from one user to another. Without it, the switched-out user's mutevolume (below) will happen after the switched-in user's setvolume, effectively cancelling it out.

mutevolume in each user's "Switch-Out Items" folder:


#!/bin/sh

/usr/bin/osascript -e 'set volume 0'



[ Reply to This | # ]
Perform tasks on sleep with AppleScript
Authored by: dogboy on Jan 28, '05 12:14:31PM

Why not just sleep your machine instead of logging out? You can have the screensaver set to require a password upon waking. Way easier. And better for the environment.



[ Reply to This | # ]
Perform tasks on sleep with AppleScript
Authored by: leenoble_uk on Jan 28, '05 03:08:30PM

My reason for not allowing the screensaver to kick in is because I have lots of Applescripts running at various times doing things for me.
One of these is a radio recorder. I get AS to record a Windows Media Player (yeuch!!) stream using Wiretap so I can listen to it at my leisure and remote controlled (Saling Clicker) using QuickTime instead.
Windows Media's Applescript is non existent and Wiretap's is patchy for what I want it to do (file particular shows away in particular folders with particular names). Therefore I have to use some UIScripting. Unfortunately if the screensaver is active it captures the keystrokes instead rendering the scripts useless.

Answer your question :)

---
So, I said ... well, I can't actually remember exactly what I said. But it was one of the most enormously cruel and frighteningly witty put downs ever.



[ Reply to This | # ]
Perform tasks on sleep with AppleScript
Authored by: dogboy on Jan 28, '05 06:59:02PM

Yeah, I leave mine on to pick up Podcasts overnight. My new mail sound is a dog bark, and it's at the other end of the house so it's not too audible.
What about hitting the mute key on your keyboard before going to the login screen?



[ Reply to This | # ]
Perform tasks on sleep with AppleScript
Authored by: leenoble_uk on Jan 28, '05 08:09:50PM

Applescript is automatic, no remembrin' required.
I really didn't want to explain my whole life, but here's the next part.
Ok, sometimes I work late, hence the idle script.
Sometimes I might finish early, but I'm still sorta on call to answer the phone and emails. Now, I'm somewhere else in the house and I hear my Mac cry out that it's received an email. All I have to do is look at my mobile screen thanks to Saling Clicker and I can see who the email was from and sometimes even read it. If it's important I can go and check it out.
At the end of the day If I haven't bothered going back to the computer it'll quit Mail for me overnight. And if I'm working late it won't.

Now quit with the pick pick picking. It works for me. ;-)

---
So, I said ... well, I can't actually remember exactly what I said. But it was one of the most enormously cruel and frighteningly witty put downs ever.



[ Reply to This | # ]
Perform tasks on sleep with AppleScript
Authored by: Tom Robinson on Jan 28, '05 12:54:38PM

You could stop mail from fetching new messages instead of quitting:

set fetches automatically to false

This toggles the 'Check for new mail' preference.



[ Reply to This | # ]
Perform tasks on sleep with AppleScript
Authored by: fungus on Jan 28, '05 01:54:03PM
The subject of this hint is misleading. I thought you were talking about running a script when the machine is in sleep mode. I thought this was not possible, and curious what you did. After reading your script I see that it should be titled "Perform tasks on idle with AppleScript". It was a good hint anyways. Good job.

[ Reply to This | # ]
SleepWatcher
Authored by: escowles on Jan 28, '05 11:40:14PM

I'm not sure how to do things while the computer is asleep (though I think it can be done, it seems like Retrospect was able to wake the machine up to do scheduled backups...).

But the next best thing is SleepWatcher -- it runs a script when the machine goes to sleep. I used this for a while to make my account switch to the login screen when it went to sleep (to get around the bug that makes you have to type in your password twice when waking from sleep and the screensaver at the same time).



[ Reply to This | # ]
Perform tasks on sleep with AppleScript
Authored by: gneagle on Jan 28, '05 02:36:19PM

I'm curious how the original poster is calling an AppleScript from cron: in my testing, the way it's done in the hint doesn't work! Replacing

/path/to/script.scpt

with

osascript /path/to/script.scpt

does work, though, as I would expect.



[ Reply to This | # ]
Perform tasks on sleep with AppleScript
Authored by: leenoble_uk on Jan 28, '05 03:10:10PM

You're correct of course, my bad. I use osascript as you are supposed to, I just omitted it when submitting the hint.

---
So, I said ... well, I can't actually remember exactly what I said. But it was one of the most enormously cruel and frighteningly witty put downs ever.



[ Reply to This | # ]
Seems a little heavy...
Authored by: adrianm on Jan 29, '05 09:50:45AM

Just a thought... might it not be better to have Mail NOT make a noise when new mail arrives, but run an applescript instead?

This script can check time of day, idle time, etc and then IT can decide whether or not to make a noise... or even quit mail if it's late + idling.

Would remove the need to run ioreg and perl every 30 seconds.



[ Reply to This | # ]
re Seems a little heavy...
Authored by: surfingmarmot on Feb 02, '05 11:55:32PM

Heavy as a black hole but sometimes that's what 'hacking' is about: doing cool, intricate things just 'because you can' and not because that's the 'best' way to accomplish it. Larry Wall's Perl manifesto sums it up "there's more than one way to do it". However your point is well-taken and your suggestion simple, elegant, and flexible. Now that my interest is piqued, I am going to implement your suggestion. Man! OS X is the perfect blend of interface elegance and UNX power. Windows seems so dull and unimaginative in comparison--the tool of bankers and drones. ;-)



[ Reply to This | # ]