Toggle Low Power Mode

Just on a proof-of-concept basis, I wrote a shortcut that toggles Low Power Mode in System Setting on my 2023 Mac mini. There is not a shortcut action that does this directly, so I used the pmset shell utility. This works but requires that the user input a password, which is probably more work then simply making the change directly in System Settings. It should be noted that the password dialog contains an Add Helper instead of an OK button, and, as far as I can determine, this is normal verbiage.

BTW, it’s possible to avoid the password dialog by rewriting this shortcut as an AppleScript and by including the username and password in the script. Because of security concerns, this is not an option for me. GUI scripting might also be an option.

The following screenshot only shows a potion of the shortcut.

Low Power Mode.shortcut (22.7 KB)

Belatedly I did a Google search on this topic, and it returned many, quite elaborate shortcuts that toggle low power mode (e.g. here). I tested a number of these shortcuts and they all required that the user input their password at some point. Also, enabling low power mode on a laptop may be different than enabling low power mode on a desktop computer, but I don’t have any way to test that.

The operation of the following AppleScript is similar to that of the above shortcut, except that the AppleScript uses a regular dialog to prompt for the user’s password. I placed the script in a handler to prevent the user’s password from being saved with the script.

on main()
	set userName to "robert" --set to desired value
	
	set pmsetOutput to paragraphs of (do shell script "pmset -g") --get current status of low power mode
	repeat with aLine in pmsetOutput
		if aLine contains "lowpowermode" then
			set lowPowerStatus to text -1 of aLine
			exit repeat
		end if
	end repeat
	
	if lowPowerStatus is "0" then
		set userPassword to text returned of (display dialog "Enter your password to enable the \"Low Power Mode\" system setting:" default answer "")
		if userPassword is "" then error number -128
		set lowPowerStatus to "1"
	else if lowPowerStatus is "1" then
		set userPassword to text returned of (display dialog "Enter your password to disable the \"Low Power Mode\" system setting:" default answer "")
		if userPassword is "" then error number -128
		set lowPowerStatus to "0"
	else
		display dialog "The current status of the \"Low Power Mode\" system setting could not be determined" buttons {"OK"} cancel button 1 default button 1
	end if
	
	try
		do shell script "/usr/bin/pmset -a lowpowermode " & lowPowerStatus user name userName password userPassword with administrator privileges
	on error
		display dialog "The \"Low Power Mode\" system setting could not be changed. This normally occurs when the password is incorrect" buttons {"OK"} cancel button 1 default button 1
	end try
end main

main()
1 Like