6 Replies Latest reply: May 6, 2012 12:07 AM by personal_username
personal_username Level 1 (0 points)

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)
  • Pierre L. Level 5 (4,255 points)

    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

                click pop up button 2

                click menu 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


  • twtwtw Level 5 (4,925 points)

    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.

  • personal_username Level 1 (0 points)

    Thanks a lot, works very well!

    Just had to add a line with password and indicate to do that with admin privileges... THANKS!

  • personal_username Level 1 (0 points)

    Just wondering... Is it possibile to have less than 1 minute? And how?

    Thanks!

  • twtwtw Level 5 (4,925 points)

    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.

  • personal_username Level 1 (0 points)

    thank you, it just make sense! so 1 minute is safer by now... ;-)