Skip to content

Instantly share code, notes, and snippets.

@earthboundkid
Forked from zrzka/!IMPORTANT.md
Last active February 28, 2020 15:12
Show Gist options
  • Save earthboundkid/9ab99ab9696b28aa27e03c54d8c181d3 to your computer and use it in GitHub Desktop.
Save earthboundkid/9ab99ab9696b28aa27e03c54d8c181d3 to your computer and use it in GitHub Desktop.
A shell script for Tot
#!/usr/bin/env bash
# Fork of gruber's tot.sh https://gist.github.com/gruber/b18d8b53385fa612713754799ed4d0a2
# which is a fork of chockenberry's tot.sh https://gist.github.com/chockenberry/d33ef5b6e6da4a3e4aa9b07b093d3c23
#
# WARNING some options have different & potentially destructive meaning, for example:
# -r gets the dot contents in original script, but this one REPLACES the dot contents
# and does use -p to get the dot contents
#
# Exit immediately if a pipeline returns a non-zero status.
set -e
# If set, the return value of a pipeline is the value of the last (rightmost) command
# to exit with a non-zero status, or zero if all commands in the pipeline exit successfully.
set -o pipefail
basename=$(basename "$0")
#
# Helpers
#
function print_usage() {
cat <<END
Usage: ${basename} <dot> [ -p | -c | -r ] [ <file> | - ]
Options:
<dot> dot number (1 to 7) or 0 as a first empty dot
-p print contents of <dot>
-r replace contents of <dot> with <file> or stdin (-)
-c clear contents of <dot>
<file> append contents of <file> to <dot>
- append standard input to <dot>
Examples:
$ ${basename} 2 # activate Tot.app and select second dot
$ ${basename} 2 -p # print second dot contents to stdout
$ ${basename} 2 -c # clear contents of second dot
$ cal -h | ${basename} 1 - # append a calendar to first dot
$ ${basename} 2 MyApp.crash # append a crash report to second dot
$ ${basename} 2 -r MyApp.crash # replace second dot contents with a crash report
END
}
function tell_tot_to() {
osascript -e "tell application \"Tot\" to $1"
}
function tell_tot_to_append_contents_of() {
local dot=$1
local loc=$2
local text
text=$(python -c 'import urllib; import sys; print urllib.quote(sys.stdin.read())' <"$loc")
tell_tot_to "open location \"tot://$dot/append?text=${text}\""
}
function tell_tot_to_replace_contents_of() {
local dot=$1
local loc=$2
local text
text=$(python3 -c 'import urllib.parse; import sys; print(urllib.parse.quote(sys.stdin.read()))' <"$loc")
tell_tot_to "open location \"tot://$dot/replace?text=${text}\""
}
function replace_zero_dot_with_first_empty_dot() {
local testdot
local content
if ! [[ "${dot}" == "0" ]]; then
return 0
fi
for testdot in {1..7}; do
content=$(tell_tot_to "open location \"tot://${testdot}/content\"")
# Matching whitespace-only strings in Bash: https://stackoverflow.com/questions/9767644/
if ! [[ $content =~ [^[:space:]] ]]; then
# clear contents of dot, in case there's whitespace:
tell_tot_to "open location \"tot://${testdot}/replace?text=\""
dot=$testdot
break
fi
done
if ((dot == "0")); then
echo >&2 "error: no empty dots"
return 1
fi
}
#
# Main
#
if [ -z "$*" ]; then
print_usage
exit 1
fi
#
# Replace 0 with first empty dot & validate dot #
#
dot="$1"
replace_zero_dot_with_first_empty_dot
if ! [[ $dot =~ ^[1-7]$ ]]; then
echo >&2 "error: invalid dot number ${dot}"
exit 1
fi
#
# Actions
#
if [ -z "$2" ]; then
# No action specified - select dot & activate
tell_tot_to "open location \"tot://${dot}\""
tell_tot_to "activate"
exit 0
fi
case "$2" in
-p)
# Print contents of dot
tell_tot_to "open location \"tot://${dot}/content\""
;;
-c)
# Clear contents of dot
tell_tot_to "open location \"tot://${dot}/replace?text=\""
;;
-r)
# Replace contents of dot
tell_tot_to_replace_contents_of "${dot}" "$3"
;;
*)
# Append contents to dot
tell_tot_to_append_contents_of "${dot}" "$2"
;;
esac
@earthboundkid
Copy link
Author

Ran the script through shfmt and shellcheck.

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