Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Click here to return to the '10.6: Sleep computer via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Sleep computer via AppleScript
Authored by: tobyvoss on Apr 29, '10 02:02:51AM
My hint submission got mangled somehow in the publication process. So I'll try to rephrase:
  • The problem: tell application "Finder" to sleep or tell application "System Events" to sleep don't do anything after the display sleep or screensaver has kicked in AND the password-requirement time period (set in System Preferences -> Security -> General) has elapsed.
  • My solution: Using do shell script "pmset sleepnow" works as expected.
For completeness' sake, here's my whole script:
set sleep_minutes to 30
	set sleep_seconds to sleep_minutes * 60
	display dialog "Putting computer to sleep in " & sleep_minutes & " minutes,\nat exactly " & 
		(current date) + sleep_minutes * minutes & 
		".\n\nDo you want to cancel or restart the timer?" default answer sleep_minutes 
		buttons {"Cancel", "Restart timer"} 
		default button 2 
		cancel button 1 
		giving up after sleep_seconds
	copy the result as list to {newsleepminutes, button_pressed}
	if button_pressed is "Cancel" then
		return false
		if button_pressed is "Restart timer" then
			set sleep_minutes to newsleepminutes
			do shell script "pmset sleepnow"
			return true
		end if
	end if
end repeat
Save this as an application in ~/Library/Scripts/ and invoke it from the AppleScript menu in the menubar.

[ Reply to This | # ]