Thu, 19 Feb 2009

メモ:AppleScriptでGUIを操作する

ふだん、iMacで音楽を聴くときには内蔵スピーカーを使うことが多いのですが、 ときどき音量の関係やSkypeなんかでヘッドセットで音を聞くこともあります。 出力先はシステム環境設定から変えられるのですが、いちいちクリックするのが面倒なので、AppleScriptでショートカットするためのスクリプトを書いてみました。 初AppleScriptです。環境はLeopard 10.5.6です。

AppleScriptでGUIを操作するためには、システム環境設定のユニバーサルアクセスから、「補助装置にアクセスできるようにする」にチェックをいれておく必要があります。

次に、スクリプトですが、まぁ、さらすのが恥ずかしいぐらいしょぼいです。

tell application "System Preferences"
activate
Set current pane to pane id "com.apple.preference.sound"
end tell

tell application "System Events"
     tell process "System Preferences"
         set awin to tab group 1 of window "サウンド"
         tell awin
             click radio button "出力" of awin
                 tell scroll area 1 of awin
                      tell table 1 of scroll area 1 of awin
                          set t1 to table 1 of scroll area 1 of awin
                          if row 1 is selected then
                              select row 2 of t1
                          else
                              select row 1 of t1
                          end if
                       end tell
                  end tell
           end tell
    end tell
end tell

tell application "System Preferences"
quit
end tell

出力をスイッチする部分はめちゃくちゃ適当です。内蔵スピーカー(スクロールエリアの1行目)が選択されていれば2行目を選択するたけ。 出力装置が増えたときのことなど何も考えていません。

むしろGUIを操作するときには、何が大変かというと、クリックや選択などの操作を記述することだとおもいます。 今回は、たぶん一番基本的であろうと思われる、次の流れをとりました。

操作するアプリケーションの構造を調べる

今回の場合、調べる対象は「システム環境設定.app」になります。 調べるにはScriptEditorの用語説明を使います(ファイル > 用語説明を開く)。 これで、対象にするアプリケーションを選べばクラス構造などが分かります。 実際に、ScriptEditor上で、

tell application "System Preferences"
     get pane of it
end tell

と書いて実行すれば、次のように、ずらずらっと実際のクラス名が取得できます。(本当は1行)

{pane id "com.apple.preferences.Bluetooth" of application "System Preferences", 
 pane id "com.apple.preference.digihub.discs" of application "System Preferences", 
 pane id "com.apple.preference.dock" of application "System Preferences", 
 pane id "com.apple.preference.expose" of application "System Preferences", 
 pane id "net.telestream.wmv.prefpane" of application "System Preferences",
 ...
 }

そこで、先のスクリプトの最初の部分のように

set current pane to pane id "com.apple.preference.sound"

とやれば、サウンド設定画面に移動します。

GUIを操作する

お約束ですが、GUIを操作する場合は、

tell application "System Events"
     tell process "HOGE.app"
     <処理内容>
     end tell
end tell

と書きます。上にかいた「補助装置にアクセスできるようにする」を有効にしていないと"System Events"が効きません。 あとは、他のGUIプログラミングでよくあるように、オブジェクト階層を辿っていって、対象のところで、set とかすればいいわけです。

問題はどうやって辿るか、地味にScriptEditorで

tell application "System Events"
     tell process "System Preferences"
          get every UI element
                tell window "サウンド"
                     every UI element
                end tell
      end tell
end tell

というように書いて実行し、階層を調べていくのが、今回のような簡単なスクリプトには(ださいですが)一番いいみたいです。 上の例の場合、ScriptEditorのイベントログに、次のように出力されます。

tell application "System Events"
     get every UI element of process "System Preferences"
         {window "サウンド" of application process "System Preferences", menu bar 1 of application process "System Preferences"}
            get every UI element of window "サウンド" of process "System Preferences"
                {button 1 of window "サウンド" of application process "System Preferences", button 2 of window "サウンド" of application process "System Preferences", button 3 of window "サウンド" of application process "System Preferences", tab group 1 of window "サウンド" of application process "System Preferences", group 1 of window "サウンド" of application process "System Preferences", checkbox "メニューバーに音量を表示" of window "サウンド" of application process "System Preferences", tool bar 1 of window "サウンド" of application process "System Preferences", static text "サウンド" of window "サウンド" of application process "System Preferences"}
end tell

そのうち勘も働くようになるでしょうけど。

ちなみにAppleのサイトには、UI Element Inspectorというユーティリティソフトが紹介されていますが、 私の環境では嘘つきまくりなやつでした。あまりつかえませんでした。

ショートカットを設定する

スクリプトをApplicationに保存したら、あとは、QuickSilverの偉大な力を借りればいいだけです。

あー、らくちん。

posted at: Thu, 19 Feb 2009 22:47 [mac] permanent link/comments(2)

Comments

Posted by ノダ虫 at Wed Oct 7 00:31:30 2009
参考にさせて頂きました。
ありがとうございました。
Posted by A at Wed Jul 14 20:06:53 2010
まったく同じことがしたかったので参考になりました。
GUIが表示されなければ最高です。

お名前:


Secret Number Image

(スパムコメントよけです。上の数字を入力してからコメントしてください。)

コメント:


Profile

Calendar

< February 2009 >
SuMoTuWeThFrSa
1 2 3 4 5 6 7
8 91011121314
15161718192021
22232425262728

Twitter

Photo

www.flickr.com

Awasete

Recent Track

Web Feed

This blog is...

pyblosxom.log