2013-05-12 44 views
1

這是前一個問題 Applescript: on clicking Menu Bar item via gui script菜單項旁邊的AppleScript反覆複選標記

在下面的f.lux菜單欄的高亮顯示的菜單項目的延伸,如果你點擊它,會出現一個對號,表明「禁用一小時」功能已啓用。我想要做的是爲f.lux編寫一個GUI AppleScript,用戶可以通過在Alfred中鍵入一個關鍵字來決定切換複選標記,後面是1或0,其中1用於啓用「禁用一小時「和0,用來保持不受檢查。

這是菜單欄的f.lux

screen shot of the element inspector providing info on the "Disable for an hour" element and its attributes

但是我有一個很難搞清楚什麼屬性的調整「禁用一小時」菜單項中的屏幕截圖爲了切換複選標記。這裏是代碼,但是當通過applescript編輯器編譯它時,我得到了一個意外的令牌錯誤。到目前爲止,我試圖做的是定位上面截圖中箭頭所示的「菜單項標記字符」屬性,但我不確定這是否是切換「禁用一小時」項目的正確方法。有人可以給我建議嗎?

on alfred_script(q) 
    set myOption to q as integer 

ignoring application responses 
    tell application "System Events" to tell process "Flux" 
     click menu bar item 1 of menu bar 2 
    end tell 
end ignoring 
do shell script "killall System\\ Events" 
delay 0.1 
tell application "System Events" to tell process "Flux" 
    tell menu bar item 1 of menu bar 2 
     if myOption is 1 then 
      set ✓ to value of attribute "AXMenuItemMarkChar" of menu item "Disable for an hour" of menu 1 

     else if myOption is 0 then 
      set nil to value of attribute "AxMenuItemMarkChar" of menu item "Disable for an hour" of menu 1 

     end if 

    end tell 
end tell 

end alfred_script 

回答

5

這似乎工作:

tell application "System Events" to tell process "Flux" 
    tell menu bar item 1 of menu bar 2 
     set v to (value of attribute "AXMenuItemMarkChar" of menu item "Disable for an hour" of menu 1) as string 
     if (v = "" and myOption = 1) or (v is not equal to "" and myOption = 0) then 
      click menu item "Disable for an hour" of menu 1 
     else 
      key code 53 
     end if 
    end tell 
end tell 
+2

這完美地工作,我想給予好評你,但我需要代表。 – 2013-05-12 07:49:43