iOS shortcuts can run commands over SSH
By goz
When I want to automate something, my go to technology is usually Bash. I can then run the script when I’m on any of my devices or through an iOS shortcut.
What is iOS Shortcuts?
Shortcuts is the current version of an app the Workflows which was purchased by Apple. It is a way to build up scripts, consisting of steps. While it is useful, it is a pain to troubleshoot or to build complicated workflows. That’s where I add Bash scripts over SSH.
Let’s toot an example
I’ve been playing around on Mastodon and want a quick way to annoy, I mean share, with my followers from my phone. I could always start up Ivory or Metatext and toot from there, but that takes too long and not nearly geeky enough.
I have a VPS that I run all sorts of little projects on, and on this VPS I installed toot, a cli toot program. My iOS shortcuts asks for two things: what I want to share & what tags to use with the toot. The Run script over SSH step takes those two variables and runs the toot
command with them. Is this faster? Maybe. Is it cooler? Heck yeah!
More advanced
Instead of just tooting, I like to also post my toots to my website. Into Bash I went. The script takes the same two variables as the regular toot
command, but instead of just tooting, the script creates a new post for my website, separates out the tags into the metadata header, and then publishes.
Let me introduce gozshare.sh
:
#!/bin/bash
cd ${HOME}/Development/gozalert
msg=${1}
tags=${2}
s=$(echo -n "${msg}" | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z)
slug=${s:0:30}
post="${HOME}/notes/BlogPosts/${slug}.md"
d=$(date +"%Y-%m-%d")
h=$(date +"%H")
m=$(date +"%M")
emojidir="${HOME}/Development/repos/twemoji/assets/svg"
backgroundimg="${HOME}/Pictures/RoundedPurple.png"
featuredimage="https://cdn.collinsoft.com/file/cdn-collinsoft/gozgeek.com/img/2022/2022-10-18-gozgeek-avatar/2022-10-18-gozgeek-avatar-800x.png"
## If msg contains a pipe, split it to msg and body
if [[ "${msg}" == *"|"* ]]; then
body=${msg##*|}
msg=${msg%|*}
fi
first=$(printf %x "'${msg:0:1}")
if (( ${#first} > 4 )); then
msg="${msg:1}"
if [ -f "${emojidir}/${first}.svg" ]; then
/usr/bin/composite -compose atop -background none -geometry +90+90 -density 900x900 "${emojidir}/${first}.svg" "${backgroundimg}" "/tmp/${first}.png"
## Upload picture to CDN
rclone="/usr/bin/rclone"
y=$(date +"%Y")
b2url="//cdn-collinsoft/gozgeek.com/img/${y}"
url="//cdn.collinsoft.com/file/cdn-collinsoft/gozgeek.com/img/${y}"
${rclone} copyto "/tmp/${first}.png" "b2gg:${b2url}/${first}.png"
featuredimage="https:${url}/${first}.png"
fi
fi
echo "Sending - ${msg}"
${HOME}/.local/bin/toot post "${msg}
${body}
${tags}"
## If body is a web address, create a link to the page with a title
if [[ ${body:0:5} == "https" ]]; then
title=$(wget -qO- "${body}" | perl -l -0777 -ne 'print $1 if /<title.*?>\s*(.*?)\s*<\/title/si')
[ -z "${title}" ] && title="${msg}"
body="[${title}](${body})"
fi
## Create article for GozGeek
t="- aside"
# Split tags
if [ ! -z "${tags}" ]; then
for i in ${tags}
do
t="$t
- ${i:1}"
done
fi
echo "${t}"
# Put header on top
cat > "${post}"<< EOF
---
Title: "${msg}"
Author: goz
date: 2023-01-24T15:24:00-04:00
Category:
Slug: ${slug}
Tags:
${t}
featured_image: "${featuredimage}"
images: [ "$featuredimage" ]
---
${body}
EOF
${HOME}/.config/dotfiles/scripts/,postgg ${post}
(There is a bunch of hardcoded directories in the script, so you will have to modify it to make it work for you. Also, the ,postgg
script simply pusts the new post file into my website posts directory and rebuilds the site. I use Hugo for the GozGeek site)
Now, my GozShare
Shortcut passes my two variables to this script, which then toots my ramblings along with posting them to my website.
Pretty cool and slick!