Skip to content

Instantly share code, notes, and snippets.

@ammmze
Last active January 21, 2020 04:32
Show Gist options
  • Save ammmze/124788e7c2ce64dd207c to your computer and use it in GitHub Desktop.
Save ammmze/124788e7c2ce64dd207c to your computer and use it in GitHub Desktop.
Open OS X default screenshots in monosnap (or can be customized to open in a different application)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Disabled</key>
<false/>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>com.mashupmill.screenshot-watcher</string>
<key>Program</key>
<string>/Users/brandencash/projects/screenshot-watcher/screenshot-watcher.sh</string>
<key>ProgramArguments</key>
<array>
<string>-f</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/tmp/screenshot-watcher.log</string>
<key>StandardOutPath</key>
<string>/tmp/screenshot-watcher.log</string>
</dict>
</plist>
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
ln -s "${DIR}/com.mashupmill.screenshot-watcher.plist" ~/Library/LaunchAgents/com.mashupmill.screenshot-watcher.plist
#!/bin/bash
if [ $# -eq 0 ]; then
"$0" -f
exit
fi
APP=/Applications/Monosnap.app
PATTERN='^Screen Shot [0-9]{4}-[0-9]{2}-[0-9]{2} at [0-9]{1,2}\.[0-9]{2}\.[0-9]{2} (AM|PM)\.png$'
WATCH_DIR=~/Desktop
DELETE_AFTER_OPEN=
PATH="/usr/local/bin:$PATH"
if [ -f ~/.screenshot-watcher ]; then
source ~/.screenshot-watcher
fi
for PARAM in "$@"
do
case "$PARAM" in
-w|-f)
echo "Starting fswatch on $WATCH_DIR"
npx onchange -v --filter 'add' "${WATCH_DIR}/Screen Shot *.png" -- "$0" "{{changed}}"
;;
-h|--help)
echo "Has 2 usages..."
echo "First run \`$0 -f\` which will start up fswatch on $WATCH_DIR."
echo "When file changes are detected, the file path is passed back to this script running the second usage."
echo
echo "Second usage is \`$0 <file>\`."
echo "It will check the file matches the OS X default Screen Shot filename pattern."
echo "If it does, then it will open the file in $APP and delete the file."
echo
echo "So try it out...run \`$0 -f\` then hit CMD+SHIFT+4 and create a snapshot."
echo "Then Monosnap should open up with the screenshot and the screenshot should get removed from your desktop."
;;
*)
FILE="$PARAM"
NAME=${FILE##*/}
APP_NAME=${APP##*/}
case "$APP_NAME" in
Preview.app)
# Preview.app cannot open it if its deleted
DELETE_AFTER_OPEN=${DELETE_AFTER_OPEN:-false}
;;
Monosnap.app)
# Monosnap.app supports deleting the file right after opening it...so if we don't have a value set, set it to true
DELETE_AFTER_OPEN=${DELETE_AFTER_OPEN:-true}
;;
esac
# echo "Found $FILE with name $NAME"
echo $NAME | grep -E "$PATTERN" >> /dev/null
MATCHES=$?
if [ -f "$FILE" ] && [ $MATCHES -eq 0 ]; then
echo "Opening $FILE in $APP_NAME"
open -a "$APP" "$FILE"
if [ "$DELETE_AFTER_OPEN" = true ]; then
# wait 5s then delete the file
(sleep 5s && echo "Deleting $FILE" && rm "$FILE")&
fi
fi
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment