Skip to content

Instantly share code, notes, and snippets.

@lincerely
Created December 8, 2022 02:10
Show Gist options
  • Select an option

  • Save lincerely/65a3e1c77a14be846fe4cb9367c753a9 to your computer and use it in GitHub Desktop.

Select an option

Save lincerely/65a3e1c77a14be846fe4cb9367c753a9 to your computer and use it in GitHub Desktop.
run command when mac clipboard change
#!/usr/bin/env bash
# pbwatch - run cmd when clipboard change
tmp=/tmp/clipboard
cmd=${@:1}
if [ -z "$cmd" ]; then
echo "USAGE: $0 CMD [ARGS...]"
exit 1
fi
process() {
pbpaste > $tmp
/usr/bin/env bash -c "$cmd"
}
while sleep 1; do
if [ -f $tmp ]; then
diff=`diff -q $tmp <(pbpaste) | wc -l`
if [ $diff -eq 1 ]; then
process
fi
else
process
fi
done
@lincerely
Copy link
Author

Example, read clipboard content on change: pbwatch "pbpaste | say"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment