I've been toying around with AppleScript and Automator to get this thing to work, but I just can't seem wrap my head around it. If anyone could show me an example or give me some tips on how I can set an application to close at a specific time and then put the computer to sleep or shutdown, it would be greatly appreciated.

  • Would you like this to be done regularly or is this something that you want to initiate manually?
    – ephsmith
    Aug 11, 2012 at 14:40
  • Do you have the AppleScript written already?
    – ephsmith
    Aug 11, 2012 at 14:49
  • It's something I would like to be done regularly and not have to worry about initiating. I did have a script written but it was quite messy so I trashed it and ended up here. Thanks for your answer. I'll give this a go and hopefully it clears up the issues I've been having. Aug 12, 2012 at 8:41
  • if you want this done regularly, then scheduling the job with at isn't what you want. cron or launchd. If you want an easier interface to setting that up, maybe lingon 3 will help.
    – ephsmith
    Aug 12, 2012 at 12:33

4 Answers 4


I'm assuming that you'd like to initiate this procedure as opposed to having it run at a regularly scheduled time.

My approach would be to initiate this from the command line, but any commands can be run in a shell script component of an Automator script with some modification. The following approach combines a few components to get the job done:

  1. A tell command to quit the application
  2. A command-line call to put the computer to sleep
  3. A command-line call to schedule 1 and 2 for a specific time.

1. Telling the Application to quit

This can be as simple as:

osascript -e 'tell application "AppName" to quit'

2. Putting the computer to sleep

Have a look at pmset for more details, but issuing the following command will put your computer to sleep:

pmset sleepnow

3. Scheduling an operation to be run at a specific time

Have a look at the at command. This command gives you the ability to specify a time for a command to run. You'll need to enable this functionality as it is not by default. To enable the atrun daemon, run the following command [reference on SU]:

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist

Once you've enabled atrun, any command can be scheduled for a specific time by issuing

echo "<command>" | at HH:MM

which works because at takes it's input from stdin by default. A simpler approach is to run commands listed in a file. A file can be executed by using the -f flag, followed by the name of the script to be run like this:

at -f /path/to/file HH:MM

Putting it all together

What follows assumes that atrun is enabled. As a toy example, let's say that I want to quit Mail and put the computer to sleep at 11PM. I would create the a file containing the commands that I want run as follows:

osascript -e 'tell application "Mail" to quit'
pmset sleepnow

Save that file to quit-and-sleep and then run on the command line

at -f /path/to/quit-and-sleep 23:00


I didn't do any checking to make sure that Mail actually closed. The script also assumes that nothing else is going to get in the way of putting the computer to sleep. I've kept it simple here as a starting point for what you want.


You can schedule an event once with at:

at 23:59 <<< 'osascript -e "quit app \"Mail\""; pmset sleepnow'

Or to schedule it to repeat every day, edit the crontab with EDITOR=nano crontab -e and add an entry like this:

59 23 * * * osascript -e 'quit app "Mail"'; pmset sleepnow
  • at 23:59 <<< 'osascript -e "quit app \"Mail\""; pmset sleepnow' does not work for me. It says "job 1 at Sat Apr 30 12:47:00 2016" but does not run the command when the time comes
    – oluckyman
    Apr 30, 2016 at 9:47

Here's a strictly applescript version that accomplishes this given a hard-coded time value. You could, of course make this part interactive if you want. Also, this version assumes the time-string relates to today.

--Get time setting for action to occur
set t to "3:16:30 PM"
--build a time string to use as a target.
set cd to (current date) as text
set AppleScript's text item delimiters to " "
set targetTime to text items 1 thru -3 of cd
set targetTime to date ((targetTime as text) & " " & t)

repeat while (current date) < targetTime
    delay 30 -- check the time every 30 seconds
end repeat

tell application "Mail"
end tell

tell application "Finder"
    display dialog "Do you want to shut the computer down now?"
    if button returned of the result is "Ok" then
    end if
end tell

Here's code that will wait an amount of seconds before quitting the current application and going to sleep.

delay (your amount of seconds here)
tell application "System Events"
    key down {command}
    keystroke "q"
    key up {command}
    set volume "1"
    say "computer going to sleep"
    tell application "Finder"
    end tell
end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .