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.

6 Answers 6


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


  1. You could buy Alfred
  2. You could use Cmd+Shift+Eject
  • 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
    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
    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 :) Apr 30, 2016 at 16:10
  • @ᔕᖺᘎᕊ I get the same error as above, "Invalid Index"
    – user166405
    Oct 12, 2016 at 17:48

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

 tell application "Finder" to sleep
  • 1
    I combined this answer with the accepted answer and it worked very well. Maybe the accepted answer could incorporate this. Oct 26, 2016 at 13:33
  • Contrary to the code in the accepted answer, the code of this answer does not require setting privileges for all apps where you want to use your shortcut and also does not require that the keychain status is visible in the menu bar. Apr 2, 2017 at 10:16
  • 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
    Mar 12, 2018 at 21:16

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}
  • 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. Jul 10, 2019 at 3:29
  • 1
    It works in all versions of OSX. including Mojave 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. Jul 10, 2019 at 13:27
  • 1
    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 Jul 11, 2019 at 18:32
  • 2
    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
    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
  • 1
    best answer for big sur. used this to make a lock button on my Stream Deck. Thanks!
    – CristosLC
    Jan 5 at 19:29

You must log in to answer this question.

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