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.
You can set it up like this:
sudo defaults write com.apple.loginwindow LoginHook /path/to/script