9

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.

12

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
  • 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 '15 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 '16 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 '16 at 16:10
  • @ᔕᖺᘎᕊ I get the same error as above, "Invalid Index" – user166405 Oct 12 '16 at 17:48
5

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. – David Cullen Oct 26 '16 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. – valyron Apr 2 '17 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 '18 at 21:16
2

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.

1

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

0

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

You must log in to answer this question.

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