Is there a way to run a specified script or program when the screen is locked or unlocked? Ideally a bash function or an AppleScript.
Is there an event that I could subscribe to that would allow me to do that? Or an existing app that supports this?
Ask Different is a question and answer site for power users of Apple hardware and software. It only takes a minute to sign up.Sign up to join this community
After some searching, I found SleepWatcher, a free tool that allows to run a script when the screen goes to sleep or wakes up.
After installing it, you can link a shell script that is run on one of the available events (which include dim screen, wake up screen, computer sleep, wake up computer and some more). From the shell script, I can run a command, or call an AppleScript that I want to run on these events.
Since my screen goes to sleep when I lock my computer, this is exactly what I was looking for.
You can see it in action here: https://github.com/nwinkler/sleepwatcher-lync-status
EventScript has a few options that might work for you. It's a neat little utility that can trigger .sh/applescript/automator workflows and trigger them on a host of different options.
Here's some screen locking/unlocking options:
Check it out here.
Info for future googlers landing here:
I got this working using EventScripts.
I wanted my desk lamp, which is connected to a TP-link smart plug to turn on when I unlock my Mac, and to turn off when it locks.
I found this bash script which can turn the smart plug on and off. (I don't have nmap installed, so I had to figure out the IP address of the smart plug by looking at my router's config screens, then supply the IP address to the
hs100.sh script via the
Then it was just a matter of writing two simple scripts for "on" and "off", to call
hs100.sh on and
hs100.sh off. Since EventScripts is on the Mac App Store, it can only run scripts in its own folder, so I put my two scripts there.
And voilà! Works perfectly!