-
-
Save gruber/b18d8b53385fa612713754799ed4d0a2 to your computer and use it in GitHub Desktop.
A shell script for Tot
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/bash | |
# Fork of CHOCK's original tot.sh to add support for dot "0" to target | |
# the first empty dot. | |
# https://gist.github.com/chockenberry/d33ef5b6e6da4a3e4aa9b07b093d3c23 | |
# | |
# 2 Mar 2020 | |
# + Incorporated suggestions from ShellCheck (https://www.shellcheck.net). | |
# Thanks to Ramsey Dow. | |
# + Changed call to `python` to `/usr/bin/python` to get Python 2, avoiding | |
# conflicts for folks whose default `python` is Python 3. | |
basename=$(basename "$0") | |
if [ -z "$*" ]; then | |
echo "usage: ${basename} <dot> [ -o | -r | <file> | - ]" | |
echo "" | |
echo "options:" | |
echo " -o open dot in window with keyboard focus" | |
echo " -r read contents of dot" | |
echo " -c clear contents of dot" | |
echo " <file> append contents of regular file to dot" | |
echo " - append standard input to dot" | |
echo "" | |
echo "examples:" | |
echo " $ cal -h | tot 1 - # put a calendar in first dot" | |
echo " $ tot 2 MyApp.crash # put a crash report in second dot" | |
echo " $ date | tot 0 - # put a timestamp in first empty dot" | |
echo "" | |
exit 1 | |
fi | |
dot="$1" | |
if [ -z "$2" ]; then | |
echo "error: no dot action specified" | |
exit 1 | |
else | |
if [ "$dot" = "0" ]; then | |
# dot 0: first empty dot; error if no dots are empty | |
for testdot in {1..7} | |
do | |
content=$(osascript -e "tell application \"Tot\" to open location \"tot://${testdot}/content\"") | |
# Matching whitespace-only strings in Bash: https://stackoverflow.com/questions/9767644/ | |
if ! [[ $content =~ [^[:space:]] ]]; then | |
# testdot is the first empty dot | |
dot=$testdot | |
# clear contents of dot, in case there's whitespace: | |
osascript -e "tell application \"Tot\" to open location \"tot://${dot}/replace?text=\"" | |
break | |
fi | |
done | |
if [ "$dot" = "0" ]; then | |
echo "error: no empty dots" | |
exit 1 | |
fi | |
fi | |
if [ "$2" = "-o" ]; then | |
# open dot | |
osascript -e "tell application \"Tot\" to open location \"tot://${dot}\"" | |
osascript -e "tell application \"Tot\" to activate" | |
elif [ "$2" = "-r" ]; then | |
# get contents of dot | |
osascript -e "tell application \"Tot\" to open location \"tot://${dot}/content\"" | |
elif [ "$2" = "-c" ]; then | |
# clear contents of dot | |
osascript -e "tell application \"Tot\" to open location \"tot://${dot}/replace?text=\"" | |
else | |
# append file or stdin to dot | |
if [ "$2" = "-" ]; then | |
FILE=$(mktemp -t "$basename") || exit 1 | |
cat /dev/stdin > "$FILE" | |
else | |
if [ -f "$2" ]; then | |
FILE="$2" | |
else | |
echo "error: not a regular file" | |
exit 1 | |
fi | |
fi | |
text=$(/usr/bin/python -c 'import urllib; import sys; print urllib.quote(sys.stdin.read())' < "$FILE") | |
osascript -e "tell application \"Tot\" to open location \"tot://${dot}/append?text=${text}\"" | |
fi | |
fi |
Author
gruber
commented
Feb 29, 2020
via email
On Feb 28, 2020, at 11:41 AM, Jonathan Buys ***@***.***> wrote:
If you've changed the default Python in your path to python3, this script will fail, but if you change line 76 to
text=`cat $FILE | python -c 'import urllib.parse; import sys; print(urllib.parse.quote(sys.stdin.read()))'`
that will fix it.
But that seems to break if you still have Python 2 as your default. Is there a way to write this such that it will work with both Python 2 and 3?
My best idea: hardcoding the call to /usr/bin/python, which should be Python 2 for everyone, regardless their default for `python`.
—J.G.
My best idea: hardcoding the call to /usr/bin/python, which should be Python 2 for everyone, regardless their default for
python
.
That's probably the right thing to do.
Done and checked in. Thanks for reporting this.
—J.G.
… On Feb 29, 2020, at 7:35 AM, Jonathan Buys ***@***.***> wrote:
My best idea: hardcoding the call to /usr/bin/python, which should be Python 2 for everyone, regardless their default for python.
That's probably the right thing to do.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment