System Events 是系统应用(application),要「告诉」它做某事,AppleScript 要这么写,
tell application "System Events"
-- 你希望应用 System Events 做的事
end tell
所有带 UI 结构的应用,都是 System Events 下的进程(process),包括 Money Pro。如果我要「告诉」Money Pro 做某事,因为它是套在 System Events 之内的,就要这么写
tell application "System Events"
tell process "Money Pro"
-- 你希望进程 Money Pro 做的事
end tell
end tell
再进一步,我们要控制 Money Pro 的第一个窗口,就是
tell application "System Events"
tell process "Money Pro"
tell window 1
-- 你希望 window 1 做的事
end tell
end tell
end tell
以此类推,你可以无限
tell
下去,
tell application "System Events"
tell process "Money Pro"
tell window 1
tell something
tell something
tell something
-- System Events: 你他喵的够了
end tell
end tell
end tell
end tell
end tell
end tell
图中被选中的这个「软件」菜单项的完整描述是「Money Pro 应用中的第一个窗口中的第一个可滚动区域中的第一个表单中的第二列中的第一个 UI 元素中的第一个弹出菜单按钮的第一个弹出菜单中的菜单项“买买买”的第一个菜单中的“软件”菜单项」,在 AppleScript 中就是
menu item "软件" of menu 1 of menu item "买买买" of menu 1 of pop up button 1 of UI element 1 of row 2 of table 1 of scroll area 1 of window 1 of process "Money Pro" of application "System Events"
tell application "System Events"
tell process "Money Pro"
click menu item "软件" of menu 1 of menu item "买买买" of menu 1 of pop up button 1 of UI element 1 of row 2 of table 1 of scroll area 1 of window 1 -- of process "Money Pro" of application "System Events"
end tell
end tell
因为我们处在进程 Money Pro 的 tell 中,所以需要注释掉后面 of process "Money Pro" of application "System Events" 的部分。
2. 如果是文本输入框,你可以设置文本框内容。
set value of text field 1 of ... to "一些文本内容"
也可以设置激活该输入框的光标,
set value of attribute "AXFocused" of text field 1 of ... to true
#激活 Money Pro
tell application "Money Pro" to activate
tell application "System Events"
tell process "Money Pro"
#点击 + 号按钮
click button 2 of group 1 of group 1 of group 1 of window 1
#暂停 0.3 秒,等待新建面板出现
delay 0.3
#点击「类别」按钮
click pop up button 1 of UI element 1 of row 2 of table 1 of scroll area 1 of window 1
#选中「买买买」菜单项click menu item "买买买" of menu 1 of pop up button 1 of UI element 1 of row 2 of table 1 of scroll area 1 of window 1
#点击「软件」菜单项
click menu item "软件" of menu 1 of menu item "买买买" of menu 1 of pop up button 1 of UI element 1 of row 2 of table 1 of scroll area 1 of window 1
end tell
end tell
值得一提的小事
如何使用 UI 元素的描述语句,取决于所处层级
你可能已经注意到了,只要处于某个层级内部,对一个 UI 元素进行操作使用的并不是它的完整描述。
还是拿点击那个「软件」菜单项为例,我们是这样写的:
tell application "System Events"
tell process "Money Pro"
click menu item "软件" of menu 1 of menu item "买买买" of menu 1 of pop up button 1 of UI element 1 of row 2 of table 1 of scroll area 1 of window 1 -- of process "Money Pro" of application "System Events"
end tell
end tell
但你也可以这样写:
tell application "System Events"
tell process "Money Pro"
tell window 1
click menu item "软件" of menu 1 of menu item "买买买" of menu 1 of pop up button 1 of UI element 1 of row 2 of table 1 of scroll area 1 --of window 1 of process "Money Pro" of application "System Events" end tell
end tell
end tell
tell application "System Events"
tell process "Money Pro"
tell window 1
tell scroll area 1
tell table 1
tell row 2
tell UI element 1
tell pop up button 1
tell menu 1
tell menu item "买买买"
tell menu 1
tell menu item "软件"
click
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
ignoring application responses
-- 这里是你的第一次点击操作
click button 1
end ignoring
然后在 System Events 的 tell 语句外面杀掉 System Events 进程:
delay 0.1
do shell script "killall System\\ Events"
然后正常进行第二次点击操作。
完整语句是这样:
tell application "System Events"
tell process "Reeder"
ignoring application responses --忽略应用的反馈
click button 1 of window "Day One 2"
end ignoring
end tell
end tell
-- 杀掉 System Events 应用
delay 0.1 --自定义 UI 反馈的等待时间为0.1 秒
do shell script "killall System\\ Events"
tell application "System Events"
tell process "Reeder"
-- 第二次点击操作
end tell
end tell