Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript to schedule switch off and after 1 minute switch on

Sometimes restarting is not enough: you need to power off your Mac, wait for a little, and power it on again.

I'm looking for a script which does just that. Is it possible?

It could be a script which act like the "schedule" program in system preferences: it would switch off the Mac immediately, the add 1 minute to the current time and switch it on again.


Do you think it's possible?

Thanks

mac mini, Mac OS X (10.6.4)

Posted on Apr 15, 2012 5:09 AM

Reply
Question marked as Best reply

Posted on Apr 15, 2012 6:01 AM

The following script seems to work properly under Mac OS X 10.7.3:


set nextTime to ((current date) + 60)


tell application "System Preferences"

activate

reveal anchor "Schedule" of pane id "com.apple.preference.energysaver"

delay 0.1

tell application "System Events"

tell sheet 1 of window 1 of process "System Preferences"

if value of checkbox 1 = 0 then click checkbox 1 -- Start up or wake

clickpop up button 2

clickmenu item 3 of menu 1 of pop up button 2 -- Every Day

set value of UI element 10 to nextTime

click button "OK"

end tell

shut down

end tell

end tell

6 replies
Question marked as Best reply

Apr 15, 2012 6:01 AM in response to personal_username

The following script seems to work properly under Mac OS X 10.7.3:


set nextTime to ((current date) + 60)


tell application "System Preferences"

activate

reveal anchor "Schedule" of pane id "com.apple.preference.energysaver"

delay 0.1

tell application "System Events"

tell sheet 1 of window 1 of process "System Preferences"

if value of checkbox 1 = 0 then click checkbox 1 -- Start up or wake

clickpop up button 2

clickmenu item 3 of menu 1 of pop up button 2 -- Every Day

set value of UI element 10 to nextTime

click button "OK"

end tell

shut down

end tell

end tell

Apr 15, 2012 6:26 AM in response to personal_username

I haven't tested this, so it may take some tweaking (I'm suspicious of the 'quoted form of' bits, because I don't know if pmset requires double quotes), but it should work. basically it tells the power management system (pmset) to schedule a one-time shutdown in 1 minute and a one-time poweron in 2 minutes.


set shutdownTime to do shell script "date -v+1M '+%D %T'"

set restartTime to do shell script "date -v+2M '+%D %T'"


do shell script "pmset schedule shutdown " & quoted form of shutdownTime

do shell script "pmset schedule poweron " & quoted form of restartTime


If you have troubles it's almost certainly a matter of quoting. it might be easier in that case just to rewrite it as a plain shell script.

Apr 29, 2012 9:11 PM in response to personal_username

See man date. You can use -v+#S to specify seconds (e.g., -v+20S for 20 seconds), but keep in mind that there are sometimes non-negligible times involved, and I don't know what will happen if you try to schedule a shutdown or restart for a time that's already passed. you could probably set shutdownTime to as little as 1 second in the future, but you'll need to account for the time it takes to finish shutdown in restartTime.

AppleScript to schedule switch off and after 1 minute switch on

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.