sleep script

I’m trying to make a ‘deepsleep’ script that puts my computer to hibernate, but puts the sleep settings back to sleep only after hibernating (so I can just close my laptop for sleep only, without having to change the configuration).

I constructed a script to achieve this, and for just a moment I considered myself a genius:


do shell script "sudo pmset -a hibernatemode 1"
tell application "System Events" to sleep
do shell script "sudo pmset -a hibernatemode 0"

I have to add that I fiddled with my configuration so that I don’t have to give my root password for executing these commands.

My moment of genius was only short. This works when running it from the scripteditor, but does not work as soon as I save the script and try to run it from Spotlight or the script menu (and that was my ultimate goal). Not working means that my script is executed, but it goes into sleep only, not hibernation as expected. It seems like the last line of the script is executed before the system goes to sleep. It also takes a very long time between the screen going blank and the actual sleeping (also for sleep only), which is not the case in the script editor.

Two questions:

  1. Why is there a difference between running a script from the script editor and SpotLight, and what are these differences?
  2. Any suggestions on how to make it work?

If you need more on configuration to answer this question, I will be happy to provide it.

greets,
Nelis

Model: MacBook Pro
AppleScript: 2.0.1
Browser: Safari 525.20.1
Operating System: Mac OS X (10.5)

Try adding a delay between sleeping and removing safe sleep.


do shell script "sudo pmset -a hibernatemode 1"
tell application "System Events" to sleep
delay 5
do shell script "sudo pmset -a hibernatemode 0"

This works!

I’m still puzzled why it works without the delay command when started from the script editor, but not when started from finder or script menu. One would expect that commands are executed in the same way, but the need to introduce a delay suggests there is a slight difference. Still, happy with the solution :slight_smile: