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?

  • 2
    For the lockscreen, there's notification sent which can be handled in Your Cocoa app. You'd need to listen to notificaiton called: "com.apple.screenIsLocked". Code: [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"com.apple.screenIsLocked" object:nil]; – Mateusz Szlosek Dec 3 '14 at 13:48
  • Is there a way to get a hold of this through AppleScript? – nwinkler Dec 3 '14 at 14:32
  • Sorry I don't know of Apple Script way of doing this. But creating Your own app for this task is not so difficult. Let me know if I can help. – Mateusz Szlosek Dec 3 '14 at 14:49
  • As an addition notification sent when screen is unlocked is named: "com.apple.screenIsUnlocked" – Mateusz Szlosek Dec 4 '14 at 8:24

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

  • 2
    This program could be installed with brew install sleepwatcher you can find more information about configuration in plist files. The exact location brew info sleepwatcher. Default files are ~/.sleep and ~/.wakeup – A B Mar 18 '18 at 4:49
  • I've a problem, it seems that if my mac locks before sleeping the script is not called. what parameters have you used? – EsseTi Oct 7 '19 at 9:15
  • Sorry, haven't used this in a while, TBH... – nwinkler Oct 7 '19 at 10:05

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: EventScripts

Check it out here.

  • 1
    That looks pretty useful, thanks! It's $3.99 in the App Store. – nwinkler Dec 5 '14 at 7:09
  • I have issues with EventScript crashing silently now, so just keep an eye on it... – Pylinux May 4 '18 at 7:50

You can set it up like this:

sudo defaults write com.apple.loginwindow LoginHook /path/to/script
  • The name implies that this runs when you log in, not when you unlock the screen, though. Is that correct? – tripleee Jun 11 '19 at 10:45
  • stackoverflow.com/questions/22842016/… suggests that LoginHook is deprecated anyway. You can use launchd to run things at login, but that's not what this question is asking. – tripleee Jun 12 '19 at 5:02

You must log in to answer this question.

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