AppleScript to wake and sleep computer

How would I tell my computer to go asleep and wake up through applescript?

TIA
~Balthamos

Putting the system to sleep is simple:

tell application "System Events"
	sleep
end tell

But getting it to wake at a preset time is a lot harder. You can’t do it within your script since the system isn’t running and therefore doesn’t know to wake up.

The only way of doing it is to script the Energy Saver preferences or to use a third party utility like iBeeZz ( http://www.ibeezz.com/ )

Ya for waking up, I use this little french program
WakeUp is what its called and It works great. See, what I got going is the ability to wake the computers up and pull a file or two off of them and put it back to sleep.

I didn’t realize you wanted to wake on LAN activity. I assumed from your original post that you were looking for a time-based solution.

Since you want network-wake then, yes, WakeUp, or one of the other Wake-On-LAN utilities will do this.

WakeUp is terrific (been using it for a long time) but, and this is just a point of clarification, the developer is in Switzerland, not France (though some of the documentation is in French).

Jon

sorry about calling it a french program. The site was in french so i assumed it was french. anyway i have a problem. i entered this code

and tried to compile it and it said “Application is not running” so I went to go look at the other computer and it wasn’t sleeping. How can I fix this?
Thanks

You have to make sure the remote machine can accept Remote Apple Events and that you have permission to send events to that machine. Then, try this script:

property user_name : ""
property user_password : ""
property remote_ip : ""

--set the properties once and then they will be saved with the script until it is recompiled
set user_name to my dd("What is your user name?")
set user_password to my dd("What is your password?")
set remote_ip to my dd("What is the IP of the target machine?")

set remote_machine to "eppc://" & user_name & ":" & user_password & "@" & remote_ip
using terms from application "Finder"
	with timeout of 5 seconds
		try
			tell application "Finder" of machine remote_machine to sleep
		on error the_err
			beep
			activate
			display dialog the_err buttons {"OK"} default button 1 with icon 0 giving up after 5
		end try
	end timeout
end using terms from

on dd(the_prompt)
	set the_result to ""
	repeat while the_result = ""
		activate
		set the_result to text returned of (display dialog the_prompt default answer "" buttons {"Cancel", "OK"} default button 2 with icon 1)
	end repeat
	return the_result
end dd

Jon

ok i did what you said but now its sayin finder got an error:cant continue sleep. i have checked and the computer is on. any suggestions?

I tried again and it still won’t work. Any tips or advice?

When I tested Jon’s script on my setup (from my OS X machine to a local OS 9 machine), nothing happened on the OS 9 machine. The sleep command failed with no error, yet the following script worked as expected. Does this work? If so, at least you’ll know that the script is communicating the remote computer’s Finder.

property user_name : ""
property user_password : ""
property remote_ip : ""

-- set the properties once and then they will be saved with the script until it is recompiled 
set user_name to my dd("What is your user name?")
set user_password to my dd("What is your password?")
set remote_ip to my dd("What is the IP of the target machine?")

set remote_machine to "eppc://" & user_name & ":" & user_password & "@" & remote_ip
using terms from application "Finder"
	with timeout of 5 seconds
		try
			tell application "Finder" of machine remote_machine
				activate
				display dialog "test"
			end tell
		on error the_err
			beep
			activate
			display dialog the_err buttons {"OK"} default button 1 with icon 0 giving up after 5
		end try
	end timeout
end using terms from

on dd(the_prompt)
	set the_result to ""
	repeat while the_result = ""
		activate
		set the_result to text returned of (display dialog the_prompt default answer "" buttons {"Cancel", "OK"} default button 2 with icon 1)
	end repeat
	return the_result
end dd

– Rob

i got it to work, but unfortunately i had to resort to Extra Suites. Although it is a great program, I would have loved to use something less crude. Thanks for your help guys.

Care to share your code? It might help someone else with the same need.

– Rob

oh im sorry. :frowning: I thought I posted it but I guess I didn’t :slight_smile: anyway here it is

property theTicks : 10
set user_name to ("Insert user name here")
set user_password to ("insert password here")
set remote_ip to ("insert.your.ip.address")

set remote_machine to "eppc://" & user_name & ":" & user_password & "@" & remote_ip
using terms from application "Extra Suites"
	try
		tell application "Extra Suites" of machine remote_machine
			ES move mouse {30, 15}
			ES pause theTicks
			ES click mouse
			ES type key "down arrow"
			ES type key "down arrow"
			ES type key "down arrow"
			ES type key "down arrow"
			ES type key "down arrow"
			ES type key "down arrow"
			ES type key "down arrow"
			ES type key "down arrow"
			ES type key "down arrow"
			ES type key "enter"
			ES move mouse {26, 656}
			ES click mouse
		end tell
	end try
end using terms from

Oh and also the little move mouse part at the end gets rid of that annoying little popup box that comes up

Ok, I have just solved the problem without the use of 3rd party applications. All you need to do is make a script on the computer you want to turn off, and then just have your run it! Also, I think that some commands are off limits like shutting down and sleeping remotely because i tried a shell script (do shell script sleep 1) and that did not work. So here are the example scripts if you want them.

--put this script on the Mac you would like to sleep
tell application "Finder"
	sleep
end tell

--run this script on another Mac to control sleep of the Mac with the above script on it 
set user_name to ("Your user name")
set user_password to ("Your password")
set remote_ip to ("Your.Remote.IP.Here")

set remote_machine to "eppc://" & user_name & ":" & user_password & "@" & remote_ip
using terms from application "Finder"
	with timeout of 5 seconds
		try
			tell application "Finder" of machine remote_machine
				open file "Path:to:sleep.app"
			end tell
		end try
	end timeout
end using terms from
display dialog "The iMac is now asleep" buttons {"OK"} default button 1

~Balthamos

Thanks for sharing your code and discoveries. I’m sure that others will benefit. :slight_smile:

FYI, ‘do shell script sleep x’ is not the same as telling the Finder to sleep. The shell’s sleep is similar to AppleScript’s delay, which simply adds a pause to the script.

– Rob