はじめに
この記事を読むとできるようになること
Sidecarを、キー操作で開始・終了できるようになります。
Sidecarとは、iPadをMacのMacの2台目のディスプレイとして使う機能です。
参考:[iPadをMacのMacの2台目のディスプレイとして使う - Appleサポート] (https://support.apple.com/ja-jp/HT210380)

背景
Sidecarは便利なのですけれども、意外とつかいづらくて、つい使わなくなってしまいます。久しぶりにつかうと、やっぱりとても便利です。
じゃあどうしてつかわなくなるのか?というと
- バッテリを消費する
- 切れる(いつのまにか画面が固まっている)ことがけっこうある
- 接続・接続解除の場所がわかりにくいし、マウス操作がめんどうくさい
2の「いつのまにか固まっている」については、有線接続を試していて、まあまあいい感じに見えます。
3の「操作がめんどうくさい」について、スクリプトを書いてラクができるようになったので、この記事でご紹介します。
動きの概要
command + space
でAlfredを起動し、sc
とタイプすると、SidecarがONとなり、iPadが2台めのディスプレイになります。SidecarがすでにONの場合は、OFFに切り替えます。

詳細
AppleScript
元のコードは下記にあります。
そのままでは日本語版で動かないので、動くようにしました。スクリプトエディタに貼り付けて、実行
すれば動きます。
set controlCenterName to "コントロールセンター"
set deviceName to "My iPad" -- Change this to the name of your iPad.
set connectToSidecarName to "Sidecarに接続"
set disconnectSidecarName to "接続解除"
tell application "System Events"
tell its application process "ControlCenter"
-- Click the Control Center menu.
click menu bar item controlCenterName of menu bar 1
-- Give the window time to draw.
delay 1
-- Get all of the checkboxes in the Control Center menu.
set ccCheckboxes to name of (every checkbox of window controlCenterName)
if ccCheckboxes contains connectToSidecarName then
-- If one of the checkboxes is named "Connect to Sidecar," click that checkbox.
set sidecarToggle to checkbox connectToSidecarName of window controlCenterName
click sidecarToggle
-- This opens a secondary window that contains the button to actually connect to Sidecar. Give the window time to draw.
delay 1
-- In masOS Monterey, the Sidecar device toggle (checkbox) is inside of a scroll area.
-- Rather than assume that it's in scroll area 1, get all of the scroll areas, loop through them, and find the device toggle.
set scrollAreas to (every scroll area of window controlCenterName)
set saCounter to 1
set displayCheckboxes to ""
repeat with sa in scrollAreas
set displayCheckboxes to name of (every checkbox of sa)
if displayCheckboxes contains deviceName then
-- Device toggle found.
exit repeat
end if
-- We didn't find the device toggle. Try the next scroll area.
set saCounter to saCounter + 1
end repeat
if displayCheckboxes contains deviceName then
-- If we found the a checkbox with the iPad's name, `saCounter` tells us which scroll area contains the Sidecar toggle.
set deviceToggle to checkbox deviceName of scroll area saCounter of window controlCenterName
-- Click the toggle to connect Sidecar.
click deviceToggle
-- Click the Control Center menu to close the secondary menu and return to the main menu.
click menu bar item controlCenterName of menu bar 1
-- Click the Control Center menu again to close the main menu.
click menu bar item controlCenterName of menu bar 1
else
-- Sidecar is available, but no devices with deviceName were found.
display dialog "The device " & deviceName & " can't be found. Please verify the name of your iPad and update the `deviceName` variable if necessary."
end if
else
-- A checkbox named "Connect to Sidecar" wasn't found.
set isConnected to false
repeat with cb in ccCheckboxes
-- Loop through the checkboxes and determine if Sidecar is already connected.
if cb contains disconnectSidecarName then
-- If one of the checkboxes has "Disconnect" in its name, Sidecar is already connected.
-- Break out of the loop.
set isConnected to true
exit repeat
end if
end repeat
if isConnected is equal to true then
-- Click the checkbox to disconnect Sidecar.
set sidecarToggle to ((checkbox 1 of window controlCenterName) whose name contains disconnectSidecarName)
click sidecarToggle
-- Click the Control Center menu again to close the main menu.
click menu bar item controlCenterName of menu bar 1
else
-- Sidecar isn't connected, and no devices are available to connect to. Show an error message.
display dialog "No Sidecar devices are in range."
end if
end if
end tell
end tell
UI要素名の日本語版を拾うのに手こずりましたが、方法はわかってきました。長くなりましたので、別記事にしました。
AppleScriptで操作するために、UI要素の日本語名を調べる方法 - Qiita
Alfred Workflow
参考にさせていただいたAlfred Workflowは下記にあります。
GitHub - TonyWu20/Sidecar-toggle-alfredworkflow: An alfred workflow to toggle Sidecar in Big Sur
このAppleScript部分を差し替えました。
Keyword
モジュールとRun Script
モジュールを連結
Keyword
モジュールでは、sc
とタイプすれば起動するよう設定
Run Script
モジュールに、上記AppleScriptを貼り付け
Alfred Workflow以外の選択肢
上記のAppleScriptが起動できればいいので、Alfredを使う必要はありません。もとの作者はScript Menu
、Keyboard Maestro
、FastScripts
をあげています。macOS Montereyからつかえるようになった、ショートカットアプリもいけるとおもいます。
おわりに
Sidecarの接続が安定しなかったり、SidecarのON/OFF切り替えが深いところにあって操作がめんどうなのは、Appleのバグあるいは仕様なのですけれども、待っていても変わりそうじゃないのでスクリプトを書きました。
あらためて、Sidecar(セカンドディスプレイ)あると、とても便利です。
参考リンク
下記のコードに大いに頼りました。
GitHub - TonyWu20/Sidecar-toggle-alfredworkflow: An alfred workflow to toggle Sidecar in Big Sur
環境
ここで書いていることは、下記のバージョンで実施しました。
- Alfred 4.6.1
- AppleScript 2.8
- iPadOS 15.2.1
- macOS Monterey 12.1