Skip to content

Instantly share code, notes, and snippets.

@alexander-bauer
Created July 15, 2026 17:33
Show Gist options
  • Select an option

  • Save alexander-bauer/423cb7131663f267eea13e6ad30c69a7 to your computer and use it in GitHub Desktop.

Select an option

Save alexander-bauer/423cb7131663f267eea13e6ad30c69a7 to your computer and use it in GitHub Desktop.
Capture Slack emojis from the Wayland clipboard to disk, with optional desktop notifications and a watch mode for bulk export
#!/usr/bin/env bash
set -euo pipefail
## LLM-produced -- tamp those expectations down
# Usage: capture-emoji.sh [--notify]
# Reads a Slack emoji from the Wayland clipboard and saves it to the script's
# directory. Pass --notify to fire a desktop notification on success or failure.
#
# Three-way outcome:
# success -- emoji saved, notification fired (if --notify)
# silent skip -- clipboard does not contain a Slack emoji; exit 0, no notification
# real failure -- clipboard looked like an emoji but processing broke; exit 1,
# failure notification fired (if --notify)
DIR="$(cd "$(dirname "$0")" && pwd)"
mkdir -p "$DIR"
# State -- initialize all before the EXIT trap is registered (set -u safety)
NOTIFY=false
NAME=""
EXT=""
OUT=""
ERROR_MSG=""
SUCCESS=false
[[ "${1:-}" == "--notify" ]] && NOTIFY=true
TMP=$(mktemp)
on_exit() {
local rc=$?
rm -f "$TMP"
$NOTIFY || return
if [[ $SUCCESS == true && $rc -eq 0 ]]; then
notify-send \
-a "Slack Emoji" \
-i "$OUT" \
-t 3000 \
"Emoji saved" ":${NAME}: -> $(basename "$OUT")" || true
else
notify-send \
-a "Slack Emoji" \
-i dialog-error \
-u low \
-t 2000 \
"Emoji capture failed" "${ERROR_MSG:-unexpected error (exit ${rc})}" || true
fi
}
trap on_exit EXIT
# Sets ERROR_MSG, prints to stderr, and exits 1 (triggers on_exit with failure).
fail() {
ERROR_MSG="$1"
echo "Error: $1" >&2
exit 1
}
# Silent skip: clipboard was cleared or selection has no usable type
[[ "${CLIPBOARD_STATE:-}" == "nil" ]] && exit 0
# Silent skip: no text/html in clipboard -- not a Slack emoji
HTML=$(wl-paste --type text/html 2>/dev/null) || exit 0
[[ -z "$HTML" ]] && exit 0
# Silent skip: no emoji markers in HTML -- not a Slack emoji
# Assumes a single <img> tag in the clipboard HTML (true for Slack web and desktop).
# Try data-stringify-emoji=":NAME:" first (Slack web), fall back to alt=":NAME:" (Slack desktop).
NAME=$(grep -oP 'data-stringify-emoji=":\K[^:]+(?=:")' <<<"$HTML" | head -1 || true)
if [[ -z "$NAME" ]]; then
NAME=$(grep -oP 'alt=":\K[^:]+(?=:")' <<<"$HTML" | head -1 || true)
fi
[[ -z "$NAME" ]] && exit 0
# From here the clipboard looks like a Slack emoji -- failures become real errors.
SRC=$(grep -oP 'src="\K[^"]+(?=")' <<<"$HTML" | head -1 || true)
[[ -z "$SRC" ]] && fail "could not extract src URL from clipboard HTML"
# Guard: base64 data URIs are from old Slack clients and are not supported
[[ "$SRC" == data:* ]] && fail "base64 data URIs are not supported -- please use a current Slack client"
# Guard: only fetch from Slack's emoji CDN
[[ "$SRC" == https://emoji.slack-edge.com/* ]] ||
fail "unexpected src URL (expected https://emoji.slack-edge.com/...): $SRC"
# Guard: prevent path traversal via malformed emoji names
[[ "$NAME" =~ ^[A-Za-z0-9_+\-]+$ ]] || fail "invalid emoji name: $NAME"
EXT="${SRC##*.}"
if [[ "$EXT" == gif ]]; then
curl -sL --fail "$SRC" -o "$TMP" || fail "failed to download $SRC"
FRAMES=$(identify "$TMP" 2>/dev/null | wc -l)
if [[ "${FRAMES:-0}" -gt 1 ]]; then
OUT="$DIR/$NAME.gif"
cp "$TMP" "$OUT"
else
echo "Warning: .gif URL yielded a single frame; saving as PNG" >&2
EXT="png"
OUT="$DIR/$NAME.png"
convert "$TMP" "$OUT"
fi
else
EXT="png"
OUT="$DIR/$NAME.png"
wl-paste --type image/png >"$OUT" 2>/dev/null ||
fail "could not read image/png from clipboard"
read -r W H < <(identify -format "%w %h\n" "$OUT" 2>/dev/null | head -1) || true
if [[ -z "${W:-}" ]] || [[ "$W" -lt 8 ]] || [[ "$H" -lt 8 ]]; then
rm -f "$OUT"
fail "clipboard PNG appears invalid (dimensions: ${W:-?}x${H:-?})"
fi
fi
SUCCESS=true
echo "Saved: $OUT"
#!/usr/bin/env bash
set -euo pipefail
# Watches the Wayland clipboard and captures any Slack emoji that is copied.
# Fires a desktop notification on success or failure (via capture-emoji.sh --notify).
# Press Ctrl-C to stop.
DIR="$(cd "$(dirname "$0")" && pwd)"
echo "Watching clipboard for Slack emojis... (Ctrl-C to stop)"
exec wl-paste --watch "$DIR/capture-emoji.sh" --notify
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment