1

Is there a way to build an AppleScript for locking the screen? I want to add a Quick Action (and the relative button on the Touch Bar as described here)

This solution i.e.,

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

does not work in Catalina. After allowing accessibility for Apple Script Editor from Security and privacy, I still get this error in automator:

Can’t get menu 1 of menu bar item 2 of menu bar 1 of application process "SystemUIServer". Invalid index.

  • 1
    Allow accessibility for Apple Script Editor from Security and privacy then you will get rid of your error. As mentioned in following solution, it is already natively in macOS. – Udhy Feb 6 at 10:36
  • Yes, I know there is a built-in button. However, I'd like to group all my custom buttons under the QuickAction button. – floatingpurr Feb 6 at 10:40
  • 1
    did you allow and see if you were able to avoid this error you have mentioned. – Udhy Feb 6 at 10:48
  • I've just tried but I'm not still there. Details in the updated question – floatingpurr Feb 6 at 11:05
  • 2
    @ankii, See my updated answer for using application process .... – user3439894 Feb 6 at 16:48
3

You can use:

tell application "System Events" to keystroke "q" using {control down, command down}

You could also use the following instead:

tell application "System Events"
    set appName to (name of every application process whose frontmost is true and visible is true) as string
    click menu item "Lock Screen" of menu 1 of menu bar item "Apple" of menu bar 1 of application process appName
end tell

      Hint: Mouse over and horizontal/vertical scroll to see full code.

However, the first mentioned solution is more direct.

| improve this answer | |
  • Unfortunately this still deos not work. When I try, all seems fine. Later, after a while, when I press the button, I get the error "com.automator.runner.xpc is not allowed to send keystrokes". Excuse me for the late notice. – floatingpurr Feb 7 at 9:17
2

There already is a Screen Lock button for the Touch Bar natively !

Here is how to set up the Screen Lock button on Touch Bar:

  1. Go to the Apple menu and choose “System Preferences” and then go to “Keyboard”
  2. Under the “Keyboard” tab choose “Customize Control Strip” enter image description here
  3. Expand the Touch Bar and then drag the “Screen Lock” button into the touch bar screen (drag it down off the Mac display and it will pop up on the Touch Bar)

enter image description here

  1. Click on “Done” and exit out of Keyboard preferences, the Screen Lock button is now available on the Mac Touch Bar

enter image description here

| improve this answer | |
  • 1
    Yes, I know. But I'd like to group all custom buttons under the QuickAction button. I'm afraid I need an AppleScript to do this. – floatingpurr Feb 6 at 10:39

You must log in to answer this question.

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