AppleScript 快速入门
admin
2023-07-27 10:00:05
0次

AppleScript 快速入门

AppleScript 顾名思义是苹果开发的一套脚本语言,利用 AppleScript 在 macOS 系统上可以对其他程序进行操作,点击按钮、发送消息、模拟自动化执行功能,比如可以打开浏览器,清空回收站等等一些操作,是一个非常有意思的脚本。说好了要快速入门,下面我们开始快速学习了解它吧。

一、让其他程序执行任务

在 macOS 上有一个应用叫脚本编辑器,通过 Launchpad 可以搜索到,打开脚本编辑器之后,可以看到支持编写和解析 AppleScript 和 JavaScript 两种脚本,如下图所示:

AppleScript 快速入门

AppleScript 的语法和平时的英语语法很类似,你想让哪个程序执行操作,就 tell 它,比如你想让 Finder 清空回收站那就这样写:

tell application "Finder"
    empty the trash
end tell

在脚本编辑器上点击运行按钮就可以看到回收站的内容被清空了,或者按快捷键 Command + R 也能运行,运行之前记得回收站得有东西,不然可能会执行失败。

如果你想让系统说话,可以这样写:

tell application "Finder"
    say "My name is exchen"
end tell

哈哈,记得把电脑的声音打开,是不是听到说话了?不仅支持英文和中文,其他国家语言,像德语、荷兰语笔者试过,同样也可以。

如果你想让浏览器打开 URL,可以这样写:

set myBlog to "http://www.exchen.net"

# 告诉 Chrmoe 浏览器打开 URL
tell application "Google Chrome"
    # 新建一个 chrome 窗口
    set window1 to make new window
    tell window1
        set currTab to active tab of window1
        set URL of currTab to myBlog
    end tell
end tell

看看 Chrmoe 浏览器是不是打开了你指定的 URL 了?有意思吧?

上面的测试代码都是在脚本编辑器里运行的,如何脱离脚本编辑器,直接在系统上运行呢?我们可以保存或导出脚本,点击文件菜 -> 存储,可以看到支持的格式有四种,如图所示:

AppleScript 快速入门

保存为脚本类型,然后通过 osascript 来执行脚本,如下:

/usr/bin/osascript test1.scpt

如果保存为应用程序类型,就是一个 .app 的包,直接双击打开就能运行。

二、数据类型

AppleScript 的数据类型比较简单,一般常用的有 number、string、list、record,也就是数字类型、字符串类型、列表类型、字典类型。

数字类型的赋值和使用如下:

set num1 to 10 # 给 num1 赋值
set num2 to 20 # 给 num2 赋值
set num3 to num1 + num2 # num1 + num2 赋值给 num3
set num4 to num3 * 2 # num3 * 2 赋值给 num4

字符串类型的赋值和使用如下:

set str1 to "exchen.net"
set str2 to "hehe"
set str3 to str1 & str2

字符串与数字的转换方法如下:

set str3Len to the length of str3
set numToStr to num1 as string
set strToNum to "123" as number

列表类型其实就是相当于数组,定义和操作列表类型的方法如下:

set myLists to {1, 2, "str", 4, 5} # 定义列表数据
set item 3 of myLists to "exchen" #操作第三列的数据
get myLists  # 获取列表数据

字典类型的定义和操作方法如下:

set myRecord to {name:"exchen", blog:"http://www.exchen.net", body:"hehe"} # 定义 Record 数据
set value to the body of myRecord # 从 Record 中获取 body 数据给 value
get value

三、条件语句

既然是脚本语言,当然不能少了 if 和 else 语句,使用方法如下:

set num to 123
if num = 123 then
    display dialog "等于 123"

else if strToNum > 456 then
    display dialog "大于 456"

else
    display dialog "不等于 123 也不大于 456"
end if

通过 contains 方法来进行字符串的比较判断:

set domainName to "www.exchen.net"
if domainName contains "exchen" then
    display dialog "包含 exchen"
else
    display dialog "不包含 exchen"
end if

四、循环

循环的写法有好几种,不过都是使用 repeat … end repeat,比如循环 100 次可以这样写:

set num to 10
repeat 100 times
    set num to num + 1
end repeat

get num

类似于 for 循环,就这样写:

set num to 5
repeat with counter from 0 to num by 1
    display dialog counter
end repeat

类似于 while 循环,可以这样写:

set num to 0
repeat until num ≥ 10
    display dialog num
    set num to num + 3
end repeat

五、函数

如果某些功能有重用性,应该要写成函数,AppleScript 也支持定义函数,定义和使用方法如下:

on testFun()
    set num to 1
end testFun

testFun()

函数当然会有返回值,通过 return 返回值:

on testFun()
    set num to 1
    return num
end testFun

set ret to testFun()
get ret

另外函数可能还会带参数,带参数的方法使用如下:

on testFun(str)
    display dialog str

end testFun

testFun("exchen")

函数有可能会带多个参数,使用方法如下:

on testFun(str1, str2)
    display dialog str1
    display dialog str2

end testFun

testFun("exchen", "hehe")

六、用户交互对话框

在前面我们使用过 display dialog 弹出对话框,如果要指定标题通过 with title 关键字,代码如下:

display dialog "这是内容" with title "这是标题"

指定按钮的内容,可以通过 buttons {"No", "Yes"},按钮个数最多三个,代码如下:

display dialog "这是内容" with title "这是标题" buttons {"No", "Yes"}

也可以通过 default button 设置默认选择的按钮,代码如下:

display dialog "这是内容" with title "这是标题" buttons {"No", "Yes"} default button "Yes"

还可以指定对话框的图标,icon 图标可以指定 note/stop/caution 类型,或者指向文件路径,代码如下:

display dialog "这是内容" with title "这是标题" buttons {"No", "Yes"} default button "Yes" with icon note

对话框一般是用于和用户进行交互,通过 button returned 可以获取用户点击了哪个按钮,然后进行相应用操作,代码如下:

display dialog "这是内容" with title "这是标题" buttons {"No", "Yes"} default button "Yes"
if button returned of result = "Yes" then

else if button returned of result = "No" then

end if

对话框中也可以带输入框,让用户进行输入内容,代码如下:

display dialog "请输入内容:" default answer ""

带输入框的对话框的效果如下图:

AppleScript 快速入门

输入内容之后,通过 text returned 来获取输入框的内容:

display dialog "请输入内容:" default answer ""
if text returned of result = "exchen" then
    get "exchen.net"
end if

七、使用词典

在第一节我们知道了如何在其他程序中执行任务,比如让浏览器打开 URL、清空回收站,如果还想执行其他额外更多的功能怎么办?去哪儿查相应的方法名称?

可以通过词典来找相应的方法名称,将应用直接拖到 Dock 上的脚本编辑器图标,然后就会显示扩展的词典,在这里可以查看该应用支持的相应方法名称说明,比如 Chrome 的词典如下图所示:

AppleScript 快速入门

有些应用没有功能扩展的词典,就会提示打开词典失败,如下图所示:

AppleScript 快速入门

八、操作其他程序的界面

本小节我们来试一下操作其他程序来实现简单的自动化,打开计算器,使用 entire contents 显示出 UI 信息,代码如下:

tell application "System Events"
    tell process "Calculator"
        entire contents
    end tell
end tell

返回 UI 信息如下:

{window 1 of application process "Calculator" of application "System Events", group 1 of window 1 of application process "Calculator" of application "System Events", static text "0" of group 1 of window 1 of application process "Calculator" of application "System Events", group 2 of window 1 of application process "Calculator" of application "System Events", button 1 of group 2 of window 1 of application process "Calculator" of application "System Events", button 2 of group 2 of window 1 of application process "Calculator" of application "System Events", button 3 of group 2 of window 1 of application process "Calculator" of application "System Events", button 4 of group 2 of window 1 of application process "Calculator" of application "System Events", button 5 of group 2 of window 1 of application process "Calculator" of application "System Events", button 6 of group 2 of window 1 of application process "Calculator" of application "System Events", button 7 of group 2 of window 1 of application process "Calculator" of application "System Events", button 8 of group 2 of window 1 of application process "Calculator" of application "System Events", button 9 of group 2 of window 1 of application process "Calculator" of application "System Events", button 10 of group 2 of window 1 of application process "Calculator" of application "System Events", button 11 of group 2 of window 1 of application process "Calculator" of application "System Events", button 12 of group 2 of window 1 of application process "Calculator" of application "System Events", ......

column 2 of table 1 of menu item 1 of menu "帮助" of menu bar item "帮助" of menu bar 1 of application process "Calculator" of application "System Events", menu item "计算器帮助" of menu "帮助" of menu bar item "帮助" of menu bar 1 of application process "Calculator" of application "System Events"}

比如我们关心的是按钮 9,信息比较多,一时看不出我们所关心的按钮,可以通过 Xcode 自带的工具 Accessibility Inspector 查看 UI 信息,打开 Xcode 菜单,在 Open Developer Tool 里可以找到它,打开之后点击捕获按钮,找到我们关心的按钮,效果如下图所示:

AppleScript 快速入门

在 Accessibility Inspector 界面往下拉,可以看到按钮 9 是在第二组的第四个,所图所示:

AppleScript 快速入门

从返回的 UI 信息里可以找到按钮信息:

button 4 of group 2 of window 1 of application process "Calculator"

编写代码实现点击按钮:

tell application "System Events"
    tell process "Calculator"
        entire contents
        click button 7 of group 2 of window 1
    end tell
end tell

如果想点击菜单,在 UI 返回信息里你关心的菜单,编写代码如下:

tell application "System Events"
    tell process "Calculator"
        click menu item "关于计算器" of menu "计算器" of menu bar item "计算器" of menu bar 1
    end tell
end tell

执行之后,就相当于点击了 "关于计算器" 菜单,如下图所示:

AppleScript 快速入门

九、运行参数

在第一节,我们知道通过 /usr/bin/osascript 能够执行脚本,如果脚本在启动的时候需要参数怎么办?通过 on run 定义好参数,代码如下:

on run {parameter1, parameter2}
    display dialog parameter1
end run

然后在命令行执行的时候,后面跟参数执行就行了,命令如下:

/usr/bin/osascript test1.scpt "exchen.net" "parameter2"

原文地址:http://www.exchen.net/applescript-快速入门.html

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...