1

I want to lock my Mac's screen remotely. I don't want to sleep my Mac. Only the screen. I don't want to require a password every time I put my monitor to sleep (system prefs/require password). And I definitely don't want to suspend the current user (CGSession -suspend) because my Mac also serves as a media control center.

I tried using an applescript to create an app I could launch remotely, but this command in the applescript fails:

 tell application "System Events"
      key code 12 using {command down, control down}
 end tell

When I launch the applescript over terminal, I get an error telling me that osascript is not allowed to send keystrokes.

Here's what I'm trying to accomplish: I have a Philips Hue dimmer remote near my home's front door. I've reprogrammed the buttons to do various things (lights on, restart playing whatever I was listening to on iTunes last, etc). It's great, except, I want the OFF button to turn off my lights, stop iTunes, etc, and lock my Mac's monitor. Everything works but that last step.

I can sleep my Mac's monitor using

do shell script "pmset displaysleepnow"

I've settled with that for now, but that sleeps the monitor. I want to lock the monitor.

I know I could go into system prefs and require a password after sleep, but I don't want to use a password every time I sleep my monitor. That would drive me crazy. I also know I could do something like this:

do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"

But that suspends the current user's session, which prevents my remote from controlling my Mac. I just want to lock my monitor (command control Q) without using a hotkey, so I'll be able to trigger it remotely.

How can I lock my Mac's screen remotely?

  • Have you tried breaking out the screen locking portion of your script into an AppleScript Application, and then just calling that Application from osascript? That should work around the issue of osascript not being able to send key commands. – Chris Norman Aug 21 at 3:48
  • Yup. I started with an applescript. When that failed, I tried saving the applescript as an app. That failed too. I can launch the app on my Mac and it works perfectly (once I went into system prefs security/accessibility and gave it permission), but the command in the app to lock the screen fails when the app is launched from terminal. So strange! – 2oh1 Aug 21 at 4:17
  • I am talking about launching the script from another script. So if your AppleScript app is called "test.app" then call it from the osascript as "tell application "test" to activate" then the Applescript Application should launch and do the key commands. – Chris Norman Aug 21 at 5:20
  • Gotcha. I just tried that and it failed too. I made a script called "testing.scpt" and, in that script, I used run script file to run the lock script and it did the same thing. It executed all of the commands in the lock script except for locking my monitor. I then tried it again by saving the testing script as an app that called the other app. It failed too: execution error: System Events got an error: osascript is not allowed to send keystrokes. (1002). ...it's so strange how it'll run on my Mac, but it can't run when called from terminal. – 2oh1 Aug 21 at 5:33
  • using "CGSession -suspend" should not prevent you from remote controlling the Mac. Are you using macOS's built-in Screen Sharing, or some other app? You might have to re-initiate your VNC session, but it should still be possible to control it. I'm curious to know if that does not work. – TJ Luoma Aug 21 at 7:05
2

I found an answer.

The Lock Screen app on Github does exactly what I wanted to do. So, by downloading it and adding this to my script, I can lock my monitor remotely:

run application "Lock Screen"

Edit: It seems the application runs the following script:

activate application "SystemUIServer"
tell application "System Events"
    tell process "SystemUIServer" to keystroke "q" using {command down, control down}
end tell

Adding that to my applescript still gives me an osascript error, but triggering the Lock Screen app using applescript works perfectly for my needs.

This makes me happy!

I have a remote at my door that I programmed to turn my lights on when I click the ON button, and when I click OFF, it turns my lights off, makes sure various smart devices are off, and it locks my Mac's monitor.

If I'm headed out the door, OFF makes sure everything is off and the Mac's monitor is locked.

Love it!

  • I tried using the edited part of the script instead of the Lock Monitor app, and it still gives me an osascript error. – 2oh1 Aug 21 at 7:07
  • Great you could solve this issue. I'd like to share an alternative: create a service with Automator that runs this AppleScript code: on run {input, parameters} do shell script "open /System/Library/CoreServices/ScreenSaverEngine.app" end run and assign a shortcut to the service. This is the way I lock my Mac, and runs on Mojave without a hitch. – jaume Aug 21 at 7:17
  • 1
    That is a good way to go if you're using a screensaver to lock your screen. I'm not. My screensaver is just a clock. I'm a big fan of fliqlo! But I don't use it to lock my screen. – 2oh1 Aug 21 at 7:53

You must log in to answer this question.

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