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

10.6: Sleep computer via AppleScript System 10.6
Snow Leopard only hintI use a minimal AppleScript to sleep my computer after nn minutes of playing music through iTunes. After upgrading to 10.6, my older solutions (tell application "Finder" to sleep or tell application "System Events" to sleep) were ineffectual, so I searched for another solution, and came up with this:
do shell script "pmset sleepnow"
This seems like the only way to enter sleep mode after the password-required activation of display sleep (or the screensaver). Note that I've only tried this from an administrator account, where it works fine.
    •    
  • Currently 2.70 / 5
  You rated: 4 / 5 (20 votes cast)
 
[17,150 views]  

10.6: Sleep computer via AppleScript | 4 comments | Create New Account
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: Andrew J Freyer on Apr 27, '10 07:58:08AM

tell application finder
to sleep
end tell

works just fine on my 10.6 machine.

---
Easier location-based computing: with Airport Location, app for 10.6. It's an early Beta, so please report bugs!

http://airportlocation.blogspot.com



[ Reply to This | # ]
10.6: Sleep computer via AppleScript
Authored by: karog on Apr 27, '10 08:28:12AM

Or simply:

tell application "Finder" to sleep

which works for me in 10.6.3 without root status



[ Reply to This | # ]
10.6: Sleep computer via AppleScript
Authored by: n6ac on Apr 27, '10 10:19:34AM

For what it's worth, here's a sleep script with timer -

global timer
beep
display dialog "You may select the delay before sleep ... " & " If you do nothing sleep starts in 10 seconds" buttons {"Cancel", "Sleep Now", "Select Delay"} cancel button 1 default button 2 with icon 2 with title "Sleep in 321 Seconds" giving up after 10
if button returned of result is "Sleep Now" then
say "Iam going to sleep rite now. Buy"
sleepnow()
stop
else if button returned of result is "Select Delay" then
SelectDelay()
else if gave up of result is true then
sleepnow()
end if
stop

on sleepnow()
say "pretend i am asleep" --remove this line if you REALLY want to sleep
--tell application "Finder" to sleep ---uncomment this to REALLY sleep
stop
end sleepnow

on SelectDelay()
display dialog "Enter the number of seconds before sleep:" default answer "5" with title "Sleep in 321 Seconds"
set timer to the text returned of the result as integer
repeat until timer is 0
display dialog "Time until sleep is: " & timer & " seconds" buttons {"Cancel"} default button 1 with title "Sleep in 321 Seconds" giving up after 1
set timer to timer - 1
end repeat
sleepnow()
--return
end SelectDelay



[ Reply to This | # ]
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
repeat
	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
	else
		if button_pressed is "Restart timer" then
			set sleep_minutes to newsleepminutes
		else
			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 | # ]