Saturday, August 17, 2013

AppleScript 로 UI Scripting 하는 법 기초

사운드 preference 에서 체크박스를 토글해주는 애플스크립트를 짜보자.

우선 다음과 같이 해서 "Anchor"의 이름들을 알아낼 수 있다.
 tell application "System Preferences"  
      get the name of every anchor of pane id "com.apple.preference.sound"  
 end tell  

Script Editor 아래쪽의 Output 창에는 다음과 같이 찍힌다.
{"output", "input", "effects"}

원하는 체크박스가 "effects" 라는 anchor 밑에 있음을 알았으므로, 다음과 같이
"effects" anchor 가 나타나게 한 후,
체크박스를 클릭하게 만들면 된다.

 tell application "System Preferences"  
      reveal anchor "effects" of pane id "com.apple.preference.sound"  
 end tell  
 tell application "System Events"  
      tell process "System Preferences"  
           click checkbox 1 of tab group 1 of window 1  
      end tell  
 end tell  

응용예) 다음은 이어폰 모드와 스피커 모드를 전환해주는 애플스크립트.
이어폰을 끼기 전에는 볼륨 레벨을 낮추고 F11/F12 키로 갑자기 볼륨이 변화하는걸 막아줌. 스피커 모드를 선택하면 그 반대로 해줌.

 (*  
 on run  
      tell application "System Preferences"  
           get the name of every anchor of pane id "com.apple.preference.keyboard"  
           --> {"keyboardTab", "shortcutsTab", "keyboardTab_ModifierKeys"}  
      end tell  
 end run  
 *)  
 on run  
      set earphoneVolume to 0.1  
      set dialogResult to myMessageBox2ButtonsAndCancel("Change Sound Volume Mode to :", "Earphone", "Speaker")  
      if (dialogResult = "Cancelled") then  
           display dialog dialogResult giving up after 1  
      else  
           -- Enable Assistive Devices via Terminal   
           do shell script ¬  
                "touch /private/var/db/.AccessibilityAPIEnabled" password "q" with administrator privileges  
           if (dialogResult = "Earphone") then  
                set volume earphoneVolume  
                changeMode(1)  
                display dialog ("EARPHONE mode (Volume settings and Function key settings). volume set to " & earphoneVolume) giving up after 1  
           else  
                set volume (earphoneVolume * 2)  
                changeMode(0)  
                display dialog "SPEAKER mode (Volume settings and Function key settings)" giving up after 1  
           end if  
      end if  
 end run  
 on changeMode(earphoneMode)  
      set headphoneMode to 1 - earphoneMode  
      tell application "System Preferences"  
           set current pane to pane "com.apple.preference.keyboard"  
           reveal anchor "keyboardTab" of pane id "com.apple.preference.keyboard"  
      end tell  
      tell application "System Events"  
           if UI elements enabled then  
                tell tab group 1 of window "Keyboard" of process "System Preferences"  
                     if value of checkbox "Use all F1, F2, etc. keys as standard function keys" is headphoneMode then  
                          click checkbox "Use all F1, F2, etc. keys as standard function keys"  
                          --display dialog "clicked checkbox" giving up after 1  
                     end if  
                end tell  
           else  
                tell application "System Preferences"  
                     set current pane ¬  
                          to pane "com.apple.preference.universalaccess"  
                     display dialog ¬  
                          "UI element scripting is not enabled. Check \"Enable access for assistive devices\""  
                end tell  
           end if  
      end tell  
 end changeMode  
 on myMessageBox2ButtonsAndCancel(msg, buttonL, buttonR)  
      set userCanceled to false  
      try  
           set dialogResult to ¬  
                display dialog msg buttons {"Cancel", buttonL, buttonR} ¬  
                     default button buttonR cancel button ¬  
                     "Cancel" with title ""  
      on error number -128  
           set userCanceled to true  
      end try  
      if userCanceled then  
           return "Cancelled"  
      else  
           button returned of dialogResult  
      end if  
 end myMessageBox2ButtonsAndCancel  


키워드 : UI Scripting
레퍼런스 :
Modifying “User interface sound effects” with applescript
Why does this applescript not actually set the input volume to zero?
AppleScript Essentials - User Interface Scripting by Benjamin S. Waldie
Applescript 10.7:Can’t get anchor “FileVault” of pane id “com.apple.preference.security”

레퍼런스 (볼륨 관련):
Set incredibly precise volume levels using AppleScript
http://hints.macworld.com/article.php?story=20100226174946948

Applescript to toggle F1-F11 keys as function keys
http://forums.macrumors.com/showthread.php?t=383969

Enable and disable Assistive Devices via Terminal
http://hints.macworld.com/article.php?story=20060203225241914

No comments:

Post a Comment