Created
November 16, 2023 06:06
-
-
Save luckman212/b70649f1c4b007ce0c978b06ff9742c8 to your computer and use it in GitHub Desktop.
Example sprinkler alert script (Ask Different)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
#number of times after which a refill is required | |
REFILL_AFTER=25 | |
#text (case sensitive regex) that should cause the counter to increment | |
TRIGGER_STR='Sprinkler turned on' | |
#script to run when alert fires (send email, SMS, etc...) | |
ALERT_SCRIPT=/path/to/your/script | |
#directory to store data | |
DATA_DIR=$HOME/.sprinkler | |
######## DON'T CHANGE ANYTHING BELOW THIS POINT ######## | |
mkdir -p $DATA_DIR | |
touch $DATA_DIR/cnt $DATA_DIR/seen | |
read -r LAST_COUNT <$DATA_DIR/cnt | |
[[ -n LAST_COUNT ]] || LAST_COUNT=0 | |
DARWIN_USER_DIR=$(/usr/bin/getconf DARWIN_USER_DIR) | |
cd "${DARWIN_USER_DIR}com.apple.notificationcenter/db2" 2>/dev/null || exit 1 | |
[[ -e db ]] || exit 1 | |
sqlite3 db "PRAGMA wal_checkpoint(TRUNCATE)" &>/dev/null | |
sqlite3 db "SELECT hex(uuid), hex(data) FROM record" >$DATA_DIR/msgs | |
while IFS='|' read UUID MSG _ ; do | |
if grep -q $UUID $DATA_DIR/seen; then | |
continue | |
fi | |
echo "$UUID" >>$DATA_DIR/seen | |
echo "parsing new message: $UUID" | |
#messages are stored in binary plist format | |
DECODED=$( | |
xxd -r -p <<<"$MSG" | | |
plutil -convert xml1 -o - -- - | | |
xmllint --xpath "//key[.='body']/following-sibling::string[1]/text()" - | |
) | |
if [[ $DECODED =~ $TRIGGER_STR ]]; then | |
(( LAST_COUNT++ )) | |
echo "matched! count is now: $LAST_COUNT/$REFILL_AFTER" | |
echo $LAST_COUNT >$DATA_DIR/cnt | |
fi | |
done <$DATA_DIR/msgs | |
if (( LAST_COUNT >= REFILL_AFTER )); then | |
rm $DATA_DIR/cnt | |
echo "time to refill the tank!!!" | |
$ALERT_SCRIPT | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ref: https://apple.stackexchange.com/questions/466126/trigger-automation-or-notification-by-counting-other-notifications