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.
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:
- A
tell
command to quit the application - A command-line call to put the computer to sleep
- 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
Caveats
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.
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
beep
delay 30 -- check the time every 30 seconds
end repeat
tell application "Mail"
quit
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
shutdown
end if
end tell
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 '16 at 9:47
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"
sleep
end tell
end tell
at
isn't what you want.cron
orlaunchd
. If you want an easier interface to setting that up, maybe lingon 3 will help. – ephsmith Aug 12 '12 at 12:33