LoginSignup
3
3

More than 1 year has passed since last update.

AppleScriptでSidecarを開始終了するための、UI要素の日本語名を調べる方法

Last updated at Posted at 2022-01-23

はじめに

この記事を読むとできること

Sidecarを開始終了するためのmacOSのUI要素の日本語名を調べられます → AppleScriptでSidecarの開始終了を自動化できます

この記事を書くにいたった背景

macOS+iPadでSidecarをキー操作で開始・終了するAppleScriptをつくりました。そのときに、操作の対象となるUI要素の名前がわからなくて、困ったのですが、わりといい感じの方法にいたりました。(ほんとうはもっといい方法があるのかもしれません……)

入り口として、下記サイトのConnect Sidecar - Monterey.applescriptを用います。

GitHub - geofftaylor/connect-sidecar

  • 英語版では動くことがわかっていて(UI要素の存在が判明していて)
  • 日本語版での名称がわからない

ので、具体例を示しながら実行するのにちょうどよいとおもいます。

TL;DR - 結論的なこと

  • UI elements of xxxコマンドをつかう(every menu bar item of xxxもOK)
  • set xxxなどのコマンドを書き、スクリプトエディタの返された値タブを見る

詳細

"Control Center"の日本語名を特定する

Connect Sidecar - Monterey.applescriptをスクリプトエディタで開き、実行します(▶ボタンを押します)。エラーが出ます。

error "System Eventsでエラーが起きました: menu bar item \"Control Center\" of menu bar 1 of application process \"ControlCenter\"を取り出すことはできません。" number -1728 from menu bar item "Control Center" of menu bar 1 of application process "ControlCenter"

該当部分のコードはここです。

tell application "System Events"
    tell its application process "ControlCenter"
        -- Click the Control Center menu.
        click menu bar item "Control Center" of menu bar 1
end tell

のうち

click menu bar item "Control Center" of menu bar 1

"Control Center"のところが英語なので通じず、日本語でUI要素名を伝えてあげないといけないようです。

そこでスクリプトエディタに

tell application "System Events"
    tell its application process "ControlCenter"
        UI elements of menu bar 1
        -- 下記でもOK
        -- every menu bar item of menu bar 1
    end tell
end tell

とだけ書いて実行すると、下記の結果が得られます。

結果:
{menu bar item "Clock" of menu bar 1 of application process "ControlCenter" of application "System Events", menu bar item "コントロールセンター" of menu bar 1 of application process "ControlCenter" of application "System Events", menu bar item "Wi‑Fi、接続済み、2本" of menu bar 1 of application process "ControlCenter" of application "System Events", menu bar item "バッテリー" of menu bar 1 of application process "ControlCenter" of application "System Events", menu bar item "ショートカット" of menu bar 1 of application process "ControlCenter" of application "System Events"}

図にすると、こういうことを言っておられます。

英語でControl Centerであったところは、日本語ではコントロールセンターであることがわかりました。日本語に置き換えて、

tell application "System Events"
    tell its application process "ControlCenter"
        -- Click the Control Center menu.
        click menu bar item "コントロールセンター" of menu bar 1
    end tell
end tell

AppleScriptを動かすと、メニューバーのコントロールセンターアイコンをクリックしてくれます。

"Connect to Sidecar"の日本語名を特定する

次は、このボタンを押させたいのです。

英語版のコードは下記ですが、思ったように動きません。

tell application "System Events"
    tell its application process "ControlCenter"
        -- Click the Control Center menu.
        click menu bar item "コントロールセンター" 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 "コントロールセンター")

        if ccCheckboxes contains "Connect to Sidecar" then
            -- If one of the checkboxes is named "Connect to Sidecar," click that checkbox.
            set sidecarToggle to checkbox "Connect to Sidecar" of window "コントロールセンター"
            click sidecarToggle
        end if
    end tell
end tell

"Connect to Sidecar"のところを、日本語で伝えてあげる必要があります。

そこで、スクリプトエディタの返された値タブを見ると、こうなっています。

tell application "System Events"
    click menu bar item "コントロールセンター" of menu bar 1 of application process "ControlCenter"
        --> menu bar item "コントロールセンター" of menu bar 1 of application process "ControlCenter"
    get name of every checkbox of window "コントロールセンター" of application process "ControlCenter"
        --> {"Wi‑Fi", "集中モード", "Bluetooth", "AirDrop", "画面ミラーリング", "Sidecarに接続", "Airplayオーディオ"}
end tell

図にすると、こういうことを言っているようです。

英語でConnect to Sidecarであったところは、日本語ではSidecarに接続であることがわかりました。日本語に書き換えて実行すると、該当ボタンをクリックしてくれます。

つぎの、デバイス名ボタンを押すところは、元のコードのままで動くので調べる必要はありません。

"Disconnect"の日本語名を特定する

こんどは、すでにSidecarに接続されているときに、接続解除するボタン名がほしいです。

該当部分だけ抜き出すと、下記のとおりです。

tell application "System Events"
    tell its application process "ControlCenter"
        -- Click the Control Center menu.
        click menu bar item "コントロールセンター" 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 "コントロールセンター")

        if ccCheckboxes contains "Sidecarに接続" then
        else
            -- Click the checkbox to disconnect Sidecar.
            set sidecarToggle to ((checkbox 1 of window "コントロールセンター") whose name contains "Disconnect")
            click sidecarToggle
        end if

    end tell
end tell

このままでは下記のエラーが起きます。

結果:
error "System Eventsでエラーが起きました: checkbox 1 of window \"コントロールセンター\" of application process \"ControlCenter\" whose name contains \"Disconnect\"を取り出すことはできません。正しくないインデックスです。" number -1719

英語でDisconnectとなっているところの、日本語名を特定しなければなりません。

スクリプトエディタの返された値タブを見ると

tell application "System Events"
    click menu bar item "コントロールセンター" of menu bar 1 of application process "ControlCenter"
        --> menu bar item "コントロールセンター" of menu bar 1 of application process "ControlCenter"
    get name of every checkbox of window "コントロールセンター" of application process "ControlCenter"
        --> {"Wi‑Fi", "集中モード", "Bluetooth", "AirDrop", "画面ミラーリング", "“Nosce te ipsum”を接続解除", "Airplayオーディオ"}
    get checkbox 1 of window "コントロールセンター" of application process "ControlCenter" whose name contains "Disconnect"
        --> error number -1719 from checkbox 1 of window "コントロールセンター" of application process "ControlCenter" whose name contains "Disconnect"

とあります。英語でDisconnectであった部分は接続解除であることがわかりました。

最終的なコード

は、こちらを見て頂ければとおもいます。

Sidecarをキー操作で開始・終了(macOS+iPad) - Qiita

おわりに

macOSの操作を自動化するにあたって、AppleScriptは手軽でよいです。Montereyからつかえるショートカットアプリからでも動かせますし、Alfred Workflowからでも動かせますし、シェルスクリプトでも動かせます。

ところが困ったことに、UI要素名がわからない……。GitHubでよさそうなコードを見つけても、英語なので日本語名がわからないと動かせない……。Xcodeのツールでも調べられた気がしますが、むかしやって何かうまくいかなかった暗い思い出が。

今回、手探りかもしれないけれど、UI要素名を特定していく方法がだいぶわかったので、

参考リンク

GitHub - geofftaylor/connect-sidecar
GitHub - TonyWu20/Sidecar-toggle-alfredworkflow
初めての Apple Script - Qiita

環境

ここで書いていることは、下記のバージョンで実施しました。

  • AppleScript 2.8
  • macOS Monterey 12.1
  • MacBook Pro (13-inch, 2020, Four Thunderbolt 3 Ports)
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3