0%

applescript UI 操作

  1. UI 操作是通过以下的方式来操控具体的 app 的
1
2
3
4
5
6
7
tell application "Capture One 21"
tell application "System Events"
tell process "Capture One 21"
-- 这里是对具体应用的 UI 模拟操作
end tell
end tell
end tell
  1. 模拟按下键盘

使用键盘按键对应的 key code,示例:

1
2
3
tell application "System Events"
key code 49
end tell

所有 keycode:

keycode.png
  1. 模拟按下组合键

如:shift+command+G

1
key code 5 using {shift dow, command down}
  1. 查看所有可操作的 UI 元素可使用 UI elements,这一点非常有用,在 app 不支持 applescript 的时候,我们只能通过模拟人为去操作 UI 元素,而这个 UI elements 可以让我们具体元素的名称是什么。
1
2
3
4
5
6
7
tell application "Capture One 21"
tell application "System Events"
tell process "Capture One 21"
UI elements
end tell
end tell
end tell

输出:

1
{window "Capture One Catalog" of application process "Capture One 21" of application "System Events", menu bar 1 of application process "Capture One 21" of application "System Events"}

如果我们想再获取 UI 元素里面的子元素,可以使用 of,如:

1
2
3
4
5
6
7
tell application "Capture One 21"
tell application "System Events"
tell process "Capture One 21"
UI elements of window "Capture One Catalog" -- 这里的 window "Capture One Catalog" 是通过 UI elements 获取的元素
end tell
end tell
end tell

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{image 1 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
static text "1/90 s" of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
slider 1 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
button 1 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
static text "ISO 200" of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
static text "Alex_Benes.RAF" of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
UI element 7 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
static text "120" of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
static text "148" of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
static text "125" of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
static text "100" of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
button 2 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
button 3 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
button 4 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
group 1 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
static text "1/6" of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
button 5 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
button 6 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
button 7 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
button 8 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
group 2 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
radio group 1 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
toolbar 1 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
button 9 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
button 10 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
button 11 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
image "Capture One.cocatalogdb" of window "Capture One Catalog" of application process "Capture One 21" of application "System Events",
static text "Capture One Catalog" of window "Capture One Catalog" of application process "Capture One 21" of application "System Events"}

同理,如果我们再想获取下一级的子元素,同样可以使用 of,如:

1
2
3
4
5
6
7
tell application "Capture One 21"
tell application "System Events"
tell process "Capture One 21"
UI elements of group 1 of window "Capture One Catalog"
end tell
end tell
end tell

输出:

1
{scroll area 1 of group 1 of window "Capture One Catalog" of application process "Capture One 21" of application "System Events"}
  1. 我们可以使用 script editor 的 File->Dictionary 来查看某一个 app 具体有哪些命令或者类,这样我们就可以在 applescript 中使用它们,这个可能不太准确。

准确的层级结构可以 script debugger 来查看,在 script debugger 里面输入如下一行即可查看:

1
tell application "Capture One 21" to set appObjectModel to it
  1. 可以使用 xcode 里面的 Accessibility Inspector 来查看大概有哪些 UI 元素

  2. 查看应用里面的属性(在 apple script 里面,一个应用实际上会映射为 apple script 里面的一个对象,就是 application xx 这是一个 apple script 对象)

1
2
3
4
5
6
tell application "Capture One 20" to set a to it

tell application "Capture One 20"
get properties -- 获取 "Capture One 20" 的属性
get properties of viewer of a -- 获取 "Capture One 20" 里面的 viewer 里面的属性
end tell

我们可以通过这种方式获取到一个应用的对象结构。

参考链接:

  1. applescript key code
  2. A Strategy for UI Scripting in AppleScript
  3. UserInterfaceScripting