Return status of SleepDisabled in Applescript

I'm trying to do an Applescript to enable or not SleepDisabled (using pmset) depending on the state of SleepDisabled.

Something like :

set SuperSleep to do shell script "return status of SleepDisabled"
if SuperSleep is equal to "0" then
do shell script "sudo pmset -a disablesleep 1" user name "Myname" password "Mypassword" with administrator privileges
else
do shell script "sudo pmset -a disablesleep 0" user name "Myname" password "Mypassword" with administrator privileges

I tried looking into man psmet or using defaults read but no success.

The only thing I'm thinking of is using pmset -g to shows up this :

System-wide power settings:
 SleepDisabled      0
Currently in use:
 lidwake              1
 autopoweroff         1
 standbydelayhigh     86400
 autopoweroffdelay    28800
 standbydelaylow      10800
 standby              1
 proximitywake        0
 ttyskeepawake        1
 hibernatemode        3
 powernap             0
 gpuswitch            2
 hibernatefile        /var/vm/sleepimage
 highstandbythreshold 50
 displaysleep         2
 sleep                2 (sleep prevented by coreaudiod, coreaudiod)
 tcpkeepalive         1
 halfdim              1
 acwake               0
 disksleep            10

And then look up the status of SleepDisabled on the list, but I don't know how to return the "0" or "1" of SleepDisabled...


The following works for me:

set SuperSleep to (do shell script "pmset -g | awk '/SleepDisabled/{print $2}'")

if SuperSleep is equal to "0" then
    do shell script "pmset -a disablesleep 1" with administrator privileges
else
    do shell script "pmset -a disablesleep 0" with administrator privileges
end if

NOTE: Do not use sudo in a do shell script command, Just use with administrator privileges as it's all that's needed when you'd normally do sudo in Terminal or a bash script. Additionally I would not include the user name "Myname" and password "Mypassword" as it is not encrypted in the file. Just type it into the dialog box brought up by the administrator privileges option.

Also note that SleepDisabled was not present in the output of pmset -g when I first tried in Terminal, and was not present until I used the sudp pmset -a disablesleep 0 command from Terminal. If this is the case on your system you'd need to adjust the code to handle that situation. You could use an if (compound) control statement.


Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.