Skip to content

Instantly share code, notes, and snippets.

@imaizume
Last active August 11, 2025 03:24
Show Gist options
  • Select an option

  • Save imaizume/02825bbfed83ff3eae9c96f672f75dd1 to your computer and use it in GitHub Desktop.

Select an option

Save imaizume/02825bbfed83ff3eae9c96f672f75dd1 to your computer and use it in GitHub Desktop.
起動・接続しているiOS/AndroidデバイスでURLを開くZSH関数
#!/usr/bin/env zsh
# URL履歴ファイル
URL_HISTORY_FILE="$HOME/.url_history"
URL_HISTORY_MAX=100
# URL履歴にURLを追加
add_to_history() {
local url=$1
# 履歴ファイルが存在しない場合は作成
[[ ! -f "$URL_HISTORY_FILE" ]] && touch "$URL_HISTORY_FILE"
# 重複を除去(既存のURLを削除してから先頭に追加)
grep -v "^$url$" "$URL_HISTORY_FILE" > "${URL_HISTORY_FILE}.tmp" 2>/dev/null || true
echo "$url" | cat - "${URL_HISTORY_FILE}.tmp" > "$URL_HISTORY_FILE"
rm -f "${URL_HISTORY_FILE}.tmp"
# 履歴の上限を適用
if [[ $(wc -l < "$URL_HISTORY_FILE") -gt $URL_HISTORY_MAX ]]; then
head -n $URL_HISTORY_MAX "$URL_HISTORY_FILE" > "${URL_HISTORY_FILE}.tmp"
mv "${URL_HISTORY_FILE}.tmp" "$URL_HISTORY_FILE"
fi
}
# Androidデバイスでのみ開く
_open_url_android() {
local url=$1
if ! command -v adb >/dev/null 2>&1; then
echo "adb command not found"
return 1
fi
if ! adb devices 2>/dev/null | grep -q "device$"; then
echo "No Android device found"
return 1
fi
echo "Opening on Android device: $url"
adb shell am start -a android.intent.action.VIEW -d "$url" >/dev/null 2>&1
return $?
}
# iOSシミュレーターでのみ開く
_open_url_ios() {
local url=$1
if ! command -v xcrun >/dev/null 2>&1; then
echo "xcrun command not found"
return 1
fi
if ! xcrun simctl list devices | grep -q "Booted"; then
echo "No iOS simulator found"
return 1
fi
echo "Opening on iOS simulator: $url"
xcrun simctl openurl booted "$url" >/dev/null 2>&1
return $?
}
# デバイス/シミュレーターでURLを開く(自動選択)
_open_url_on_device() {
local url=$1
local device_type=${2:-"auto"}
local opened=false
case $device_type in
"android")
_open_url_android "$url"
return $?
;;
"ios")
_open_url_ios "$url"
return $?
;;
"auto")
# Androidデバイス/エミュレーターをチェック
if command -v adb >/dev/null 2>&1; then
if adb devices 2>/dev/null | grep -q "device$"; then
_open_url_android "$url"
opened=true
fi
fi
# iOSシミュレーターをチェック
if command -v xcrun >/dev/null 2>&1; then
if xcrun simctl list devices | grep -q "Booted"; then
_open_url_ios "$url"
opened=true
fi
fi
if [[ "$opened" = false ]]; then
echo "No Android device or iOS simulator found"
return 1
fi
return 0
;;
*)
echo "Invalid device type: $device_type"
return 1
;;
esac
}
# メイン関数
local device_type="auto"
open_url() {
local url=""
# オプション解析
while [[ $# -gt 0 ]]; do
case $1 in
-a|--android)
device_type="android"
shift
;;
-i|--ios)
device_type="ios"
shift
;;
-h|--help)
echo "Usage: open_url [-a|--android] [-i|--ios] [URL]"
echo " -a, --android Open only on Android device"
echo " -i, --ios Open only on iOS simulator"
echo " -h, --help Show this help message"
echo " Without URL, select from history using fzf"
return 0
;;
-*)
echo "Unknown option: $1" >&2
return 1
;;
*)
url="$1"
shift
;;
esac
done
# URLが引数で指定された場合
if [[ -n "$url" ]]; then
if _open_url_on_device "$url" "$device_type"; then
add_to_history "$url"
fi
return $?
fi
# 履歴ファイルが存在しない場合
if [[ ! -f "$URL_HISTORY_FILE" || ! -s "$URL_HISTORY_FILE" ]]; then
echo "No URL history found. Usage: open_url [-a|-i] <URL>"
return 1
fi
# fzfを使用してURLを選択
if ! command -v fzf >/dev/null 2>&1; then
echo "fzf is not installed. Please install fzf for URL selection."
return 1
fi
local selected_url
selected_url=$(cat "$URL_HISTORY_FILE" | fzf --prompt="URL> " --height=40% --reverse)
if [[ -n "$selected_url" ]]; then
_open_url_on_device "$selected_url" "$device_type"
fi
}
alias ou="open_url"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment