How to prevent macOS Catalina from sleeping when connected to an external monitor
Today I want to post a simple tip about macOS Catalina and external displays.
I bought a new external monitor and I’m using my MacBook Pro in clamshell mode with an external keyboard and external mouse.
The only problem is that when the MacBook Pro is not connected to the AC power I can’t use it in clamshell mode.
I used the MacBook keeping the display open to avoid the sleep for some days and then I started to look for a solution to this problem.
I tried some applications that should prevent macOS from sleeping but they did not work for me (I guess that it is a problem caused by macOS Catalina).
Finally, I found a solution and it is really simple, it is just a shell command that is able to prevent my Mac from sleeping:
sudo pmset disablesleep 1
If you want to enable the sleep mode you can use this command:
sudo pmset disablesleep 0
This second command works only if the lid of the MacBook is open, if it is closed and you launch this command the MacBook won’t sleep even if it is not connected to the AC power.
One More Thing
You can understand that it is too much uncomfortable to type these two commands each time that I use my MacBook with an external display and so I found an even better solution.
In this case, I used the power of Keyboard Maestro and I created two macros:
- The First macro is executed when I connect my Corsair keyboard to the MacBook Pro. If I connect this keyboard I want to disable the automatic sleep because I’m using the MacBook with the external display
- The second macro is similar to the first one, the only difference is that this is executed when I disconnect the keyboard. In this case, I enable the sleep mode.
Note that I need to execute the command using administrator privilege so I have to add:
echo “PASSWORD” |
in this shell command because I can’t type the password each time that the macro is executed.