A script for opening parts of an iOS simulator with device selection
ios_sim_open.sh
77 lines 2.0 kB view raw
1#!/usr/bin/env bash 2set -euo pipefail 3 4# Require container type 5if [ "$#" -lt 1 ]; then 6echo "Usage: $0 <container-type> [bundle-id]" >/dev/stderr 7echo "Examples: $0 data | caches | app [com.example.app]" >/dev/stderr 8exit 1 9fi 10CONTAINER_TYPE="$1" 11 12# Optional bundle id (defaults to Pocket Casts) 13if [ "$#" -ge 2 ]; then 14BUNDLE_ID="$2" 15else 16BUNDLE_ID="au.com.shiftyjelly.podcasts" 17fi 18 19list="$( 20xcrun simctl list devices 2>/dev/null | { 21 runtime="" 22 while IFS= read -r line; do 23 case "$line" in 24 # Header lines: -- iOS 18.5 -- 25 --\ *\ --) 26 runtime="$(printf '%s\n' "$line" | sed -E 's/^-- (.*) --$/\1/')" 27 continue 28 ;; 29 esac 30 31 # Device lines: ... (Booted) 32 case "$line" in 33 *"(Booted)"*) 34 # Skip Watch/TV 35 case "$line" in *Watch*|*TV*) continue ;; esac 36 37 # Trim leading whitespace 38 trimmed="$(printf '%s\n' "$line" | sed -E 's/^[[:space:]]+//')" 39 40 name="$(printf '%s\n' "$trimmed" | sed -E 's/^([^()]+) \(([0-9A-Fa-f-]{36})\) \(Booted\).*$/\1/')" 41 udid="$(printf '%s\n' "$trimmed" | sed -nE 's/.*\(([0-9A-Fa-f-]{36})\).*/\1/p')" 42 43 if [ -n "$name" ] && [ -n "$udid" ] && [ -n "$runtime" ]; then 44 printf '%s (%s) (%s)\n' "$name" "$runtime" "$udid" 45 fi 46 ;; 47 esac 48 done 49} 50)" 51 52# Count non-empty lines 53count="$(printf '%s\n' "$list" | sed '/^[[:space:]]*$/d' | wc -l | tr -d ' ')" 54if [ "$count" -eq 0 ]; then 55echo "No booted iOS simulators found." >/dev/stderr 56exit 1 57fi 58 59# Pick with fzf if multiple 60if [ "$count" -gt 1 ]; then 61selection="$(printf '%s\n' "$list" | fzf --prompt='Select Simulator: ' --height=10 --reverse || true)" 62[ -z "$selection" ] && exit 130 63else 64selection="$(printf '%s\n' "$list" | head -n1)" 65fi 66 67# UDID is the final (...) group 68udid="$(printf '%s\n' "$selection" | sed -nE 's/.*\(([0-9A-Fa-f-]{36})\)$/\1/p')" 69 70# Resolve container and open 71container="$(xcrun simctl get_app_container "$udid" "$BUNDLE_ID" "$CONTAINER_TYPE" 2>/dev/null || true)" 72if [ -z "$container" ]; then 73echo "App '$BUNDLE_ID' does not have a '$CONTAINER_TYPE' container on: $selection" >/dev/stderr 74exit 2 75fi 76 77open "$container"