tellapplication <applicationname> to <verb> <do something>
可用动词
get 用来访问窗口属性
set 用来设置窗口属性
open 打开窗口
close 关闭窗口
select 把选中窗口置顶
获取 Finder 最顶层窗口的标题
1
tellapplication"Finder"togetthenameofthefront Finder window
关闭 Finder 顶层窗口(其他应用类似)
1
tellapplication"Finder"to close thefront window
打开用户主目录
1
tellapplication"Finder"to open home
打开启动盘
1
tellapplication"Finder"to open the startup disk
获取 Finder 窗口的索引
当打开了多个 Finder 窗口的时候,可以通过索引来获取对应的 Finder
窗口
1
tellapplication"Finder"toget index of home
这里假设是 1,下面的命令将会获取用户主目录窗口的标题
1
tellapplication"Finder"togetthenameof window 1
获取第一个窗口的索引
1
tellapplication"Finder"togetthe index ofthefirst window -- 1
或
1
tellapplication"Finder"togetthe index ofthe1st window
获取第二个窗口的索引
1
tellapplication"Finder"togetthe index ofthesecond window -- 2
或
1
tell application "Finder" to get the index of the 2nd window
根据相对位置获取窗口索引
1 2 3 4 5 6 7 8 9 10
tellapplication"Finder"togetthefront window --> returns: 1 tellapplication"Finder"togettheback window --> returns: 2 tellapplication"Finder"togetthelast window --> returns: 2 tellapplication"Finder"togetthe index ofthe Finder window beforethelast Finder window --> returns: 1 tellapplication"Finder"togetthe index ofthe Finder window afterthefront Finder window --> returns: 2
设置窗口索引(改变窗口层叠顺序)
1
tellapplication"Finder"tosetthe index ofthelast window to1-- 最底层窗口置顶
获取窗口索引方法
通过窗口名称
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
设置 Finder 顶层窗口到某一个目录
这里是 ~/Code
1
tellapplication"Finder"tosetthe target ofthefront Finder window to folder "Code"of home
1
tellapplication"Finder"tosetthe target ofthefront Finder window tothe startup disk
多级目录
1
tellapplication"Finder"tosetthe target ofthefront Finder window to folder "Smith Project"of folder "Documents"of home
tellapplication"Finder"toset statusbar visible of Finder window 1totrue-- 显示 tellapplication"Finder"toset statusbar visible of Finder window 1tofalse-- 隐藏
设置侧边栏宽度
1 2 3
tellapplication"Finder"tosetthe sidebar width of Finder window 1to200-- 设置顶层窗口 tellapplication"Finder"tosetthe sidebar width ofthesecond Finder window to240-- 设置第二个窗口 tellapplication"Finder"tosetthe sidebar width ofevery Finder window to0-- 隐藏所有 Finder 窗口的侧边栏
Finder view 属性设置
1 2 3 4 5 6 7 8 9 10
tellapplication"Finder"togetthe current view ofthefront Finder window -- 获取当前 view 属性 (list view, column view, flow view, icon view)
tellapplication"Finder"tosetthe current view ofthefront Finder window tolist view
tellapplication"Finder"tosetthe current view ofthefront Finder window to column view
tellapplication"Finder"tosetthe current view ofthefront Finder window to flow view --> flow view is new in Mac OS X v10.5 (Leopard)
tellapplication"Finder"tosetthe current view ofthefront Finder window to icon view
获取顶部 Finder 窗口的位置
1
tellapplication"Finder"togetthe position ofthefront window
设置 Finder 窗口的位置
1
tellapplication"Finder"tosetthe position ofthefront window to {0, 300}
tellapplication"Finder"to select thelast Finder window -- 置顶最底部窗口
1
tellapplication"Finder"tosetthe index ofthelast Finder window to1
tell 语句块
不用每一个语句都写 tell application "xxx"
1 2 3 4 5 6 7 8 9 10 11 12 13
tellapplication"Finder" close every window open home set toolbar visible ofthefront Finder window totrue setthe sidebar width ofthefront window to135 setthe current view ofthefront window to column view setthe bounds ofthefront Finder window to {36, 116, 511, 674} open folder "Documents"of home set toolbar visible ofthefront Finder window tofalse setthe current view ofthefront Finder window to flow view setthe bounds offront Finder window to {528, 116, 1016, 674} select thelast Finder window endtell
open home tellthefront Finder window set toolbar visible totrue setthe sidebar width to135 setthe current view to column view setthe bounds to {36, 116, 511, 674} endtell
open folder "Documents"of home tellthefront Finder window set toolbar visible tofalse setthe current view to flow view setthe bounds to {528, 116, 1016, 674} endtell
tellapplication"Wechat" try set miniaturized of windows tofalse endtry endtell
日志输出
1
log"a"-- 可以在 script editor 底部的 events 查看
遍历 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.
-- 等待 element 这个 UI 元素就绪,超时退出脚本,并提示错误 on ready(element, waitReady) set t to5-- 超时时间 set elapsed to0 repeatwhiletrue if waitReady then if element exists then log elapsed return endif endif ifnot waitReady then ifnot (element exists) then log elapsed return endif endif delay0.1 set elapsed to elapsed + 0.1 if elapsed > t then display notification "操作超时"with title "AppleScript错误" errornumber-128 endif endrepeat end ready
tellapplication"System Events" tell process "Capture One 20" -- 遍历查找对应的 window global win set win to0 repeatwith w in windows if exists of radio button 2of radio group 1of w then set win to w endif endrepeat if win = 0then display notification "无法获取有效窗口"with title "AppleScript错误" return endif -- 点击相机图标 click radio button 2of radio group 1of win delay0.1 global destination set destination to0 -- 遍历检查 group 里面是否有 scroll area 2 -> "NextCaptureLocation" -> "目的地" repeatwith g inevery group of win -- 遍历每一个 scroll area,找到包含 "NextCaptureLocation" 的那一个,最终找到 destination repeatwith sa inevery 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 exitrepeat-- 退出循环 endif -- 英文版 if exists of group "Destination"of nextCaptureLocation then set destination to group "Destination"of nextCaptureLocation exitrepeat-- 退出循环 endif endif endrepeat endrepeat -- 判断 destination 是否获取到,没有获取到则提示并退出 applescript if destination = 0then display notification "无法获取目标元素 Destination"with title "AppleScript错误" return endif -- 点击选择文件夹,并设置为自定义的文件夹 click pop up button 1of destination my ready(menu item-1of menu 1of pop up button 1of destination, true) click menu item-1of menu 1of pop up button 1of destination -- delay 1 my ready(sheet 1of win, true) -- sheet 1 是 Finder 弹窗 -- Finder 里面 shift + command + G 可以自己手动输入路径 key code 5 using {shift down, command down} -- 等待弹出输入框后再进行下一步 my ready(text field 1of sheet 1of win, true) -- 清空弹出文本域的内容 key code 0 using {command down} -- command + A key code 51-- 51 对应回车键 -- 输入自定义的路径 setthe clipboardto"/tmp" -- keystroke "/tmp" -- 输入法会影响,所以使用粘贴的方式 key code 9 using {command down} -- command + v -- 按下回车,等待输入框消失 keystroke return -- 中文版 if button "前往"of sheet 1of win exists then my ready(button "前往"of sheet 1of win, false) endif -- 英文版 if button "Go"of sheet 1of win exists then my ready(button "Go"of sheet 1of win, false) endif keystroke return endtell endtell
Host default HostName 127.0.0.1 User vagrant Port 2201
UserKnownHostsFile /dev/null StrictHostKeyChecking no
PasswordAuthentication no IdentityFile
/Users/ruby/Documents/ubuntu14.04/.vagrant/machines/default/virtualbox/private_key
IdentitiesOnly yes LogLevel FATAL
最重要的部分是:
1 2 3 4
HostName 127.0.0.1 User vagrant Port 2201 IdentityFile /Users/ruby/Documents/ubuntu14.04/.vagrant/machines/default/virtualbox/private_key