14

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.

1
  • 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. Commented Feb 1 at 19:49

7 Answers 7

14

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
5
  • 3
    Just 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.
    – BeepDog
    Commented Jun 30, 2015 at 19:59
  • 3
    When 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.
    – Macil
    Commented 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"
    – user166405
    Commented 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
9

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

 tell application "Finder" to sleep
2
  • 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
    – quuxman
    Commented 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
5

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}
5
  • 1
    This 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 works in all versions of OSX. including Mojave Commented Jul 10, 2019 at 5:01
  • 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
  • 2
    Look 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
  • 3
    Works 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.
    – GDP2
    Commented Jan 23, 2020 at 22:47
3

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.)

3

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.

3

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
3
  • 1
    best answer for big sur. used this to make a lock button on my Stream Deck. Thanks!
    – CristosLC
    Commented Jan 5, 2022 at 19:29
  • still works on Ventura Commented Dec 10, 2022 at 16:02
  • 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
2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .