Apple - Using AppleScript to lock screen
Getting information from this answer and this answer:
- Press the Spotlight button at the top right of the screen (Cmd + Space)
- Search
Keychain Access
. Open this - Open Preferences (Cmd + ,(comma))
- Check
Show Status in Menu Bar
- Press the Spotlight button at the top right of the screen (Cmd + Space)
- Search
Automator
. Select this - Create new
Service
- Search
Run Applescript
next to theActions
andVariables
buttons. Double click this - Make sure the checkboxes are
Service receives no input in any application
- 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!)
To make a shortcut:
- Press the Apple button at the top left of the screen
- Click
System Preferences
- Click
Keyboard
- Click
Keyboard Shortcuts
tab - Click
Services
- Find the name of the service you just created
- Click the blank space on the right
- Type your shortcut in
Use your shortcut in any application!
OR...:
- You could buy Alfred
- 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.)