Using Apple Automator to Lock and Sleep Screens

Cory Customizations, Mac, Software

First let me mention my current setup.

  • 15″ 2012 Macbook Pro running MacOS Catalina on a 2.6Ghz Quad-Core i7 cpu with 8GB ram and 256 SSD. The superdrive is replaced a 1Tb HDD for storage.
  • An old Kensington USB-3 dock drives one 23″ LG IPS monitor. A second 23″ LG IPS monitor connects directly to the laptop over mini-display port.

I like to leave my computer on all the time because it acts as my file server and I can access my files remotely through file sharing on my 2019 MacBook Air. (I use a great little free app called “Amphetamine” to keep the mac from falling asleep.) But I don’t like to leave my 3 monitors on when I’m away so every day, when I’m done with work in my office, I lock the screens, turn the brightness down to zero on the laptop, then manually turn off each monitor. That’s like a whole 10 seconds of my time. Every day.  Lame right? For the longest time I figured there should be a better, quicker way to execute this labor-intensive process.

After building my brand new office (pictured above), I really wanted to revisit this process and it turned out MUCH easier than I thought. I found a couple scripts that others had used (why reinvent the wheel) and compiled them into a nice workflow in Automator.

First let me say how underrated Automator is. For those who don’t know, it’s a built in app on MacOS that lets you, well, automate tasks. It can get files and convert or move them, combine files, send emails, manage your calendar and more. It can create actual applications and services. For instance, here are two different services that I’ve created in Automator that I use ALL the time:

This service shows up when I right click on a PDF and it converts the pdf to an image and puts it into a folder that I selected in the Automator workflow. Time saver!

This service resizes the image I right click on to 1200 x 900, which I’ve deemed a perfect size for most images I need to upload to this website and elsewhere on the web.

Once upon a time, I created an application in Automator to close all open programs and shut down my laptop. So I think it’s obvious from these three examples how extensive and useful Automator can be for day to day tasks.

So to lock my screens and put them to sleep, keeping my mac awake, I used Automator once again. The screenshot of the workflow is at the top of the article. I’ll explain what’s happening here. It’s pretty simple.

  1. After I found and tested a few scripts which do the real work (I’ll get to those below) I wanted to spice up the whole experience a bit so I added the “Run Shell Script” action and added a command to make the built in Samantha voice tell me to enjoy the rest of my day.
  2. The workflow then proceeds to run an AppleScript which locks the computer.

    on run {input, parameters}activate application “SystemUIServer”

    tell application “System Events”

    tell process “SystemUIServer” to keystroke “q” using {command down, control down}

    end tell

    return input

    end run

    It essentially presses the keys which do the work. The same keystrokes can be found by going to Apple>Lock Screen. Note that if you’re running Catalina or greater you have to give Automator (and Script Editor – if you’re testing there first like I did) permission to enter keystrokes. Do that in sys prefs>security privacy>privacy>accessibility.

  3. Then the workflow runs another shell script to complete the process:

    pmset displaysleepnow

    This command makes all three of my screens/monitors sleep.

Easy, right? The answer is “Yes”. Once I saved the workflow, I needed to create the app so I could run it. Automator allows you to convert the workflow to an application. Once I did that I saved it and called it “Signingoff.app” and found a nice “waving” icon on flaticons.com just to polish it off.

Of course I needed to also give this new app permissions to run (enter keystrokes) so I did that back in sys prefs and checked the box next to the app to allow it.

Now the app resides in my dock and can be executed at will. I could even schedule it to run but I haven’t found a need for that yet. Whoop!! for 10 seconds a day saved!