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 window
tell window1
set currTab to active tab of window1
set URL of currTab to myBlog
end 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 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 ""

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

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

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

七、使用词典

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

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

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

八、操作其他程序的界面

本小节我们来试一下操作其他程序来实现简单的自动化,打开计算器,使用 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 里可以找到它,打开之后点击捕获按钮,找到我们关心的按钮,效果如下图所示:

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

从返回的 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

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

九、运行参数

在第一节,我们知道通过 /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


AppleScript 快速入门的更多相关文章

  1. Applescript快速入门及OmniFocus每日md报告开发

    本篇主要记录 Applescript 基础语法,以及利用 applescript 生成 omnifocus 每日报告 从 windows 转换到 macos,最近一直在不断折腾,这两天浏览 githu ...

  2. Web Api 入门实战 (快速入门+工具使用+不依赖IIS)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...

  3. SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=》提升)

     SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=>提升,5个Demo贯彻全篇,感兴趣的玩才是真的学) 官方demo:http://www.asp.net/si ...

  4. 前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)

    今天给大家道个歉,没有及时更新MongoDB快速入门的下篇,最近有点小忙,在此向博友们致歉.下面我将简单地说一下mongdb的一些基本命令以及我们日常开发过程中的一些问题.mongodb可以为我们提供 ...

  5. 【第三篇】ASP.NET MVC快速入门之安全策略(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  6. 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  7. Mybatis框架 的快速入门

    MyBatis 简介 什么是 MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果 ...

  8. grunt快速入门

    快速入门 Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. Grunt 0.4.x 必须配合Node.js >= 0.8.0版本使用.:奇数版本 ...

  9. 【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

随机推荐

  1. 任务十六:零基础JavaScript编码(四)

    任务目的 在上一任务基础上继续JavaScript的体验 深入学习JavaScript的事件机制及DOM操作 学习事件代理机制 学习简单的表单验证功能 学习外部加载JavaScript文件 任务描述 ...

  2. geoserver 知识小计

    http://localhost:8888/geoserver/wms?service=WMS&request=GetCapabilities 这个地址用于获取发布的WMS服务的属性,用于获取 ...

  3. named 快速部署及主机记录普及

    实验环境centos7.2,仅供参考 yum -y install epel-release    --安装最新yum配置源 cd /etc/yum.repos.d/ # wget http://re ...

  4. 关于实现XX系统设计时所实现的质量属性战术

    可用性: 1)使用Try-catch对抛出的异常进行处理 2)使用Spring事务管理 易用性: 1)在类似删除相关选项时,弹出提示框,防止误操作 2)在不编辑基本信息时,对其进行折叠或者隐藏 3)提 ...

  5. Java API 常用 详解

    Runtime类的使用:可以查看内存信息,系统变量,执行系统软件命令,备份数据库相关操作

  6. Day02——Python基本数据类型

    一.运算符 1.算数运算符 2.比较运算符 3.复制运算符 4.逻辑运算符 5.成员运算符 二.基本数据类型 1.数字 整数(int) 在32位机器上,整数的位数为32位,取值范围为-2**31-2* ...

  7. 对sql作业的总结(不用group by 通过with as,exists实现分类)

    一次数据库作业 题目如下: Consider the following SQL table definitions: CREATE TABLE OlympicEvent ( Name text, Y ...

  8. 爬虫入门之Scrapy框架实战(新浪百科豆瓣)(十二)

    一 新浪新闻爬取 1 爬取新浪新闻(全站爬取) 项目搭建与开启 scrapy startproject sina cd sina scrapy genspider mysina http://roll ...

  9. 如何向数据库中添加TIMESTAMP(6)类型的数据

    to_timestamp('2011-11-11 11:11:11.1','yyyy-mm-dd hh24:mi:ss.ff')

  10. JS 获取指定日期的前几天,后几天

    function getNextDate(date,day) { var dd = new Date(date); dd.setDate(dd.getDate() + day); var y = dd ...