Skip to content

About

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

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

17 Commits

Folders and files

Repository files navigation

lock-observer

A tiny Swift program to run a command whenever the screen locks or unlocks
(I use it for set my status offline or online at simple webpage)

# lock-observer [OPTIONS] <onlock_command> <onunlock_command>
lock-observer ./onlock ./onunlock
# add to launchctl (start on login)
serviceman add --user \
    --path "$PATH" \
    ./lock-observer ./examples/mount-network-shares.sh

Table of Contents

Acknowledgement

Forked from coolaj86/lock-observer. Another more professional fork smartwatermelon/run-on-macos-screen-events.

Install

  1. Download
    curl --fail-with-body -L -O https://github.com/coolaj86/lock-observer/releases/download/v1.0.0/lock-observer-v1.0.0.tar.gz
  2. Extract
    tar xvf ./lock-observer-v1.0.0.tar.gz
  3. Allow running even though it's unsigned
    xattr -r -d com.apple.quarantine ./lock-observer
  4. Move into your PATH
    mv ./lock-observer ~/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/lock-observer COMMAND_GOES_HERE

With a plist template

  1. Download the template plist file

    curl --fail-with-body -L -O https://raw.githubusercontent.com/coolaj86/lock-observer/main/examples/lock-observer.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 ./lock-observer.COMMAND_LABEL_GOES_HERE.plist ./lock-observer.example-label.plist
    mv ./lock-observer.*.plist ~/Library/LaunchDaemons/
  4. Register using launchctl

    launchctl load -w ~/Library/LaunchAgents/lock-observer.*.plist

View logs

tail -f ~/.local/share/lock-observer.*/var/log/lock-observer.*.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/coolaj86/lock-observer.git
    pushd ./lock-observer/
  3. Build with swiftc
    swiftc ./lock-observer.swift

Publish Release

  1. Git tag and push
    git tag v1.0.x
    git push --tags
  2. Create a release
    https://github.com/coolaj86/lock-observer/releases/new
  3. Tar and upload
    tar cvf ./lock-observer-v1.0.x.tar ./lock-observer
    gzip ./lock-observer-v1.0.x.tar
    open .

Similar Products

About

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

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages