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

Bla*_*ilk 12 macos applescript

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

在此输入图像描述

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

在此输入图像描述

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

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
Run Code Online (Sandbox Code Playgroud)

一切都编译好,但是当我尝试运行脚本时,我不断收到以下错误消息:

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

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

Lri*_*Lri 29

这对我有用,但是在第一次单击命令后大约有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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

  • 这工作得很好。顺便说一句,我喜欢你的网站,这似乎是一个感受 applescript 的好地方。我想 +1,但我需要更多代表。 (2认同)
  • 如果您在使用第二个脚本时遇到问题,请在“killall System\\ Events”之前添加“delay 0.1” (2认同)