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