#!/bin/bash set -euo pipefail # Optional sample installed explicitly by the user for NSUserUnixTask execution. # Usage: browser # Ordinary routing to a browser does not require this optional profile launcher. fail() { printf '%s\n' "$1" >&2 exit 64 } [[ $# -eq 4 && "$1" == browser ]] \ || fail "Usage: browser " app_path="$2" profile="$3" url="$4" [[ "$app_path" == /* && "$app_path" == *.app && -d "$app_path" ]] \ || fail "Choose an absolute app bundle path." identifier="$(/usr/bin/plutil -extract CFBundleIdentifier raw -o - -- "$app_path/Contents/Info.plist" 2>/dev/null)" \ || fail "The selected app has no readable bundle identifier." case "$identifier" in com.google.Chrome|com.google.Chrome.beta|com.google.Chrome.canary|com.microsoft.edgemac|com.brave.Browser|org.chromium.Chromium) ;; *) fail "Choose a supported Chromium browser: Chrome, Chrome Beta, Chrome Canary, Edge, Brave, or Chromium." ;; esac case "$profile" in ''|.|..|*/*|*\\*|*$'\n'*|*$'\r'*) fail "The profile must be a single directory name." ;; esac [[ "$url" != *[$'\n\r\t ']* ]] || fail "The URL must not contain whitespace." [[ "$url" =~ ^[hH][tT][tT][pP][sS]?://(\[[0-9a-fA-F:.]+\]|[a-zA-Z0-9][a-zA-Z0-9_.-]*)(:[0-9]+)?([/?#]|$) ]] \ || fail "Only an HTTP or HTTPS URL with a host is supported." exec /usr/bin/open -n -a "$app_path" --args "--profile-directory=$profile" "$url"