基本格式
1 | tell application <application name> to <verb> <do something> |
可用动词
get
用来访问窗口属性set
用来设置窗口属性open
打开窗口close
关闭窗口select
把选中窗口置顶
- 获取 Finder 最顶层窗口的标题
1 | tell application "Finder" to get the name of the front Finder window |
- 关闭 Finder 顶层窗口(其他应用类似)
1 | tell application "Finder" to close the front window |
- 打开用户主目录
1 | tell application "Finder" to open home |
- 打开启动盘
1 | tell application "Finder" to open the startup disk |
- 获取 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 | 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 | 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 | tell application "Finder" to get the front window |
- 设置窗口索引(改变窗口层叠顺序)
1 | tell application "Finder" to set the index of the last window to 1 -- 最底层窗口置顶 |
获取窗口索引方法
- 通过窗口名称
1 | by name: |
- 设置 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 |
- 显示、隐藏 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 -- 显示 |
- 显示、隐藏 Finder 状态栏
1 | tell application "Finder" to set statusbar visible of Finder window 1 to true -- 显示 |
- 设置侧边栏宽度
1 | tell application "Finder" to set the sidebar width of Finder window 1 to 200 -- 设置顶层窗口 |
- Finder view 属性设置
1 | tell application "Finder" to get the current view of the front Finder window -- 获取当前 view 属性 (list view, column view, flow view, icon view) |
- 获取顶部 Finder 窗口的位置
1 | tell application "Finder" to get the position of the front window |
- 设置 Finder 窗口的位置
1 | tell application "Finder" to set the position of the front window to {0, 300} |
- 获取窗口边界距离屏幕边缘的距离
1 | tell application "Finder" to get the bounds of the front window |
各数字意义:
72: 距离屏幕左边边缘的距离
90: 距离屏幕顶部边缘的距离
512: 距离屏幕右边边缘的距离
481: 距离屏幕底部边缘的距离
- 置顶窗口
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 |
- tell 语句块
不用每一个语句都写 tell application "xxx"
1 | tell application "Finder" |
- 嵌套 tell 语句块
在对某一个窗口对象操作的时候,可以独立写一个 tell 语句块
1 | tell application "Finder" |
- 激活窗口
1 | tell application "Wechat" |
- 置顶
1 | tell application "Wechat" |
- 取消最小化
1 | tell application "Wechat" |