The Blog of Maxim Porges

Archive for January, 2011

  • An AppleScript to Toggle Sleep Modes

    I have a 2008 unibody MacBook Pro, and the battery is starting to show its age. When I leave it in sleep mode, I maybe get a day and a half before the battery is completely drained. Putting my laptop into hibernation keeps the battery charge longer, but takes longer to start up as well. Depending on what I’m doing with my laptop (i.e. putting it to sleep at home for a few hours vs. traveling with it) I like to change the power mode accordingly so that I can go longer without having to plug it in.

    You can change the power-down mode from sleep to hibernate and vice versa using the pmset command line utility, but I kept forgetting the options to pass in, so I wrote a little AppleScript that does it for me. The script prompts you for your admin password, checks the current sleep mode, and gives you the option to toggle the other mode if you want to.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    
    set loggedInUser to system attribute "USER"
    set userCancelled to false
    try
    	set sudoPasswordResult to display dialog "Please enter your password: " default answer "" with title "Password" with icon caution with hidden answer
    on error number -128
    	set userCancelled to true
    end try
     
    if not userCancelled then
    	if text returned of sudoPasswordResult is not equal to "" then
    		set sudoPassword to text returned of sudoPasswordResult
    		set currentModeText to do shell script "pmset -g | grep hibernatemode" user name loggedInUser password sudoPassword with administrator privileges
    		set currentMode to item 16 of currentModeText
     
    		set selectedButton to 1
    		set currentSleepModeName to ""
    		if currentMode is equal to "1" then
    			#Hibernate mode - default selection toggles to Sleep
    			set selectedButton to 1
    			set currentSleepModeName to "Hibernate"
    		else if currentMode is equal to "3" then
    			#Sleep mode - default selection toggles to Hibernate
    			set currentSleepModeName to "Sleep"
    			set selectedButton to 2
    		end if
     
    		set sleepModeResults to display dialog "Current sleep mode is " & currentSleepModeName & ". Select new sleep mode." with title "Select new sleep mode" buttons {"Sleep", "Hibernate"} default button selectedButton
    		set newSleepMode to button returned of sleepModeResults
    		if newSleepMode is "Sleep" then
    			set sleepModeFlag to 3
    		else if newSleepMode is "Hibernate" then
    			set sleepModeFlag to 1
    		end if
     
    		do shell script "pmset -a hibernatemode " & sleepModeFlag user name loggedInUser password sudoPassword with administrator privileges
    	end if
    end if

    2011.01.23 / no responses / Category: Uncategorized