A tiny Swift program to run a command whenever the screen unlocks
(I use it for remounting network shares when waking from sleep)
# run-on-macos-screen-unlock <command-to-run-on-unlock> [command-args]
run-on-macos-screen-unlock ./examples/mount-network-shares.sh
# add to launchctl (start on login)
serviceman add --user \
--path "$PATH" \
./run-on-macos-screen-unlock ./examples/mount-network-shares.sh
- Download
curl --fail-with-body -L -O https://github.com/coolaj86/run-on-macos-screen-unlock/releases/download/v1.0.0/run-on-macos-screen-unlock-v1.0.0.tar.gz
- Extract
tar xvf ./run-on-macos-screen-unlock-v1.0.0.tar.gz
- Allow running even though it's unsigned
xattr -r -d com.apple.quarantine ./run-on-macos-screen-unlock
- Move into your
PATH
mv ./run-on-macos-screen-unlock ~/bin/
You'll see notifications similar to these when adding launchctl services yourself:
-
Install
serviceman
curl --fail-with-body -sS https://webi.sh/serviceman | sh source ~/.config/envman/PATH.env
-
Register with Launchd
(changeCOMMAND_GOES_HERE
to your command)serviceman add --user \ --path "$PATH" \ ~/bin/run-on-macos-screen-unlock COMMAND_GOES_HERE
-
Download the template plist file
curl --fail-with-body -L -O https://raw.githubusercontent.com/coolaj86/run-on-macos-screen-unlock/main/examples/run-on-macos-screen-unlock.COMMAND_LABEL_GOES_HERE.plist
-
Change the template variables to what you need:
USERNAME_GOES_HERE
(the result of$(id -u -n)
orecho $USER
)COMMAND_LABEL_GOES_HERE
(lowercase, dashes, no spaces)COMMAND_GOES_HERE
(the example uses./examples/mount-network-shares.sh
)
-
Rename and move the file to
~/Library/LaunchDaemons/
mv ./run-on-macos-screen-unlock.COMMAND_LABEL_GOES_HERE.plist ./run-on-macos-screen-unlock.example-label.plist mv ./run-on-macos-screen-unlock.*.plist ~/Library/LaunchDaemons/
-
Register using
launchctl
launchctl load -w ~/Library/LaunchAgents/run-on-macos-screen-unlock.*.plist
tail -f ~/.local/share/run-on-macos-screen-unlock.*/var/log/run-on-macos-screen-unlock.*.log
- Install XCode Tools
(includinggit
andswift
)xcode-select --install
- Clone and enter the repo
git clone https://github.com/coolaj86/run-on-macos-screen-unlock.git pushd ./run-on-macos-screen-unlock/
- Build with
swiftc
swiftc ./run-on-macos-screen-unlock.swift
- Git tag and push
git tag v1.0.x git push --tags
- Create a release
https://github.com/coolaj86/run-on-macos-screen-unlock/releases/new - Tar and upload
tar cvf ./run-on-macos-screen-unlock-v1.0.x.tar ./run-on-macos-screen-unlock gzip ./run-on-macos-screen-unlock-v1.0.x.tar open .
- How to run a command on lock/unlock (the snippets from which this repo grew)
- EventScripts
- HammarSpoon: caffeinate.watcher