我正在尝试自动安装Slack。我运行.dmg,然后必须双击“ Slack”图标以开始安装。我猜想在图像上使用'''click''函数并不是真的可行。.我看到该图像调用的URL是file:///Volumes/Slack.app/Slack.app/我是否需要调用在这个链接上也许?我如何获得双击图像的相同结果。
Accessibility Inspector Screenshot
tell application "Finder"
set myFolder to container of (path to me) as alias
end tell
tell application "Finder"
open document file "Slack-4.3.3-macOS.dmg" of myFolder
end tell
tell application "System Events"
tell process "Finder"
click image "Slack" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack"
end tell
end tell
如果您仍然有兴趣通过单击AppleScript中的图像来完成此工作,则可以尝试以下代码:
tell application "System Events"
tell its application process "Finder"
tell image "Slack.app" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack.app"
ignoring application responses
perform action "AXShowMenu"
end ignoring
end tell
end tell
end tell
delay 0.3
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events"
tell its application process "Finder"
tell image "Slack.app" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack.app"
--key code 125 --also can try use arrow key to select item in context menu
keystroke "Open"
delay 0.2
keystroke return
end tell
end tell
end tell
我在MacOS 10.12.6上测试了此代码,它可以正常工作。即使使用终端命令轻松完成此工作,此方法也包含一些非常有用的AS知识点。
第一:执行动作“ AXOpen”和“ AXShowMenu”
第二:显示上下文菜单并选择菜单项
第三:处理系统延迟
如果没有延迟问题,您可以尝试下面的代码,它更简单,(在我的机器上也可以使用:]
tell application "System Events"
tell its application process "Finder"
tell image "Slack.app" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack.app"
perform action "AXShowMenu"
delay 0.3
keystroke "Open"
delay 0.2
keystroke return
end tell
end tell
end tell