The Real Way to Lock Mac with Keyboard Shortcut.

by on
2 minute read

There has been a number of times that I just simply want to lock my Mac at work or at home with a simple keyboard shortcut. To my surprise there really was no shortcut for example win key + L on windows or ctrl + alt + L on systems using Gnome.  When I did a few quick Google searches on how to do this many sites suggested using  control+Shift+Eject or Control+Shift+Power after enabling "require password immediately after  sleep or screen saver begins.". There are some problems with these suggestions. the first issue is this not only locks the computer it also put it to sleep which means the computer has to wake up first before the lock screen appears. The second issue is  many keyboard i use do not have an "Eject" or "Power" key on them.

Over some time I discovered a partial solution for locking the screen. The only slight issue was it needed to be done from a terminal window. This was fine for me since I spend most of my time at a terminal window anyways. This was accomplished by creating an alias with the command and perimeter to signal the computer to lock screen.

alias lock='/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend'

Finally  I have found a way to lock screen using a keyboard shortcut that didn't put the computer to sleep first. I accomplished this by using a built in tool on the Mac called "Automator" by creating a custom system service.

First launch Automator by using Spotlight search and select Service for the type of document.

launch

Set the service receives "no input" from the dropdown box.

Screenshot 2015-05-09 16.07.59

Search for "shell" in the search box and double click "Run Shell Script" and paste the following into the box.

/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend

Screenshot 2015-05-09 16.10.37

Save the service and call it "LockMac" or whatever is desired and close Automator.

Screenshot 2015-05-09 16.14.13

Now got to System Preferences > Keyboard > Shortcuts and select "Services". Scroll down until the service that was just created using Automator is seen it should be toward the bottom. Make sure the the box to the left is checked to enable it.  Then click on the word "none" to assign the desired shortcut keys.

Screenshot 2015-05-09 16.22.13

Screenshot 2015-05-09 16.26.52

At this point try out the keyboard shortcut, if everything works properly the lock screen will appear.

comments powered by Disqus