#!/bin/zsh STARTUP_DIR=~/.startup launch() { local item="$1" if [[ ! -e "$STARTUP_DIR/$item" ]]; then if [[ "$item" != *.app && -e "$STARTUP_DIR/$item.app" ]]; then item="$item.app" elif [[ "$item" != *.webloc && -e "$STARTUP_DIR/$item.webloc" ]]; then item="$item.webloc" elif [[ "$item" != *.link && -e "$STARTUP_DIR/$item.link" ]]; then item="$item.link" elif [[ "$item" != *.shortcut && -e "$STARTUP_DIR/$item.shortcut" ]]; then item="$item.shortcut" else echo "Item $item does not exist in $STARTUP_DIR" return 1 fi fi if [[ "$item" == *.app ]]; then open "$STARTUP_DIR/$item" elif [[ "$item" == *.webloc ]]; then open "$STARTUP_DIR/$item" elif [[ "$item" == *.shortcut ]]; then shortcuts run "$(basename -s .shortcut "$item")" elif [[ "$item" == *.link ]]; then cat "$STARTUP_DIR/$item" | open else mkdir -p /tmp/logs local sanitized_item=$(echo "$item" | tr ' ' '_') local stdout_file=$(mktemp /tmp/logs/${sanitized_item}.out.XXXXXX) local stderr_file=$(mktemp /tmp/logs/${sanitized_item}.err.XXXXXX) nohup "$STARTUP_DIR/$item" > "$stdout_file" 2> "$stderr_file" & echo "stdout: $stdout_file" echo "stderr: $stderr_file" fi } logs() { local item="$1" local sanitized_item=$(echo "$item" | tr ' ' '_') local latest_stdout=$(ls -t /tmp/logs/${sanitized_item}.out.* 2>/dev/null | head -n 1) local latest_stderr=$(ls -t /tmp/logs/${sanitized_item}.err.* 2>/dev/null | head -n 1) if [[ -z "$latest_stdout" && -z "$latest_stderr" ]]; then echo "No logs found for $item" return 1 fi [[ -n "$latest_stdout" ]] && echo " stdout:\n" && cat "$latest_stdout" [[ -n "$latest_stderr" ]] && echo "\n\n stderr:\n" && cat "$latest_stderr" } while [[ $# -gt 0 ]]; do key="$1" case $key in --add-app) ln -s "/Applications/$2" "$STARTUP_DIR/$2"; shift 2 ;; --add-script) cp "$2" "$STARTUP_DIR/"; shift 2 ;; --add-shortcut) touch "$STARTUP_DIR/$2.shortcut"; shift 2 ;; --add-link) echo "$3" > "$STARTUP_DIR/$2.link"; shift 3 ;; --is-running) pgrep -f "$2" > /dev/null && echo "$2 is running" || echo "$2 is NOT running"; shift 2 ;; --quit) pkill -f "$2"; shift 2 ;; --force-quit) pkill -9 -f "$2"; shift 2 ;; --remove) rm "$STARTUP_DIR/$2"; shift 2 ;; --list) find "$STARTUP_DIR" -not -name ".*" -type f,l -execdir sh -c 'pgrep -fq {} && echo "running {}" || echo "terminated {}"' \; | sort; shift ;; --launch) launch "$2"; shift 2 ;; --logs) logs "$2"; shift 2 ;; *) echo "Unknown option: $key" exit 1 ;; esac done