I would like to create an Automator workflow (or some similar function) that will close down Safari after a specified period of time.

For instance, I simply want to run Pandora in Safari during the night, but have it close out after 30 minutes to 1 hour, as sort of a sleep timer. I am a very new Mac user so detailed instructions would be great. I have familiarized myself with creating some simple Automator workflows, but I have no experience using AppleScript.

  • Assuming you have regular sleeping hour (say 12am everyday), would it be better for you to schedule a sleep at 12:30am, to put the whole computer to sleep instead of just closing Safari? – revolver Apr 19 '12 at 4:35
  • 1
    To schedule, go to System Preference -> Energy Saver -> Schedule. – revolver Apr 19 '12 at 4:36
  • I'd thought of this, but for other reasons I typically only run my TimeMachine backup among other things during the night; would prefer not to put the machine to sleep – Brenton Taylor Apr 19 '12 at 16:01
up vote 5 down vote accepted

Here is the AppleScript code, which you can put in your workflow, by implementing an AppleScript item, and copy-paste the code in. The time is configurable, but you need an exact sleep time, which I can add if you could elaborate some more on the time. Here is the code via CulturedCode.

global quit_after, check_every

set quit_after to 2700
set check_every to 10
set minute to quit_after / 60

display dialog "Check is performed every " & check_every & " seconds. Things will be quit after " & minute & " minutesof system inactivity."

on reopen
    display dialog "Check is performed every " & check_every & " seconds. Things will be quit after " & minute & " minutes of system inactivity."
end reopen

on idle
    set idletime to do shell script "echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))"
    if (idletime as integer) > quit_after then
        tell application "System Events"
            if ((name of processes) contains "Safari") then
                tell application "Safari" to quit
            end if
        end tell
    end if
    return check_every
end idle
  • This looks great so far - if you could edit it to quit after, say, 45 minutes I'd appreciate it. Thanks! – Brenton Taylor Apr 20 '12 at 23:03
  • Sure, no problem. – ruddfawcett Apr 20 '12 at 23:04
  • How might I adapt this such that an Xserve system will only close the Safari instances associated with inactive user sessions, on a multi-user-session system? – user86102 Jul 28 '14 at 3:36

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.