Skip to content

Instantly share code, notes, and snippets.

@swawa-yu
Created November 13, 2024 02:39
Show Gist options
  • Select an option

  • Save swawa-yu/aa099914d2c3c7d42ee7f1f4e9888373 to your computer and use it in GitHub Desktop.

Select an option

Save swawa-yu/aa099914d2c3c7d42ee7f1f4e9888373 to your computer and use it in GitHub Desktop.
MacBook本体の画面が故障したときのためのスクリプト
#!/bin/bash
# displayplacerの出力を取得
list_output=$(displayplacer list)
# 外部ディスプレイが接続されているか確認
if echo "$list_output" | grep -q "Type:.*external screen"; then
echo "外部ディスプレイが接続されています。"
else
echo "外部ディスプレイが接続されていません。"
exit 1
fi
# 出力を行ごとに配列に格納
IFS=$'\n' read -d '' -r -a lines <<< "$list_output"
# 変数の初期化
screen_index=-1
collecting_modes=false
declare -a screen_ids
declare -a screen_types
declare -a highest_resolutions
declare -a highest_res_widths
declare -a highest_res_heights
declare -a highest_hzs
declare -a highest_color_depths
declare -a highest_scalings
for line in "${lines[@]}"; do
# 行の前後の空白を削除
line="${line#"${line%%[![:space:]]*}"}"
line="${line%"${line##*[![:space:]]}"}"
if [[ $line == "Persistent screen id:"* ]]; then
# 新しいスクリーンの開始
((screen_index++))
current_screen_id="${line#Persistent screen id: }"
screen_ids[$screen_index]=$current_screen_id
collecting_modes=false
highest_resolutions[$screen_index]=0
elif [[ $line == "Type:"* ]]; then
current_screen_type="${line#Type: }"
screen_types[$screen_index]=$current_screen_type
elif [[ $line == "Resolutions for rotation 0:" ]]; then
collecting_modes=true
elif [[ $collecting_modes == true && $line == mode* ]]; then
# モードの解析
mode_line="${line%%<--*}"
res=$(echo "$mode_line" | grep -o 'res:[^ ]*' | cut -d':' -f2)
hz=$(echo "$mode_line" | grep -o 'hz:[^ ]*' | cut -d':' -f2)
color_depth=$(echo "$mode_line" | grep -o 'color_depth:[^ ]*' | cut -d':' -f2)
scaling=$(echo "$mode_line" | grep -o 'scaling:[^ ]*' | cut -d':' -f2)
if [ -z "$scaling" ]; then
scaling="off"
fi
width=$(echo "$res" | cut -d'x' -f1)
height=$(echo "$res" | cut -d'x' -f2)
pixels=$((width * height))
if (( pixels > highest_resolutions[$screen_index] )); then
highest_resolutions[$screen_index]=$pixels
highest_res_widths[$screen_index]=$width
highest_res_heights[$screen_index]=$height
highest_hzs[$screen_index]=$hz
highest_color_depths[$screen_index]=$color_depth
highest_scalings[$screen_index]=$scaling
fi
elif [[ $line == "" ]]; then
collecting_modes=false
fi
done
num_screens=${#screen_ids[@]}
# 外部ディスプレイと内蔵ディスプレイのインデックスを取得
external_screen_index=-1
builtin_screen_index=-1
for ((i=0; i<num_screens; i++)); do
screen_type=${screen_types[$i]}
if [[ $screen_type == *external* ]]; then
external_screen_index=$i
elif [[ $screen_type == *"MacBook built in screen"* ]]; then
builtin_screen_index=$i
fi
done
if [ $external_screen_index -eq -1 ]; then
echo "外部ディスプレイが見つかりません。"
exit 1
fi
if [ $builtin_screen_index -eq -1 ]; then
echo "内蔵ディスプレイが見つかりません。"
exit 1
fi
external_screen_id=${screen_ids[$external_screen_index]}
builtin_screen_id=${screen_ids[$builtin_screen_index]}
width=${highest_res_widths[$external_screen_index]}
height=${highest_res_heights[$external_screen_index]}
hz=${highest_hzs[$external_screen_index]}
color_depth=${highest_color_depths[$external_screen_index]}
scaling=${highest_scalings[$external_screen_index]}
# displayplacerコマンドを構築
displayplacer_cmd="displayplacer \"id:${external_screen_id}+${builtin_screen_id} res:${width}x${height} hz:${hz} color_depth:${color_depth} scaling:${scaling} origin:(0,0) degree:0\""
echo "次のコマンドを実行します:"
echo $displayplacer_cmd
# コマンドを実行
eval $displayplacer_cmd
@swawa-yu
Copy link
Author

概要

MacBookの画面が壊れて画面の状態がわからなくなってしまった時に、外部ディスプレイを接続して操作できるようにします。
外部ディスプレイ接続時に"拡張モード"になると不便なので、このスクリプトでは次の設定をします。

  • 外部ディスプレイ: 主ディスプレイ
  • 内部ディスプレイ: 主ディスプレイをミラーリング

Raycastに登録しておくと簡単に呼び出せてよいです。

環境

displayplacerを使用します。brew install displayplacerしておいてください。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment