Using Applescript to Sleep Computer

  1. In OS 10.4.11, I used this simple script to sleep the computer:

    tell application "Finder" to sleep

    In OS 10.5.5, this script has a long delay before the computer sleeps. Is there a better way to script sleep?

  2. I tell it application "System Events". also run 10.5.5 and have no delay.

    I use this:

    --pmset changes and reads power management settings such as idle sleep timing wake on administrative access automatic restart on power loss, etc.

    set current_mode to do shell script "pmset -g | grep mode" --look for current hibernate mode

    if current_mode contains "3" then set the current_name to "sleep" if current_mode contains "1" then set the current_name to "safe sleep"

    display dialog "I'm in " & current_name & " mode. Set new mode!" buttons {"sleep", "safe sleep", "cancel"} default button 3 set new_name to button returned of result

    if new_name is "sleep" then set new_mode to "3" if new_name is "safe sleep" then set new_mode to "1"

    do shell script "pmset -a hibernatemode " & new_mode with administrator privileges

    display dialog "Go to " & new_name & " now?" buttons {"yes", "no"} default button 2
    if button returned of result is "yes" then tell application "System Events" to sleep

Power Manager - Mac automation and energy saving software for Mac OS X

Download a free demonstration of Power Manager today and start saving.