Safe Sleep

I don’t know about you, but the new “Safe Sleep” feature in 10.4.3 and later is something that I could use a lot. And fortunately for those of us without the newest laptops, there is a way to enable and use it on older iBooks and PowerBooks. Thanks to the following two pages, I was able to come up with an Applescript that lets me use this new feature easily and without using the Terminal every time I want to change things.

http://www.macworld.com/weblogs/macosxhints/2006/10/sleepmode/index.php
http://andrewescobar.com/archive/2005/11/11/how-to-safe-sleep-your-mac/

I thought I’d post my script here so that people who don’t want to mess with the Terminal and want an easy way to use this feature will have one. I’d also like to get some feedback on the structure and syntax of the code. If anyone knows a more efficient or user-friendly way to organize it let me know. (Note: I know I could probably make a nice AS app in Xcode with GUI and everything, but I prefer it as a straight script.)


set MacOSX to do shell script "sw_vers -productVersion"

if MacOSX is greater than "10.4.3" then
	
	display dialog "Would you like to enter Safe Sleep?" buttons {"Enable Safe Sleep", "Cancel", "Safe Sleep"} default button 2
	
	if button returned of result is "Safe Sleep" then
		try
			do shell script " pmset force -a hibernatemode 1" --activates Safe Sleep all the time
		on error
			display alert "Safe Sleep Activate failed"
		end try
		
		tell application "Finder"
			sleep
		end tell
		
		delay 5
		
		try
			do shell script "pmset force -a hibernatemode 3" --activates Safe Sleep only on total power loss
			display alert "Welcome back from Safe Sleep!"
		on error
			display alert "Safe Sleep Deactivate failed"
		end try
	else if button returned of result is "Enable Safe Sleep" then
		try
			do shell script "sudo nvram nvramrc='" & quote & " /" & quote & " select-dev;" & quote & " msh" & quote & " encode-string " & quote & " has-safe-sleep" & quote & " property;unselect;';sudo nvram " & quote & "use-nvramrc?" & quote & "=true" with administrator privileges
			display dialog "Safe Sleep has been enabled." & return & return & "Please restart your computer for the change to take effect." buttons {"OK"} default button 1
		on error
			display alert "Failed to enable Safe Sleep"
		end try
	end if
else
	display alert "You are currently running Mac OS X " & MacOSX & "." & return & return & "You must be running Mac OS X 10.4.3 or higher to use the Safe Sleep feature."
end if

And how do you Disable it?

From AndrewEscobar.com:

I didn’t include the “hibernatemode 0” option in my script because “hibernatemode 3” does the same thing, but it does also write the RAM to disk so I could see someone wanting to disable it completely to save a bit of battery.

Hi quiteman,

your way to check the system version fails, if the current version is 10.4.10,
because it compares only two strings

a reliable version is

set MacOSX to system attribute "sysv" -- (10.4.0 is 4160)
if MacOSX > 4163 then 
.

Thanks Stefan! That definitely looks like a cleaner way to check system versions. I didn’t notice because I haven’t run the script since updating to 10.4.10. Again, thanks for your input!

I thought I’d try using this script as it sounded very cool. Unfortunately despite the fact that I’m running 10.4.10 it keeps displaying the message, “You must be running Mac OS X 10.4.3 or higher to use the Safe Sleep feature.

Any ideas?

Hi Cloudwalker_3,

The problem lies in my original implementation of the system version check that StefanK pointed out is faulty. Below is the entire script with StefanK’s system version check.


set MacOSX to system attribute "sysv" -- (10.4.0 is 4160) -- Contributed by StefanK

if MacOSX > 4163 then -- Contributed by StefanK
	
	display dialog "Would you like to enter Safe Sleep?" buttons {"Enable Safe Sleep", "Cancel", "Safe Sleep"} default button 2
	
	if button returned of result is "Safe Sleep" then
		try
			do shell script " pmset force -a hibernatemode 1" --activates Safe Sleep all the time
		on error
			display alert "Safe Sleep Activate failed"
		end try
		
		tell application "Finder"
			sleep
		end tell
		
		delay 5
		
		try
			do shell script "pmset force -a hibernatemode 3" --activates Safe Sleep only on total power loss
			display alert "Welcome back from Safe Sleep!"
		on error
			display alert "Safe Sleep Deactivate failed"
		end try
	else if button returned of result is "Enable Safe Sleep" then
		try
			do shell script "sudo nvram nvramrc='" & quote & " /" & quote & " select-dev;" & quote & " msh" & quote & " encode-string " & quote & " has-safe-sleep" & quote & " property;unselect;';sudo nvram " & quote & "use-nvramrc?" & quote & "=true" with administrator privileges
			display dialog "Safe Sleep has been enabled." & return & return & "Please restart your computer for the change to take effect." buttons {"OK"} default button 1
		on error
			display alert "Failed to enable Safe Sleep"
		end try
	end if
else
	display alert "You are currently running Mac OS X " & MacOSX & "." & return & return & "You must be running Mac OS X 10.4.3 or higher to use the Safe Sleep feature."
end if