From 82f5e4af79c63091eaeca682fee0bf8086a8f575 Mon Sep 17 00:00:00 2001 From: Esteban Vincent <73063856+EstebanVincent@users.noreply.github.com> Date: Tue, 19 May 2026 13:13:45 +0200 Subject: [PATCH] feat: add base64 encode/decode raycast script (#1) --- scripts/base64.sh | 114 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100755 scripts/base64.sh diff --git a/scripts/base64.sh b/scripts/base64.sh new file mode 100755 index 0000000..0d7bead --- /dev/null +++ b/scripts/base64.sh @@ -0,0 +1,114 @@ +#!/bin/bash + +# Required parameters: +# @raycast.schemaVersion 1 +# @raycast.title Base64 +# @raycast.mode compact + +# Optional parameters: +# @raycast.icon πŸ”‘ +# @raycast.argument1 { "type": "text", "placeholder": "encode | decode" } +# @raycast.argument2 { "type": "text", "placeholder": "text, file path, or blank for clipboard", "optional": true } + +# Documentation: +# @raycast.description Encode or decode base64. Handles text, file paths, and images. +# @raycast.author Esteban Vincent + +MODE=$(echo "$1" | tr '[:upper:]' '[:lower:]') +INPUT="$2" + +if [[ "$MODE" != "encode" && "$MODE" != "decode" ]]; then + echo "❌ Mode must be 'encode' or 'decode'" + exit 1 +fi + +# ── ENCODE ──────────────────────────────────────────────────────────────────── +if [[ "$MODE" == "encode" ]]; then + if [[ -n "$INPUT" && -f "$INPUT" ]]; then + # File path β†’ base64 + RESULT=$(base64 -i "$INPUT") + echo -n "$RESULT" | pbcopy + echo "File encoded β†’ clipboard βœ…" + elif [[ -n "$INPUT" ]]; then + # Plain text β†’ base64 + RESULT=$(echo -n "$INPUT" | base64) + echo -n "$RESULT" | pbcopy + echo "Text encoded β†’ clipboard βœ…" + else + # Try clipboard image first, then fall back to text + TMPFILE=$(mktemp /tmp/b64clipboard_XXXXXX) + osascript -e "write (the clipboard as Β«class PNGfΒ») to (open for access POSIX file \"$TMPFILE\" with write permission)" 2>/dev/null + if [[ -s "$TMPFILE" ]]; then + RESULT=$(base64 -i "$TMPFILE") + rm -f "$TMPFILE" + echo -n "$RESULT" | pbcopy + echo "Clipboard image encoded β†’ clipboard βœ…" + else + rm -f "$TMPFILE" + CLIPBOARD=$(pbpaste) + if [[ -z "$CLIPBOARD" ]]; then + echo "❌ Clipboard is empty" + exit 1 + fi + RESULT=$(echo -n "$CLIPBOARD" | base64) + echo -n "$RESULT" | pbcopy + echo "Clipboard text encoded β†’ clipboard βœ…" + fi + fi + +# ── DECODE ──────────────────────────────────────────────────────────────────── +else + if [[ -n "$INPUT" ]]; then + B64="$INPUT" + else + B64=$(pbpaste) + fi + + if [[ -z "$B64" ]]; then + echo "❌ No input provided" + exit 1 + fi + + # Decode to a temp file to inspect content + TMPFILE=$(mktemp /tmp/b64decode_XXXXXX) + if ! echo "$B64" | base64 -d > "$TMPFILE" 2>/dev/null; then + rm -f "$TMPFILE" + echo "❌ Invalid base64 input" + exit 1 + fi + + FILETYPE=$(file --brief --mime-type "$TMPFILE") + + case "$FILETYPE" in + image/png) + osascript -e "set the clipboard to (read (POSIX file \"$TMPFILE\") as Β«class PNGfΒ»)" + rm -f "$TMPFILE" + echo "PNG decoded β†’ clipboard πŸ–ΌοΈ" + ;; + image/jpeg) + osascript -e "set the clipboard to (read (POSIX file \"$TMPFILE\") as JPEG picture)" + rm -f "$TMPFILE" + echo "JPEG decoded β†’ clipboard πŸ–ΌοΈ" + ;; + image/gif|image/webp|image/*) + # For other image types, copy the file path instead + IMGFILE="/tmp/b64_image_$(date +%s).${FILETYPE##*/}" + mv "$TMPFILE" "$IMGFILE" + echo -n "$IMGFILE" | pbcopy + echo "Image (${FILETYPE##*/}) saved β†’ $IMGFILE, path copied βœ…" + ;; + text/*) + DECODED=$(cat "$TMPFILE") + rm -f "$TMPFILE" + echo -n "$DECODED" | pbcopy + echo "Text decoded β†’ clipboard βœ…" + ;; + *) + # Unknown binary β€” save to file, copy path + OUTFILE="/tmp/b64_decoded_$(date +%s).bin" + mv "$TMPFILE" "$OUTFILE" + echo -n "$OUTFILE" | pbcopy + echo "Binary decoded β†’ $OUTFILE, path copied βœ…" + ;; + esac +fi