As @ccstone says, you can use the built-in pmset
utility to do this, So in Terminal:
sudo pmset -g
...will display all your settings, and you should see displaysleep
with a number equal to whatever your Display Sleep setting is (0
for "Never").
As you'll have seen, the problem is that this requires sudo
and, by default, sudo
requires you to enter your password -- so you can't do this in a KM "Execute Shell Script" action without some setup to allow your required commands to be `sudsed without a password.
In Terminal, type sudo visudo /private/etc/sudoers.d/pmset
and authenticate, to create (or edit if it already exists) a sudoers file containing your pmset
utility exceptions.
Hit the I
key to enter "Insert mode" and type in:
username ALL=NOPASSWD: /usr/bin/pmset -a displaysleep 1, /usr/bin/pmset -a displaysleep 0, !/usr/bin/pmset -a disksleep 1 *, !/usr/bin/pmset -a disksleep 0 *
...replacing username
with your account's short username. Hit the ESC
key and then type :wq
then hit the Return
key to save and exit.
You'll have to check that sudo
is set to use this extra file, so type in sudo visudo /private/etc/sudoers
and down-arrow all the way to the bottom. The last (or near-last) line will include includedir /private/etc/sudoers.d
-- if that line starts with a #
then position the cursor on the #
and hit the X
key to delete it (uncommenting the line to make it active), then type :wq
and hit Return
to save and exit (if there's no #
then type :q!
and Return
to exit without saving).
In a new Terminal window you should now be able to type
sudo /usr/bin/pmset -a displaysleep 0
...to set Display Sleep to "Never" and
sudo /usr/bin/pmset -a displaysleep 1
...to set it to 1 minute, without the need for your password.
You can now use the same commands in "Execute Shell Script" actions in KM -- you could make one macro for each setting, or a single macro that used sudo pmset -g | grep displaysleep
to get the current setting and then an "If Then Else" action to pick between your two "setting" macros.