AppleScript 基础
一、基本语法
- 在 AppleScript 中所有值都需要使用双引号引起来。
- 变量命名字母或下划线开头。官方建议使用驼峰的方式。
- 语句结束使用换行
例如:定义变量variable值为Hello Word
set variable to "Hello Word"
二、常用数据类型
- number 数字
- string 字符串
- boolean 布尔
- list 列表类型(:list 中的内容成为元素 item)�
- record 记录(:record 中的内容成为属性property)
幸运的是 AppleScript 变量不用强制定义类型
三、字符串介绍和常用操作
1. 定义一个字符串 "set ... to ..."
set myName to "elephant"
2. 字符串连接符 "&"
set myName to "elephant"
set age to 20
-- get类似 php、python 的 echo、print
get "name: " & myName & "; age: " & age
>>> "name: elephant; age: 20"
3.查看字符串长度 "to the length/count of"
set myName to "elephant"
-- 这里 length 替换成 count 可以可以的
set nameLength to the length of myName
>>> 8
4.将字符串按照每个字母切割成 list "to every character of"
set myName to "elephant"
set myList to every character of myName
get myList
>>> {"e", "l", "e", "p", "h", "a", "n", "t"}
当然这种切割基本上没啥用,我们希望的是使用特定的符号切割字符串。继续向下看
5.按照特定的字符切割字符串 "AppleScript's text item delimiters"
set myName to "Very fat elephant"
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set myList to every text item of myName
set AppleScript's text item delimiters to oldDelimiters
get myList
>>> {"Very", "fat", "elephant"}
-- 如果只是按照空格切分字符串可以使用下面的方法
set myList to words of myName
get myList
>>> {"Very", "fat", "elephant"}
-- 还可以限制数量哦
set myList to words 1 thru 2 of myName
get myList
>>> {"Very", "fat"}
说明:
第3行:设置要切分的特殊字符
第2,5行:因为系统默认的AppleScript's text item delimiters这个东西是{""}所以在切割完字符串后需要在修改回来
第4行:"to every text item of"每个文本元素;而"to every character of"每个字符
6. 获取指定范围的字符串 "text from word ... to word ... of ..."
set shortList to text from word 1 to word 2 of "We're all in this together"
>>> "We're all"
6. 字符串比较运算符
begins with (or, starts with) | 以……开头 |
---|---|
ends with | 以……结尾 |
comes before / comes after | 在……之前 |
is in | 在……之中 |
contains | 包含 |
does not contain | 不包含 |
does not comes before | 不在....之前 |
does not start with | 不以……开头 |
does not contain | 不以……结尾 |
is not in | 不在……之内 |
用法示例:
set myName to "Very fat elephant"
set bool to myName is equal to "Very fat elephant"
get bool
>>> true
set myName to "Very fat elephant"
set bool to myName does not contains "Very"
get bool
>>> false
set myName to "Very fat elephant"
set bool to myName comes before "Very"
get bool
>>> false
set myName to "Very fat elephant"
set bool to "V" comes before myName
get bool
>>> true
四. 列表(list)介绍和常用操作
1.定义一个列表
set myList to {"a", "b", "c"}
2. 替换list 中元素的内容 "set item ... of ... to ..."
:在 AppleScript中 list 是从1开始计数的
set myList to {"a", "b"}
set item 2 of myList to "B"
get myList
>>> {"a", "B"}
3.获取 list 中最后一个元素或第一个 "set ... to first/last item of ..."
set myList to {"a", "b", "c", "d", "e", "f"}
set value to last item of myList
>>> "f"
4.获取指定值 "set ... to item ... of ..."
set myList to {"a", "b", "c", "d", "e", "f"}
set value to item -2 of myList
>>> "e"
5.获取指定范围的数据
获取从 N到M 所有的值
set myList to {"a", "b", "c", "d", "e", "f"}
set shortlist to items 2 through 5 of myList
get shortlist
>>> {"b", "c", "d", "e"}
获取范围数据时,根据数据类型筛选
set myList to {1, "a", 3, "b", 4, 5}
set shortlist to integers 1 thru 3 of myList
get shortlist
>>> {1, 3, 4}
6.排序 "set ... to reverse of ..."
set myList to {"a", "b", "c", "d", "e", "f"}
set reverseList to reverse of myList
get reverseList
7.获取 list 中的数量,和字符串一样 "set ... to the length/count of..."
set myList to {"a", "b", "c", "d", "e", "f"}
set listCount to the length of myList
>>> 6
8.随机获取 list 中一个数据 "set ... to some item of..."
set myList to {"a", "b", "c", "d", "e", "f"}
set a to some item of myList
get a
9.给 list 末尾添加一个元素 "set the end of ... to ..."
set myList to {"a", "b", "c", "d", "e", "f"}
set the end of myList to "zzz"
get myList
>>> {"a", "b", "c", "d", "e", "f", "zzz"}
10.将 list 组成字符串
set myList to {"a", "b", "c", "d", "e", "f", "ads"}
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "-"
set myList to myList as string
set AppleScript's text item delimiters to oldDelimiters
get myList
>>> "a-b-c-d-e-f-ads"
11.比较运算符和字符串类似
五、记录(record)
1. 创建一个记录
set myRecord to {name: "elephant", age: 20}
2. 获取记录的值 "set ... to ... of ..."
set myRecord to {name: "elephant", age: 20}
set value to name of myRecord
get value
>>> "elephant"
3. 修改记录的值
set myRecord to {name: "elephant", age: 20}
set age of myRecord to 30
get myRecord
>>> {name:"elephant", age:30}
4. 获取数量 "set ... to the length/count of ..."
set myRecord to {name:"elephant", age:20}
set recordCount to the length of myRecord
get recordCount
>>> recordCount
六、类型转换 "... as ..."
set a to "15" as number
get a
>>> 15
set b to 123 as string
get b
>>> "123"
set c to "asdf" as list
get c
>>> {"asdf"}
set d to 0 as boolean
get d
>>> false
网友评论