Applescript:通过 gui 脚本单击菜单栏项 [英] Applescript: on clicking Menu Bar item via gui script

查看:52
本文介绍了Applescript:通过 gui 脚本单击菜单栏项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

元素路径如下图所示:

这是我目前的代码:

告诉应用程序系统事件"告诉进程通量"单击(菜单栏 2 的菜单栏项目 1)单击菜单栏项目 1 的菜单 1 的菜单项禁用一小时"菜单栏 2结束告诉结束告诉

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

<块引用><块引用>

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

有人能指出我哪里出错了吗?

解决方案

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

告诉应用程序系统事件"告诉进程Flux"告诉菜单栏 2 的菜单栏项目 1点击单击菜单项禁用一小时"菜单 1结束告诉结束告诉

一种解决方法是使用忽略应用程序响应并在单击命令后终止系统事件:

忽略应用程序响应告诉应用程序系统事件"告诉进程Flux"单击菜单栏 2 的菜单栏项目 1结束告诉结束无视执行 shell 脚本killall System\\ Events"延迟 0.1告诉应用程序系统事件"告诉进程Flux"告诉菜单栏 2 的菜单栏项目 1单击菜单项禁用一小时"菜单 1结束告诉结束告诉

I'm trying to make an applescript for an application called F.lux that clicks the menu item "Disable for an Hour" as indicated in the screenshot below:

The element path is indicated in the screenshot below:

Here is my code thus far:

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

Everything compiles fine, however I keep getting the error message below when I attempt to run the script:

error "System Events got an error: Can’t get menu 1 of menu bar item 1 of menu bar 2 of process \"Flux\". Invalid index." number -1719 from menu 1 of menu bar item 1 of menu bar 2 of process "Flux"

Can someone pinpoint where I'm going wrong with this?

解决方案

This worked for me, but there is a delay of about 5 seconds after the first click command.

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

One workaround is to use ignoring application responses and terminate System Events after the click command:

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

这篇关于Applescript:通过 gui 脚本单击菜单栏项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆