Display Sleep Macro?

I want to be able to toggle the Display Sleep setting in System Preferences > Energy Saver. It’s a slider with values from 1 minute to Never.

Is there any way to toggle between 1 minute and Never using a Keyboard Maestro macro?

Using UI actions is the obvious solution, but there may be a more elegant one.

What creative ways can you think to accomplish this?


KM 9.2
macOS 10.12.6 Sierra, but will be transitioning to Mojave in near future.

Hey Dave,

I think you can use pmset from the command line to do this...

I haven't monkeyed with pmset for quite a while, so I won't make any guarantees – but it's way better than UI-Scripting if it works.

Worth testing.


Hi Chris,
Thank you for your note.

I’m not familiar with Hexnode. They seem to be a professional support company for the enterprise that provides remote, large scale management of devices through automated push service. Annual subscription.

Do you know if an individual can purchase a single script?

Just not experienced with such a business entity.


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.

1 Like