Apple - Using AppleScript to lock screen

Getting information from this answer and this answer:

  1. Press the Spotlight button at the top right of the screen (Cmd + Space)
  2. Search Keychain Access. Open this
  3. Open Preferences (Cmd + ,(comma))
  4. Check Show Status in Menu Bar
  5. Press the Spotlight button at the top right of the screen (Cmd + Space)
  6. Search Automator. Select this
  7. Create new Service
  8. Search Run Applescript next to the Actions and Variables buttons. Double click this
  9. Make sure the checkboxes are Service receives no input in any application
  10. Paste this Applescript:

tell application "System Events" to tell process "SystemUIServer"
    tell (menu bar item 1 of menu bar 1 where description is "Keychain menu extra")
        click
        click menu item "Lock Screen" of menu 1
    end tell
end tell

Save this Automator as something like Lock screen (note: code in GIF is different!)

enter image description here


To make a shortcut:

  1. Press the Apple button at the top left of the screen
  2. Click System Preferences
  3. Click Keyboard
  4. Click Keyboard Shortcuts tab
  5. Click Services
  6. Find the name of the service you just created
  7. Click the blank space on the right
  8. Type your shortcut in

Use your shortcut in any application!

enter image description here


OR...:

  1. You could buy Alfred
  2. You could use Cmd+Shift+Eject

Found this on another answer but an AppleScript that simply contains the following works for me.

 tell application "Finder" to sleep

I use a macro to accept the shortcut Command-L. Having recently switched from Windows to Mac, am easy way to lock my screen was important. In Windows, one merely needs to press the Windows key and L, and, voila, locked.

Using Keyboard Maestro, I created a macro for the shortcut Command-L, being essentially the same (comparing the Command key to the Windows key). The shortcut triggers the function "Login Window", which locks the screen. Easy peasy, at least to me.

(In Keyboard Maestro, the "Login Window" function is under Actions > System Control.)