Untitled

 avatar
unknown
plain_text
2 months ago
3.9 kB
13
Indexable
#!/usr/bin/env bash
# Creates a Spotlight-searchable .app that opens Terminal.app in a project
# directory with a distinctive background color and runs a command (e.g. `claude`).
#
# Output: ~/Applications/<Name>.app
#
# Re-run anytime to add/overwrite an app.

set -euo pipefail

echo "── New project terminal app ──"
read -rp "Name (shown in Spotlight, e.g. 'O8 Core'): " NAME
if [[ -z "${NAME// }" ]]; then echo "Name is required"; exit 1; fi

read -rp "Project path: " DIR
DIR="${DIR/#\~/$HOME}"
if [[ ! -d "$DIR" ]]; then
  echo "⚠ Directory does not exist: $DIR"
  read -rp "Continue anyway? [y/N] " YN
  [[ "${YN,,}" == "y" ]] || exit 1
fi

read -rp "Command to run after cd [claude]: " CMD
CMD="${CMD:-claude}"

cat <<EOF

Color presets:
  1) red       2) green     3) blue     4) yellow
  5) purple    6) cyan      7) orange   8) pink
  9) custom hex (RRGGBB)
EOF
read -rp "Choose color [1-9]: " COLOR
case "$COLOR" in
  1) HEX="cc3333" ;;
  2) HEX="33aa55" ;;
  3) HEX="3366cc" ;;
  4) HEX="d4a017" ;;
  5) HEX="9933cc" ;;
  6) HEX="2aa8a8" ;;
  7) HEX="ff8833" ;;
  8) HEX="e03e88" ;;
  9) read -rp "Hex (no #): " HEX ;;
  *) HEX="3366cc" ;;
esac
HEX="${HEX#\#}"
if ! [[ "$HEX" =~ ^[0-9a-fA-F]{6}$ ]]; then
  echo "Invalid hex: $HEX"; exit 1
fi

cat <<EOF

Style:
  1) Dark with tint  — near-black background with subtle color, white text (recommended)
  2) Vibrant         — saturated background, white text (retro/obvious)
  3) Pastel          — light tinted background, dark text (solarized-ish)
EOF
read -rp "Choose style [1-3]: " STYLE
STYLE="${STYLE:-1}"

# Parse hex -> 8-bit RGB
R8=$((16#${HEX:0:2}))
G8=$((16#${HEX:2:2}))
B8=$((16#${HEX:4:2}))

clamp() { local v=$1; (( v < 0 )) && v=0; (( v > 255 )) && v=255; echo "$v"; }

case "$STYLE" in
  1) # Dark tint: mix color at 18% over near-black
     BG_R=$(clamp $(( R8 * 18 / 100 + 12 )))
     BG_G=$(clamp $(( G8 * 18 / 100 + 12 )))
     BG_B=$(clamp $(( B8 * 18 / 100 + 12 )))
     TX_R=240; TX_G=240; TX_B=240 ;;
  2) # Vibrant: saturated
     BG_R=$R8; BG_G=$G8; BG_B=$B8
     TX_R=255; TX_G=255; TX_B=255 ;;
  3) # Pastel: mix 20% color + 80% cream
     BG_R=$(clamp $(( R8 * 20 / 100 + 235 * 80 / 100 )))
     BG_G=$(clamp $(( G8 * 20 / 100 + 232 * 80 / 100 )))
     BG_B=$(clamp $(( B8 * 20 / 100 + 222 * 80 / 100 )))
     TX_R=40; TX_G=40; TX_B=40 ;;
  *) echo "Invalid style"; exit 1 ;;
esac

# AppleScript colors are 16-bit (0..65535). 8-bit * 257 = 16-bit.
to16() { echo $(( $1 * 257 )); }
BG_R16=$(to16 $BG_R); BG_G16=$(to16 $BG_G); BG_B16=$(to16 $BG_B)
TX_R16=$(to16 $TX_R); TX_G16=$(to16 $TX_G); TX_B16=$(to16 $TX_B)

# Escape for AppleScript string literal (backslash + double-quote)
escape_as() { printf '%s' "$1" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g'; }
DIR_ESC=$(escape_as "$DIR")
CMD_ESC=$(escape_as "$CMD")
NAME_ESC=$(escape_as "$NAME")

APP_DIR="$HOME/Applications"
mkdir -p "$APP_DIR"
OUT="$APP_DIR/${NAME}.app"

TMP_SCRIPT=$(mktemp /tmp/termapp.XXXXXX.applescript)
cat > "$TMP_SCRIPT" <<APPLESCRIPT
tell application "Terminal"
    activate
    set newTab to do script "cd \"${DIR_ESC}\" && clear && ${CMD_ESC}"
    delay 0.15
    set background color of newTab to {${BG_R16}, ${BG_G16}, ${BG_B16}}
    set normal text color of newTab to {${TX_R16}, ${TX_G16}, ${TX_B16}}
    set bold text color of newTab to {${TX_R16}, ${TX_G16}, ${TX_B16}}
    set cursor color of newTab to {${TX_R16}, ${TX_G16}, ${TX_B16}}
    try
        set custom title of newTab to "${NAME_ESC}"
    end try
end tell
APPLESCRIPT

[[ -e "$OUT" ]] && rm -rf "$OUT"
osacompile -o "$OUT" "$TMP_SCRIPT"
rm "$TMP_SCRIPT"

echo ""
echo "✓ Created: $OUT"
echo "  Color:  #${HEX}  →  bg rgb(${BG_R},${BG_G},${BG_B})  text rgb(${TX_R},${TX_G},${TX_B})"
echo "  Spotlight: Cmd+Space → type \"${NAME}\""
echo ""
echo "Tip: Finder → Get Info on the .app → paste a PNG onto the icon to customise."
Editor is loading...
Leave a Comment