【发布时间】:2017-10-25 19:25:29
【问题描述】:
我想在按下选项键的情况下单击 Wi-Fi 图标,以显示 Mac 上可用的额外选项。如何使用 AppleScript 自动化它?
我尝试使用key down option 和click menu item,但在揭示特殊选项时没有运气。
有什么方法可以实现吗?
【问题讨论】:
标签: macos applescript osascript
我想在按下选项键的情况下单击 Wi-Fi 图标,以显示 Mac 上可用的额外选项。如何使用 AppleScript 自动化它?
我尝试使用key down option 和click menu item,但在揭示特殊选项时没有运气。
有什么方法可以实现吗?
【问题讨论】:
标签: macos applescript osascript
目前无法使用 AppleScript 按住键进行单击。按键操作仅适用于其他按键操作,因为 AppleScript 点击操作实际上并不执行“点击”,而是直接操作元素。
【讨论】:
如果您不介意使用第 3 方实用程序,这里有一个使用 cliclick 的示例 AppleScript 脚本:
tell application "System Events"
tell application process "SystemUIServer"
set theWiFiProperties to item 1 of (get properties of every menu bar item of menu bar 1 whose description starts with "Wi-Fi")
end tell
set theXpos to (item 1 of position in theWiFiProperties) + ((item 1 of size in theWiFiProperties) / 2) as integer
set theYpos to (item 2 of position in theWiFiProperties) + ((item 2 of size in theWiFiProperties) / 2) as integer
end tell
tell current application
do shell script "/path/to/cliclick kd:alt c:" & theXpos & "," & theYpos & " ku:alt"
end tell
/path/to/cliclick 更改为cliclick 可执行文件的实际pathname。它是如何工作的:
theWiFiProperties 变量 被设置为Wi-Fi 的properties menu extra 然后是变量 theXpos和 theYpos 被设置为 position 一起是 菜单栏上Wi-Fi menu extra 的 center /em>。
这个info然后用在do shell scriptcommand中使用cliclick按下option键,点击指定的x,y 坐标并让 option 键向上。
【讨论】:
您可以使用 Automator 并使用“Watch me do”记录该过程,然后将自动化工作流程保存为应用程序或听写命令。
在 Automator 中,我将 watch me do action 保存为应用程序。我将这个新应用程序命名为“Extended_Wifi.app”。然后我必须在系统偏好设置中添加这个应用程序才能控制我的电脑。
就个人而言,我更喜欢使用 Scripteditor 而不是 Automator,因为我很大一部分人觉得使用 Automator 是在作弊。但在一天结束时,我能够将 Automator 动作保存为应用程序,并且它运行良好,但是在 Scripteditor 中,我无法让 AppleScript 版本的动作正常运行。
这是一个快速 .gif,显示 Automator 应用程序正常工作。
【讨论】: