Last active
March 10, 2021 19:00
-
-
Save brandonjp/5d36299fcf03800b566a to your computer and use it in GitHub Desktop.
bash script for Alfred workflow to create a new snippet
This file contains hidden or 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/bash | |
# set qry to the query (argument one) | |
qry="$1" | |
# fix SNIP_DB location in case it contains tilde (won't expand in quotes) | |
SNIP_DB=$( echo $SNIP_DB | sed "s:^~:$HOME:" ) | |
# set the internal delimeter (idlm) to some char(s) that will never be used | |
idlm='' | |
# replace user's delimeter w/ ours, but only for the first occurence | |
qry=${qry/$DLM/$idlm} | |
# escape single quotes for sqlite3 | |
qry=${qry//\'/\'\'} | |
# get first field (snippet name/keyword) | |
name=$(echo "$qry" | awk -F"$idlm" 'NR==1 { print $1 }') | |
# get second field (snippet keyword) | |
keyword="$name" | |
# wipe field 1, print the rest for line one. print everything else | |
snippet=$(echo "$qry" | awk -F"$idlm" 'NR==1 { $1=""; print $2; } NR>1 { print $0 } ') | |
# if one of the reqd fields is null, say so and exit | |
# echod output will get sent to notification at end of workflow | |
if [ -z "$name" ] | [ -z "$keyword" ] | [ -z "$snippet" ]; then | |
echo "less than three fields" | |
exit 1 | |
fi | |
# turns out the alfred epoch is Sun Dec 31 16:00:01 PST 2000 | |
alf_epoch=978307201 | |
# calc. current alf. ts by subtracting the alf. epoch from unix epoch | |
ts=$(expr $(date +%s) - $alf_epoch) | |
# prepare sql | |
sql="INSERT INTO snippets (name,keyword,snippet,ts) | |
VALUES ('$name','$keyword','$snippet','$ts');" | |
# execute sql | |
sqlite3 "$SNIP_DB" << EOF | |
$sql | |
EOF | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment