欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

AppleScript 快速入门

发布时间:2024/4/18 编程问答 67 豆豆
生活随笔 收集整理的这篇文章主要介绍了 AppleScript 快速入门 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

AppleScript 快速入门

  • AppleScript 快速入门
    • 一、让其他程序执行任务
    • 二、数据类型
    • 三、条件语句
    • 四、循环
    • 五、函数
    • 六、用户交互对话框
    • 七、使用词典
    • 八、操作其他程序的界面
    • 九、运行参数

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

一、让其他程序执行任务

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

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 windowtell window1set currTab to active tab of window1set URL of currTab to myBlogend tell end tell

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

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

保存为脚本类型,然后通过 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 thendisplay dialog "等于 123"else if strToNum > 456 thendisplay dialog "大于 456"elsedisplay dialog "不等于 123 也不大于 456" end if

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

set domainName to "www.exchen.net" if domainName contains "exchen" thendisplay dialog "包含 exchen" elsedisplay dialog "不包含 exchen" end if

四、循环

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

set num to 10 repeat 100 timesset num to num + 1 end repeatget num

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

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

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

set num to 0 repeat until num10display dialog numset num to num + 3 end repeat

五、函数

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

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

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

on testFun()set num to 1return num end testFunset ret to testFun() get ret

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

on testFun(str)display dialog strend testFuntestFun("exchen")

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

on testFun(str1, str2)display dialog str1display dialog str2end testFuntestFun("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" thenelse if button returned of result = "No" thenend if

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

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

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

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

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

七、使用词典

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

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

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

八、操作其他程序的界面

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

tell application "System Events"tell process "Calculator"entire contentsend 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 里可以找到它,打开之后点击捕获按钮,找到我们关心的按钮,效果如下图所示:

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

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

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

编写代码实现点击按钮:

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

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

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

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

九、运行参数

在第一节,我们知道通过 /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-%e5%bf%ab%e9%80%9f%e5%85%a5%e9%97%a8.html

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是生活随笔为你收集整理的AppleScript 快速入门的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。