Applescript:通过gui脚本单击菜单栏项

时间:2013-05-11 01:20:21

标签: macos applescript

我正在尝试为名为F.lux的应用程序制作一个应用程序,点击菜单项“禁用一小时”,如下面的屏幕截图所示:

enter image description here

元素路径如下面的屏幕截图所示:

enter image description here

到目前为止,这是我的代码:

tell application "System Events"
    tell process "Flux"
        click (menu bar item 1 of menu bar 2)
        click menu item "Disable for an hour" of menu 1 of menu bar item 1 of        
        menu bar 2
    end tell    
end tell

所有内容编译都很好,但是当我尝试运行脚本时,我不断收到以下错误消息:

  
    

错误“系统事件出错:无法获取进程\”Flux \“菜单栏2的菜单栏项目1的菜单1。索引无效。”从流程“Flux”的菜单栏2的菜单栏项目1的菜单1开始编号-1719

  

有人可以确定我在哪里出错吗?

1 个答案:

答案 0 :(得分:28)

这对我有用,但是在第一次点击命令后大约有5秒的延迟。

tell application "System Events" to tell process "Flux"
    tell menu bar item 1 of menu bar 2
        click
        click menu item "Disable for an hour" of menu 1
    end tell
end tell

一种解决方法是使用ignoring application responses并在单击命令后终止系统事件:

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
        click menu item "Disable for an hour" of menu 1
    end tell
end tell