I need an applescript which locks my Mac in the same way pressing shift+ctrl+eject would. I tried two different approaches, but both failed. The first one was to search the internet for a script, which failed. There are only solutions with screensaver or other applications, which I don’t want. I just want to use what I described. The second one was to tell applescript to press the three keys which didn’t work. If someone could tell me how to lock my Mac or how to tell applescript to press three keys, I would be really happy, because I am getting kinda’ frustrated.
-
shift+ctrl+eject puts the display to sleep. It's not the same thing as "locking" the screen. There's a separate setting for how long after sleeping until a password is required, which may be "never". Since High Sierra 10.13, the Mac also has a "lock screen", where it can be locked without putting the display or computer to sleep. The answers here are a mix of how to put the display to sleep, invoke the lock screen, or put the whole computer to sleep.– Elhem EnohpiCommented Feb 1 at 19:49
7 Answers
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
-
3Just to note: alfred and the cmd+shift+eject kills the network when it suspends the machine. This is super handy because it just locks the screen.– BeepDogCommented Jun 30, 2015 at 19:59
-
3When I run the script, it opens the keychain menu and gets stuck. When I close the menu, I get this error:
System Events got an error: Can’t get menu 1 of menu bar item 2 of menu bar 1 of application process "SystemUIServer". Invalid index.
– MacilCommented Apr 28, 2016 at 17:20 -
@AgentME are you sure you've done steps 1-4? I've also updated the code so try that after doing steps 1-4 :)– ᔕᖺᘎᕊCommented Apr 30, 2016 at 16:10
-
@ᔕᖺᘎᕊ I get the same error as above, "Invalid Index"– user166405Commented Oct 12, 2016 at 17:48
-
This answer is obsolete, since the Keychain Access app no longer has the required command. It's also not entirely clear what it used to do, i.e., whether it was the same thing as putting the display to sleep. If so, it didn't necessarily "lock" the screen, if it was set to not require a password. Commented Feb 1 at 19:51
Found this on another answer but an AppleScript that simply contains the following works for me.
tell application "Finder" to sleep
-
It depends on preferences weather sleep actually locks. Unfortunately for me this doesn't work because our organization forces the setting to lock 1 minute after sleeping– quuxmanCommented Mar 12, 2018 at 21:16
-
This puts the entire computer to sleep, not just the display. It's not the same thing as pressing shift+ctrl+eject, as was requested. Commented Feb 1 at 19:54
I have a simple one liner for this. It uses System Events to send the default key combo for the Lock Screen command and works in all versions of OSX from High Sierra (10.13) on. Even Mojave! Though it will prompt for additional Security permissions for the App, in the newer OSX versions
tell application "System Events" to keystroke "q" using {control down, command down}
-
1This question was asked Jun 21 '14 and your answer is not applicable to the versions of OS X running at that time. Please edit your answer to include relevant context of what versions of macOS this works in. Commented Jul 10, 2019 at 3:29
-
2
-
It is not a default keyboard shortcut in all version of OS X, and a matter of fact it does not work on any version of OS X and was only introduced in one of the version of macOS. Commented Jul 10, 2019 at 13:27
-
2Look I am just trying to help and provide an answer that worked for me. Don't like it? Delete it. I couldn't care less Commented Jul 11, 2019 at 18:32
-
3Works like a charm (at least it does for me on Mojave). Not sure why this other commenter is saying it doesn't work. It's a simple and clean solution. Thanks for sharing it.– GDP2Commented Jan 23, 2020 at 22:47
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.)
macOS 10.11, El Capitan, seems to have broken ᔕᖺᘎᕊ's answer using AppleScripts, but I found that if I installed Lock Screen, and made my Automator Service open its "Lock Screen Bundle", the rest of the process worked as intended (provided I called the Service "lock-screen", not "Lock Screen": apparently spaces in the filename break the keyboard shortcut for many macOS users).
See Lock screen shortcut on Mac OS X for an illustrated step-by-step guide.
For High Sierra (10.13),the accepted answer doesn't work as Keychain Access has no Check Show Status in Menu Bar
option. But, based on shamelessly looting the source of the Lockscreen App (as mentioned by @Alice Purcell), I've discovered this nifty GUI-scripting workaround:
activate application "SystemUIServer" tell application "System Events" tell process "SystemUIServer" to keystroke "q" using {command down, control down} end tell
-
1best answer for big sur. used this to make a lock button on my Stream Deck. Thanks! Commented Jan 5, 2022 at 19:29
-
-
The question asks for an Applescript that "locks my Mac in the same way pressing shift+ctrl+eject would." That is, puts the display to sleep. This script presses cmd+ctrl+Q, which does something else: invokes the lock screen. It also only works on macOS 10.13 or later. Commented Feb 1 at 20:14
Here's an answer that works on modern macOS (Ventura as of writing this) that is keyboard shortcut agnostic (meaning, you can change your Lock Screen
keyboard shortcut and this will still work).
Use the following AppleScript:
tell application "System Events" to tell process "Finder"
click menu item "Lock Screen" of menu 0 of menu bar 1
end tell