【笔记】AppleScript学习笔记

前言

AppleScript学习笔记

AppleScript运行环境

  • Script Editor中点击运行按钮
  • Automator中点击运行按钮
  • 将脚本保存为.app文件,作为应用程序运行

注释

1
2
-- 注释内容
# 注释内容

播放提示声

<num>:播放提示声的次数

1
beep <num>

播放语音

<str>:播放的内容

1
say "<str>"

弹出通知

<text>:通知正文
<title>:标题
<subtitle>:副标题

1
display notification "<text>" with title "<title>" subtitle "<subtitle>"

获取软件的全局限定名

<app_name>:软件名

1
id of app "<app_name>"
1
id of app "<app_name>.app"

定义变量

<field_name>:变量名
<class_name>:类名称

1
set <field_name> to <class_name>

定义常量

1
property 常量名 : 常量值

获取剪贴板内容

1
set <field_name> to clipboard

分支语句

  • 判断应用是否正在运行
1
2
3
if application "System Settings" is running then
...
end if

循环语句

<num>:循环执行的次数

1
2
3
repeat <num> times
...
end repeat

弹窗

弹出提示窗口

<msg>:对话框内容
<title>:对话框标题
<ico>:对话框图标

stop:停止图标

buttons {"Yes", "No"}:设置按钮
default button 2:设置第二个按钮为默认的选中状态

1
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

执行Shell命令

<command>:Shell命令

1
do shell script "<command>"

关闭应用

1
tell application "<app_name>" to quit
1
2
3
tell application "<app_name>"
quit
end tell

完成

参考文献

哔哩哔哩——胸毛齐腰
少数派——帕特里柯基
少数派——帕特里柯基
stackoverflow——ppt
Github——deanputney
简书——EAST4021
少数派——异次元de机智君
少数派——bakamio
少数派——未央RS