Skip to content

A tiny Swift program to run a command whenever the screen locks or unlocks

License

Notifications You must be signed in to change notification settings

smartwatermelon/run-on-macos-screen-events

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

run-on-macos-screen-events

A tiny Swift program to run a command whenever the screen locks or unlocks
(I use it for locking other local machines' screens when locking the primary, and remounting network shares when waking from sleep)

# run-on-macos-screen-events <command-to-run-on-unlock> [command-args]
run-on-macos-screen-events ./examples/mount-network-shares.sh
# add to launchctl (start on login)
serviceman add --user \
    --path "$PATH" \
    ./run-on-macos-screen-events ./examples/mount-network-shares.sh

Table of Contents

Acknowledgement

Forked from coolaj86/run-on-macos-screen-unlock.

Install

  1. Download
    curl --fail-with-body -L -O https://github.com/smartwatermelon/run-on-macos-screen-events/releases/download/v1.1.0/run-on-macos-screen-events-v1.1.0.tar.gz
  2. Extract
    tar xvf ./run-on-macos-screen-events-v1.1.0.tar.gz
  3. Allow running even though it's unsigned
    xattr -r -d com.apple.quarantine ./run-on-macos-screen-events
  4. Move into your PATH
    mv ./run-on-macos-screen-events ~/bin/

Run on Login

You'll see notifications similar to these when adding launchctl services yourself:

Background Items Added Login Items from unidentified developer

With serviceman

  1. Install serviceman

    curl --fail-with-body -sS https://webi.sh/serviceman | sh
    source ~/.config/envman/PATH.env
  2. Register with Launchd
    (change COMMAND_GOES_HERE to your command)

    serviceman add --user \
        --path "$PATH" \
        ~/bin/run-on-macos-screen-events COMMAND_GOES_HERE

With a plist template

  1. Download the template plist file

    curl --fail-with-body -L -O https://raw.githubusercontent.com/smartwatermelon/run-on-macos-screen-events/main/examples/run-on-macos-screen-events.COMMAND_LABEL_GOES_HERE.plist
  2. Change the template variables to what you need:

    • USERNAME_GOES_HERE (the result of $(id -u -n) or echo $USER)
    • COMMAND_LABEL_GOES_HERE (lowercase, dashes, no spaces)
    • COMMAND_GOES_HERE (the example uses ./examples/mount-network-shares.sh)
  3. Rename and move the file to ~/Library/LaunchDaemons/

    mv ./run-on-macos-screen-events.COMMAND_LABEL_GOES_HERE.plist ./run-on-macos-screen-events.example-label.plist
    mv ./run-on-macos-screen-events.*.plist ~/Library/LaunchDaemons/
  4. Register using launchctl

    launchctl load -w ~/Library/LaunchAgents/run-on-macos-screen-events.*.plist

View logs

tail -f ~/.local/share/run-on-macos-screen-events.*/var/log/run-on-macos-screen-events.*.log

Build from Source

  1. Install XCode Tools
    (including git and swift)
    xcode-select --install
  2. Clone and enter the repo
    git clone https://github.com/smartwatermelon/run-on-macos-screen-events.git
    pushd ./run-on-macos-screen-events/
  3. Build with swiftc
    swiftc ./run-on-macos-screen-events.swift

Publish Release

  1. Git tag and push
    git tag v1.1.x
    git push --tags
  2. Create a release
    https://github.com/smartwatermelon/run-on-macos-screen-events/releases/new
  3. Tar and upload
    tar cvf ./run-on-macos-screen-events-v1.1.x.tar ./run-on-macos-screen-events
    gzip ./run-on-macos-screen-events-v1.1.x.tar
    open .

Similar Products

About

A tiny Swift program to run a command whenever the screen locks or unlocks

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 100.0%