#!/bin/bash # LogiSwitch for macOS - install or update for the current user (no admin password needed). # bash install.sh install / update, keep existing settings # bash install.sh --edge Left --channel 1 also set where the PC is and the PC's Easy-Switch channel set -euo pipefail SRC="$(cd "$(dirname "$0")" && pwd)" DIR="$HOME/Library/Application Support/LogiSwitch" PLIST="$HOME/Library/LaunchAgents/com.logiswitch.agent.plist" LABEL="com.logiswitch.agent" EDGE=""; CHANNEL=""; QUIET=0 while [ $# -gt 0 ]; do case "$1" in --edge) EDGE="$2"; shift 2 ;; --channel) CHANNEL="$2"; shift 2 ;; --quiet) QUIET=1; shift ;; *) echo "Unknown option: $1"; exit 2 ;; esac done # Everything below is logged from the first line: to the Desktop (always easy to find) and to the # install folder. Paste LogiSwitch-install-log.txt into Claude if anything goes wrong. DESKTOP_LOG="$HOME/Desktop/LogiSwitch-install-log.txt" mkdir -p "$DIR" "$HOME/Library/LaunchAgents" : > "$DESKTOP_LOG" 2>/dev/null || DESKTOP_LOG="/dev/null" exec > >(tee -a "$DIR/install.log" "$DESKTOP_LOG") 2>&1 echo "== LogiSwitch install $(date '+%Y-%m-%d %H:%M:%S') from $SRC" echo "macOS $(sw_vers -productVersion 2>/dev/null) on $(uname -m)" if [ "$(uname)" != "Darwin" ]; then echo "This installer is for macOS. On Windows, double-click Install.cmd instead." exit 1 fi # If anything fails: write a ready-to-paste request for Claude (prompt + error + source), # copy it to the clipboard and open it, so one paste into any Claude chat gets the fix. make_fix_request() { local out="$HOME/Desktop/LogiSwitch-fix-request.txt" { cat "$SRC/ASK-CLAUDE.txt" 2>/dev/null | sed -n '/^=== MAC: INSTALL FAILED/,/^=== END MAC: INSTALL FAILED/p' echo echo "===== macOS =====" sw_vers 2>/dev/null; echo "Architecture: $(uname -m)" echo "Swift compiler: $(xcrun swiftc --version 2>&1 | head -1)" echo echo "===== INSTALL OUTPUT (last 120 lines) =====" tail -n 120 "$DIR/install.log" 2>/dev/null for f in LogiSwitch.swift install.sh; do echo; echo "===== SOURCE: $f ====="; cat "$SRC/$f" 2>/dev/null done } | sed -e "s/$(id -un | sed 's/[]\/$*.^[]/\\&/g')//g" > "$out" pbcopy < "$out" 2>/dev/null || true open -e "$out" 2>/dev/null || true echo echo "Install FAILED. A fix request was saved to your Desktop (LogiSwitch-fix-request.txt)" echo "and copied to the clipboard: paste it into any Claude chat and follow the answer." } # Any failure (not just command errors) produces the fix request. Exit code 3 = waiting for # Apple's Command Line Tools, which is not a failure. INSTALL_OK=0 on_exit() { local rc=$? if [ "$INSTALL_OK" != 1 ] && [ "$rc" != 0 ] && [ "$rc" != 3 ]; then echo "Failed with exit code $rc." sleep 1; make_fix_request fi } trap on_exit EXIT # 1. Ready-built app in the package? (Saved there by the first successful build, so later # installs on any Mac need no compiler.) Otherwise Apple's Command Line Tools are needed. VERSION="$(cat "$SRC/version.txt" 2>/dev/null || echo '?')" PREBUILT="$SRC/mac-bin/LogiSwitch" USE_PREBUILT=0 if [ -f "$PREBUILT" ] && [ "$(cat "$SRC/mac-bin/version.txt" 2>/dev/null)" = "$VERSION" ]; then USE_PREBUILT=1; fi if [ "$USE_PREBUILT" = 0 ] && ! xcrun --find swiftc >/dev/null 2>&1; then echo "Apple's Command Line Tools are needed to build LogiSwitch (one-time, free, from Apple)." xcode-select --install 2>/dev/null || true echo "Click Install in the window that appears, wait for it to finish, then run this installer again." exit 3 fi # 2. Stop a running copy. launchctl bootout "gui/$(id -u)/$LABEL" 2>/dev/null || true pkill -f "Application Support/LogiSwitch/LogiSwitch$" 2>/dev/null || true rm -f "$DIR/.running" # this stop was intentional, not a crash # 3. Copy files (only the Mac ones are needed; the folder is shared with the Windows version). for f in LogiSwitch.swift install.sh uninstall.sh report.sh README.txt version.txt ASK-CLAUDE.txt; do if [ -f "$SRC/$f" ] && [ "$SRC" != "$DIR" ]; then cp -f "$SRC/$f" "$DIR/"; fi done chmod +x "$DIR"/*.sh xattr -dr com.apple.quarantine "$DIR" 2>/dev/null || true # 4. Build (or reuse the ready-built app). if [ "$USE_PREBUILT" = 1 ]; then [ "$QUIET" = 1 ] || echo "Using the ready-built app from mac-bin/ (no compiler needed)." cp -f "$PREBUILT" "$DIR/LogiSwitch.new" xattr -d com.apple.quarantine "$DIR/LogiSwitch.new" 2>/dev/null || true chmod +x "$DIR/LogiSwitch.new" codesign --force --sign - "$DIR/LogiSwitch.new" 2>/dev/null || true # local ad-hoc signature else [ "$QUIET" = 1 ] || echo "Building (about a minute)..." B="$DIR/build"; rm -rf "$B"; mkdir -p "$B" # Universal app (Apple silicon + Intel) so the saved copy works on any Mac; fall back to this Mac only. if xcrun swiftc -O -target arm64-apple-macos12 -o "$B/arm64" "$DIR/LogiSwitch.swift" -framework AppKit -framework IOKit \ && xcrun swiftc -O -target x86_64-apple-macos12 -o "$B/x86_64" "$DIR/LogiSwitch.swift" -framework AppKit -framework IOKit \ && lipo -create -output "$DIR/LogiSwitch.new" "$B/arm64" "$B/x86_64"; then [ "$QUIET" = 1 ] || echo "Built for Apple silicon and Intel Macs." else xcrun swiftc -O -o "$DIR/LogiSwitch.new" "$DIR/LogiSwitch.swift" -framework AppKit -framework IOKit fi rm -rf "$B" # Save the built app into the package, so the next Mac installs without a compiler. if mkdir -p "$SRC/mac-bin" 2>/dev/null && cp -f "$DIR/LogiSwitch.new" "$SRC/mac-bin/LogiSwitch" 2>/dev/null; then echo "$VERSION" > "$SRC/mac-bin/version.txt" [ "$QUIET" = 1 ] || echo "Saved the built app to $SRC/mac-bin (re-zip this folder to share it)." fi fi mv -f "$DIR/LogiSwitch.new" "$DIR/LogiSwitch" # 5. Settings (keeps existing ones; remembers this folder for "Update > Reinstall"). ARGS=(--configure --source "$SRC") if [ -n "$EDGE" ]; then ARGS+=(--edge "$EDGE"); fi if [ -n "$CHANNEL" ]; then ARGS+=(--channel "$CHANNEL"); fi "$DIR/LogiSwitch" "${ARGS[@]}" # 6. Start at login (user LaunchAgent) and start now. cat > "$PLIST" < Label$LABEL ProgramArguments$DIR/LogiSwitch RunAtLoad KeepAliveSuccessfulExit LimitLoadToSessionTypeAqua ProcessTypeInteractive AbandonProcessGroup StandardErrorPath$DIR/stderr.log EOF # macOS sometimes refuses to re-register right after the old copy was stopped: retry, then fall back. if ! launchctl bootstrap "gui/$(id -u)" "$PLIST" 2>/dev/null; then echo "launchctl bootstrap refused; retrying..." sleep 2 launchctl bootout "gui/$(id -u)/$LABEL" 2>/dev/null || true sleep 1 if ! launchctl bootstrap "gui/$(id -u)" "$PLIST"; then echo "launchctl still refused; starting LogiSwitch directly (it will start at login next time)." nohup "$DIR/LogiSwitch" >/dev/null 2>&1 & fi fi sleep 2 if pgrep -f "Application Support/LogiSwitch/LogiSwitch$" >/dev/null; then echo "LogiSwitch is running." else echo "WARNING: LogiSwitch did not stay running. See $DIR/stderr.log and watch.log."; fi INSTALL_OK=1 if [ "$QUIET" != 1 ]; then echo echo "LogiSwitch $(cat "$DIR/version.txt" 2>/dev/null) installed." echo "Look for the arrows icon in the menu bar (top right). Click it for options." echo "Default: the PC is to the LEFT of this Mac and is on Easy-Switch channel 1." fi