display dialog "<msg>" with title "<title>" with icon <ico>
弹出警告窗口
<msg>:警告的内容
1
display alert "<msg>"
对应用程序的操作
启动应用程序并保持活跃
<app_name>:应用程序名称,不含后缀,通常情况下首字母是大写的,实际以应用的名称为准
1
tell application "<app_name>" to activate
1 2 3
tell application "<app_name>" activate end tell
获取应用名称
1 2 3
tell application "<app_name>" name end tell
获取最前面窗口名称
1
tell application "<app_name>" to get name of front Window
1 2 3 4 5
tell application "<app_name>" tell front document name end tell end tell
窗口位置
获取指定应用最前面窗口位置
1
tell application "<app_name>" to get bounds of front Finder Window
1
{x轴坐标,y轴坐标,长度,宽度}
修改指定应用最前面窗口的位置
1
tell application "<app_name>" to set bounds of front Finder Window to {x轴坐标,y轴坐标,长度,宽度}
在访达打开Home目录
1
tell application "Finder" to open home
模拟键盘
输出字符串
<str>:字符串内容,不能是中文
1
keystroke "<str>"
根据键码值按下按键
<num>:键码值
1 2 3
tell application "System Events" key code <num> end tell
包含修饰键(快捷键)
<num>:键码值 command:同时按下了command键
1 2 3
tell application "System Events" key code <num> using {command down} end tell
模拟鼠标
模拟鼠标点击指定菜单
实际上鼠标不会移动,也不会点击,但是当前的焦点会转移到被点击的元素上
获取指定应用程序的所有UI元素
1 2 3 4 5
tell application "System Events" tell process "<app_name>" entire contents end tell end tell
通过 Accessibility Inspector 获取任意UI元素
安装Xcode.app
1
open /Applications/Xcode.app/Contents/Applications/Accessibility\ Inspector.app
菜单的点击
通过click关键字实现菜单的点击
每一组of xxx表示了一次tell嵌套tell xxx ... end tell
<element_name>:UI元素名
1 2 3 4 5 6 7 8
-- 先将指定应用作为焦点 tell application "WeChat" activate end tell -- 再点击指定菜单 tell application "System Events" click menu item "加大" of menu "显示" of menu bar item "显示" of menu bar 1 of process "WeChat" end tell
1 2 3 4 5 6 7 8 9 10
-- 先将指定应用作为焦点 tell application "WeChat" activate end tell -- 再点击指定菜单 tell application "System Events" tell process "WeChat" click menu item "默认大小" of menu "显示" of menu bar item "显示" of menu bar 1 end tell end tell
1 2 3 4 5 6 7 8 9 10 11 12
-- 先将指定应用作为焦点 tell application "WeChat" activate end tell -- 再点击指定菜单 tell application "System Events" tell process "WeChat" tell menu bar 1 click menu item "默认大小" of menu "显示" of menu bar item "显示" end tell end tell end tell
1 2 3 4 5 6 7 8 9 10 11 12 13 14
-- 先将指定应用作为焦点 tell application "WeChat" activate end tell -- 再点击指定菜单 tell application "System Events" tell process "WeChat" tell menu bar 1 tell menu bar item "显示" click menu item "默认大小" of menu "显示" end tell end tell end tell end tell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
-- 先将指定应用作为焦点 tell application "WeChat" activate end tell -- 再点击指定菜单 tell application "System Events" tell process "WeChat" tell menu bar 1 tell menu "显示" tell menu bar item "显示" click menu item "默认大小" end tell end tell end tell end tell end tell
窗口中的按钮
点击第一个图标(红色按钮)
1 2 3
tell application "System Events" click button 1 of window "WeChat" of application process "WeChat" end tell
点击第二个图标(绿色按钮)
1 2 3
tell application "System Events" click button 2 of window "WeChat" of application process "WeChat" end tell
点击第三个图标(黄色按钮)
1 2 3
tell application "System Events" click button 3 of window "WeChat" of application process "WeChat" end tell
菜单栏上的系统图标
点击第一个图标(系统通知)
1 2 3
tell application "System Events" click menu bar item 1 of menu bar 1 of application process "ControlCenter" of application "System Events" end tell
点击第二个图标(控制中心)
1 2 3
tell application "System Events" click menu bar item 2 of menu bar 1 of application process "ControlCenter" of application "System Events" end tell
点击第三个图标
从第三个图标开始为用户开启的显示在菜单栏的系统图标
1 2 3
tell application "System Events" click menu bar item 3 of menu bar 1 of application process "ControlCenter" of application "System Events" end tell
延迟
<num>:延迟的时间,单位秒
1
delay <num>
窗口出现之前一直等待
1 2 3 4 5 6 7 8
tell application "System Events" tell process "<app_name>" repeat until window 1 exists -- 在窗口没出现时反复执行空循环 end repeat -- 在窗口出现后向下执行 end tell end tell