MacID is a very useful application which automatically locks a Mac when a paired iOS device moves out of range. It will also offer to unlock the Mac using Touch ID on the device, but I use that part less than I would expect. (You can also unlock with Apple Watch, which is cool.)
The latest update adds scripting support:
You can now extend MacID by dropping AppleScript text files (they MUST be exported as text files) into onSleep, onLock, onWake, onUnlock directories in ~/Application Support/MacID/. MacID will execute these scripts at the respective event. For example, you could pause iTunes when your Mac locks, and resume playing when your Mac unlocks.
To try this, I created a simple AppleScript
tell application "iTunes" pause end tell
and saved it as a text AppleScript document in the
onUnlock folder. Works as expected!