#!/bin/bash # Create Raycast script commands and Alfred keywords for Mole (clean + uninstall). set -euo pipefail BLUE='\033[0;34m' GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' ICON_STEP="➜" ICON_SUCCESS="βœ“" ICON_WARN="!" ICON_ERR="βœ—" log_step() { echo -e "${BLUE}${ICON_STEP}${NC} $1"; } log_success() { echo -e "${GREEN}${ICON_SUCCESS}${NC} $1"; } log_warn() { echo -e "${YELLOW}${ICON_WARN}${NC} $1"; } log_error() { echo -e "${RED}${ICON_ERR}${NC} $1"; } detect_mo() { if command -v mo >/dev/null 2>&1; then command -v mo elif command -v mole >/dev/null 2>&1; then command -v mole else log_error "Mole not found. Install it first via Homebrew or ./install.sh." exit 1 fi } write_raycast_script() { local target="$1" local title="$2" local mo_bin="$3" local subcommand="$4" local cmd="${mo_bin} ${subcommand}" local cmd_escaped="${cmd//\\/\\\\}" cmd_escaped="${cmd_escaped//\"/\\\"}" cat > "$target" </dev/null 2>&1; then osascript <<'APPLESCRIPT' tell application "Terminal" activate do script "${cmd_escaped}" end tell APPLESCRIPT else echo "TERM environment variable not set. Run this manually:" echo " ${cmd}" exit 1 fi EOF chmod +x "$target" } create_raycast_commands() { local mo_bin="$1" local default_dir="$HOME/Library/Application Support/Raycast/script-commands" local alt_dir="$HOME/Documents/Raycast/Scripts" local dirs=() if [[ -d "$default_dir" ]]; then dirs+=("$default_dir") fi if [[ -d "$alt_dir" ]]; then dirs+=("$alt_dir") fi if [[ ${#dirs[@]} -eq 0 ]]; then dirs+=("$default_dir") fi log_step "Installing Raycast commands..." for dir in "${dirs[@]}"; do mkdir -p "$dir" write_raycast_script "$dir/mole-clean.sh" "clean" "$mo_bin" "clean" write_raycast_script "$dir/mole-uninstall.sh" "uninstall" "$mo_bin" "uninstall" log_success "Scripts ready in: $dir" done if open "raycast://extensions/script-commands" > /dev/null 2>&1; then log_step "Raycast settings opened. Run β€œReload Script Directories”." else log_warn "Could not auto-open Raycast. Open it manually to reload scripts." fi } uuid() { if command -v uuidgen >/dev/null 2>&1; then uuidgen else # Fallback pseudo UUID openssl rand -hex 16 | sed 's/\(..\)/\1/g' | cut -c1-32 fi } create_alfred_workflow() { local mo_bin="$1" local prefs_dir="${ALFRED_PREFS_DIR:-$HOME/Library/Application Support/Alfred/Alfred.alfredpreferences}" local workflows_dir="$prefs_dir/workflows" if [[ ! -d "$workflows_dir" ]]; then log_warn "Alfred preferences not found at $workflows_dir. Skipping Alfred workflow." return fi log_step "Installing Alfred workflows..." local workflows=( "fun.tw93.mole.clean|Mole clean|clean|Run Mole clean|\"${mo_bin}\" clean" "fun.tw93.mole.uninstall|Mole uninstall|uninstall|Uninstall apps via Mole|\"${mo_bin}\" uninstall" ) for entry in "${workflows[@]}"; do IFS="|" read -r bundle name keyword subtitle command <<< "$entry" local workflow_uid="user.workflow.$(uuid | tr '[:upper:]' '[:lower:]')" local input_uid local action_uid input_uid="$(uuid)" action_uid="$(uuid)" local dir="$workflows_dir/$workflow_uid" mkdir -p "$dir" cat > "$dir/info.plist" < bundleid ${bundle} createdby Mole name ${name} objects config argumenttype 2 keyword ${keyword} subtext ${subtitle} text ${name} withspace type alfred.workflow.input.keyword uid ${input_uid} version 1 config concurrently escaping 102 script #!/bin/bash PATH="/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin" ${command} scriptargtype 1 scriptfile type 0 type alfred.workflow.action.script uid ${action_uid} version 2 connections ${input_uid} destinationuid ${action_uid} modifiers 0 modifiersubtext uid ${workflow_uid} version 1 EOF log_success "Workflow ready: ${name} (keyword: ${keyword})" done log_step "Open Alfred preferences β†’ Workflows if you need to adjust keywords." } main() { echo "" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo " Mole Quick Launchers" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" local mo_bin mo_bin="$(detect_mo)" log_step "Detected Mole binary at: ${mo_bin}" create_raycast_commands "$mo_bin" create_alfred_workflow "$mo_bin" echo "" log_success "Done! Raycast (search β€œclean” / β€œuninstall”) and Alfred (keywords: clean / uninstall) are ready." echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "" } main "$@"