0%

apple script 范例

基本格式

1
tell application <application name> to <verb> <do something>

可用动词

  • get 用来访问窗口属性

  • set 用来设置窗口属性

  • open 打开窗口

  • close 关闭窗口

  • select 把选中窗口置顶

  1. 获取 Finder 最顶层窗口的标题
1
tell application "Finder" to get the name of the front Finder window
  1. 关闭 Finder 顶层窗口(其他应用类似)
1
tell application "Finder" to close the front window
  1. 打开用户主目录
1
tell application "Finder" to open home
  1. 打开启动盘
1
tell application "Finder" to open the startup disk
  1. 获取 Finder 窗口的索引

当打开了多个 Finder 窗口的时候,可以通过索引来获取对应的 Finder 窗口

1
tell application "Finder" to get index of home

这里假设是 1,下面的命令将会获取用户主目录窗口的标题

1
tell application "Finder" to get the name of window 1
  1. 获取第一个窗口的索引
1
tell application "Finder" to get the index of the first window -- 1

1
tell application "Finder" to get the index of the 1st window
  1. 获取第二个窗口的索引
1
tell application "Finder" to get the index of the second window -- 2

1
tell application "Finder" to get the index of the 2nd window
  1. 根据相对位置获取窗口索引
1
2
3
4
5
6
7
8
9
10
tell application "Finder" to get the front window
--> returns: 1
tell application "Finder" to get the back window
--> returns: 2
tell application "Finder" to get the last window
--> returns: 2
tell application "Finder" to get the index of the Finder window before the last Finder window
--> returns: 1
tell application "Finder" to get the index of the Finder window after the front Finder window
--> returns: 2
  1. 设置窗口索引(改变窗口层叠顺序)
1
tell application "Finder" to set the index of the last window to 1 -- 最底层窗口置顶

获取窗口索引方法

  • 通过窗口名称
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
by name:
Finder window "Documents"
by numeric index:
Finder window 1
by descriptive index:
the first Finder window
the second Finder window
the fifth Finder window
the 1st Finder window
the 23rd Finder window
by relative position index:
the front Finder window
the middle Finder window
the back Finder window
the last Finder window
by random index:
some Finder window
  1. 设置 Finder 顶层窗口到某一个目录

这里是 ~/Code

1
tell application "Finder" to set the target of the front Finder window to folder "Code" of home
1
tell application "Finder" to set the target of the front Finder window to the startup disk

多级目录

1
tell application "Finder" to set the target of the front Finder window to folder "Smith Project" of folder "Documents" of home
  1. 显示、隐藏 Finder 工具栏
1
tell application "Finder" to set toolbar visible of the front window to false -- 隐藏
1
tell application "Finder" to set toolbar visible of the front window to true -- 显示
  1. 显示、隐藏 Finder 状态栏
1
2
tell application "Finder" to set statusbar visible of Finder window 1 to true -- 显示
tell application "Finder" to set statusbar visible of Finder window 1 to false -- 隐藏
  1. 设置侧边栏宽度
1
2
3
tell application "Finder" to set the sidebar width of Finder window 1 to 200 -- 设置顶层窗口
tell application "Finder" to set the sidebar width of the second Finder window to 240 -- 设置第二个窗口
tell application "Finder" to set the sidebar width of every Finder window to 0 -- 隐藏所有 Finder 窗口的侧边栏
  1. Finder view 属性设置
1
2
3
4
5
6
7
8
9
10
tell application "Finder" to get the current view of the front Finder window -- 获取当前 view 属性 (list view, column view, flow view, icon view)

tell application "Finder" to set the current view of the front Finder window to list view

tell application "Finder" to set the current view of the front Finder window to column view

tell application "Finder" to set the current view of the front Finder window to flow view
--> flow view is new in Mac OS X v10.5 (Leopard)

tell application "Finder" to set the current view of the front Finder window to icon view
  1. 获取顶部 Finder 窗口的位置
1
tell application "Finder" to get the position of the front window
  1. 设置 Finder 窗口的位置
1
tell application "Finder" to set the position of the front window to {0, 300}
  1. 获取窗口边界距离屏幕边缘的距离
1
2
tell application "Finder" to get the bounds of the front window
--> returns something like: {72, 90, 512, 481}

各数字意义:

72: 距离屏幕左边边缘的距离

90: 距离屏幕顶部边缘的距离

512: 距离屏幕右边边缘的距离

481: 距离屏幕底部边缘的距离

  1. 置顶窗口
1
tell application "Finder" to select the last Finder window -- 置顶最底部窗口
1
tell application "Finder" to set the index of the last Finder window to 1
  1. tell 语句块

不用每一个语句都写 tell application "xxx"

1
2
3
4
5
6
7
8
9
10
11
12
13
tell application "Finder"
close every window
open home
set toolbar visible of the front Finder window to true
set the sidebar width of the front window to 135
set the current view of the front window to column view
set the bounds of the front Finder window to {36, 116, 511, 674}
open folder "Documents" of home
set toolbar visible of the front Finder window to false
set the current view of the front Finder window to flow view
set the bounds of front Finder window to {528, 116, 1016, 674}
select the last Finder window
end tell
  1. 嵌套 tell 语句块

在对某一个窗口对象操作的时候,可以独立写一个 tell 语句块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
tell application "Finder"
close every window

open home
tell the front Finder window
set toolbar visible to true
set the sidebar width to 135
set the current view to column view
set the bounds to {36, 116, 511, 674}
end tell

open folder "Documents" of home
tell the front Finder window
set toolbar visible to false
set the current view to flow view
set the bounds to {528, 116, 1016, 674}
end tell

select the last Finder window
end tell
  1. 激活窗口
1
2
3
tell application "Wechat"
activate
end tell
  1. 置顶
1
2
3
tell application "Wechat"
set frontmost to true
end tell
  1. 取消最小化
1
2
3
4
5
tell application "Wechat"
try
set miniaturized of windows to false
end try
end tell
  1. 日志输出
1
log "a" -- 可以在 script editor 底部的 events 查看
log.png
  1. 遍历 UI 元素

every group --> Returns a list of groups. every UI element of every group --> Returns a list of lists (corresponding to groups) of UI elements. every button of every UI element of every group --> Returns a list of lists (corresponding to groups) of lists (corresponding to UI elements) of button references.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
tell application "Safari"
activate
end tell

tell application "System Events"
tell process "Safari"
tell window 1
tell toolbar 1
set theButtons to every button of every UI element of every group
repeat with i in theButtons
try
tell (first button whose description is "Translate")
perform action "AXPress"
end tell
end try
end repeat
end tell
end tell
end tell
end tell
  1. 获取当前窗口所有的 UI 元素
1
2
3
4
5
6
7
tell application "System Events"
tell process "Capture One 20"
tell window 1
set uiElems to entire contents
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
tell application "Capture One 20"
activate
end tell

-- 等待 element 这个 UI 元素就绪,超时退出脚本,并提示错误
on ready(element, waitReady)
set t to 5 -- 超时时间
set elapsed to 0
repeat while true
if waitReady then
if element exists then
log elapsed
return
end if
end if

if not waitReady then
if not (element exists) then
log elapsed
return
end if
end if

delay 0.1

set elapsed to elapsed + 0.1
if elapsed > t then
display notification "操作超时" with title "AppleScript错误"
error number -128
end if
end repeat
end ready

tell application "System Events"
tell process "Capture One 20"
-- 遍历查找对应的 window
global win
set win to 0
repeat with w in windows
if exists of radio button 2 of radio group 1 of w then
set win to w
end if
end repeat

if win = 0 then
display notification "无法获取有效窗口" with title "AppleScript错误"
return
end if

-- 点击相机图标
click radio button 2 of radio group 1 of win
delay 0.1

global destination
set destination to 0

-- 遍历检查 group 里面是否有 scroll area 2 -> "NextCaptureLocation" -> "目的地"
repeat with g in every group of win

-- 遍历每一个 scroll area,找到包含 "NextCaptureLocation" 的那一个,最终找到 destination
repeat with sa in every scroll area of g
if exists of group "NextCaptureLocation" of sa then
set nextCaptureLocation to group "NextCaptureLocation" of sa

-- 中文版
if exists of group "目的地" of nextCaptureLocation then
set destination to group "目的地" of nextCaptureLocation
exit repeat -- 退出循环
end if

-- 英文版
if exists of group "Destination" of nextCaptureLocation then
set destination to group "Destination" of nextCaptureLocation
exit repeat -- 退出循环
end if
end if
end repeat

end repeat

-- 判断 destination 是否获取到,没有获取到则提示并退出 applescript
if destination = 0 then
display notification "无法获取目标元素 Destination" with title "AppleScript错误"
return
end if

-- 点击选择文件夹,并设置为自定义的文件夹
click pop up button 1 of destination
my ready(menu item -1 of menu 1 of pop up button 1 of destination, true)
click menu item -1 of menu 1 of pop up button 1 of destination

-- delay 1
my ready(sheet 1 of win, true) -- sheet 1 是 Finder 弹窗
-- Finder 里面 shift + command + G 可以自己手动输入路径
key code 5 using {shift down, command down}

-- 等待弹出输入框后再进行下一步
my ready(text field 1 of sheet 1 of win, true)

-- 清空弹出文本域的内容
key code 0 using {command down} -- command + A
key code 51 -- 51 对应回车键

-- 输入自定义的路径
set the clipboard to "/tmp"
-- keystroke "/tmp"
-- 输入法会影响,所以使用粘贴的方式
key code 9 using {command down} -- command + v

-- 按下回车,等待输入框消失
keystroke return
-- 中文版
if button "前往" of sheet 1 of win exists then
my ready(button "前往" of sheet 1 of win, false)
end if
-- 英文版
if button "Go" of sheet 1 of win exists then
my ready(button "Go" of sheet 1 of win, false)
end if

keystroke return

end tell
end tell