AS 是可以帮你在 mac 上实现自动化的有效工具,他经过类英语的语法,以及简单的编程功能,帮助你摆脱机械化的劳动。html
AppleScript 包括以下三部分:编程
在 mac 中自带 Automator
和 AppleScript Editor
都可以编写脚本。其中 Automator
是可视化工具,但功能更基础(彷佛连循环都没有)。app
tell current application display dialog "Hello World." end tell
tell current application set str to "xp" display dialog "Hello " & str end tell
tell application "Finder" empty the trash beep end tell
say "How are you?" using "Zarvox" say "Find, and you?" using "Victoria" say "Me, too." using "Zarvox" beep
AS 是面向对象的脚本语言,有三个重要的OO术语:对象(Object)、属性(Property)、命令(Command)ide
命令又称方法(Method),这些都是苹果官方《AppleScript Language Guide》的说法。工具
AS 采用 Unicode 编码,不区分大小写。ui
标识符必须以英文字母开头,可以使用英文字母、数字和下划线(_)。编码
额外规则:若以|
开头并结尾,可以使用任意 Unicode 字符做为标识符,但注意标识符自己不含有|
。不推荐使用这种写法。code
综上所述,合法的标识符如:abc, a0, |a&b|, |中文|htm
语言保留使用的标识符称为关键字,请注意 AS 中存在由多个词组成的关键字。对象
set var to value
数据类型
系统偏好设置-语言与文本
决定key of {key: value}
访问其值经过 class of
关键字来肯定数据类型:
-- 这是注释 set str to "string" class of str -- 返回 "text"
使用 as
关键字强制类型转换:
"1.99" as real -- 1.99 (real) "1.99" as integer -- 2 (integer) 精度丢失 "1" as real -- 1.0 (real) "text" as list -- { "text" } { a: 1, b: 2 } as list -- { 1, 2 } 精度丢失
不是全部的数据都可以强制类型转换,不要纠结于此,作有意义的事。
if condition then do sth. end if
repeat with i from 1 to 100 do sth. end repeat
更多语法请参阅 AppleScript Editor
中提供的 字典
。或者阅读 AppleScript 简明基础教程。