- 目录
- 1.布尔类型 Boolean
- 2.数字 Number
- 3.字符串 String
- 4.列表 List
- 5.记录 Record
1.布尔类型 Boolean
True 和 False (不区分大小写)
set a to true
set b to false
2.数字 Number
set x to 25
set y to 4321.234
可以使用算数运算符
+ 加
- 减
* 乘
/ 除
^ 乘方
set x to 10.0
set y to x ^ 3
脚本编辑器会将结果显示在下半部分的结果区中。

数字基本上分为两类:整数(intergers)和分数(fractional numbers)。整数用来计数,比如循环次数。分数或者称作实数 (real numbers,简写作reals)用来计算例如棒球的击中率。整数和实数都可以是负数。
3.字符串 String
字符串必须放到双引号里

结果区中显示的字符串也是带有引号的。带有引号的就表示是字符串。
拼接字符串
可以通过 '&' 符号进行拼接
set x to "abc"
set y to "def"
set z to x & "连接" & y

查看字符串长度
length of / the length of
set theLength to the length of "I'm Rose."

length 为关键字,空格也会占用字符串的长度。
如果字符串中要包含双引号,则需要使用转义字符反斜杠 ''
set exampleString to "She said: \"Hi, I'm Rose.\""
强制类型转换
set a to "15" as number

结果中 15不带双引号,变成了数字
set a to 15 as string

结果中15变成了字符串。
set a to "1.99" as real

set a to "1.99" as integer

integer 为整数,精度丢失
4.列表 List
相当于OC中的数组。
set exampleList to {123.4, 567, "Rose", "Hello world"}

拼接数组
和string一样,通过 '&' 符号拼接
set a to {"a"}
set b to {"b"}
set c to {"c"}
set d to a & b & c

追加元素
set a to {"a"}
set c to a & "b"

取代元素
set listA to {"a", "b"}
set item 2 of listA to "c"
get listA

将第二个元素,变成了 "c"
set the second item of listA to "c"
set the 2nd item of listA to "c"
也是同样的作用。
取数组中的某个元素
set listA to {"a", "b"}
set secondItem to item 2 of listA

取最后一个元素
set listA to {"a", "b"}
set lastItem to the last item of listA
或者
set listA to {"a", "b"}
set lastItem to item -1 of listA

取列表中的一个范围的元素
set listA to {"a", "b", "c", "d", "e", "f", "g", "h"}
set rangeItems to items 2 through 5 of listA
取第二个到第五个元素,并不是5个长度的

注意:如果使用items 5 through 2 of listA
,字面意思是从第五个到第二个,但实际上取的仍是从第二个到第五个,并不会反向的取出。
使用列表中的元素反向
set reversedList to reverse of {3, 2, 1}

计算列表元素个数
可以通过以下指令得到
set listLength to the length of {"a","b","c"}
set listLength to the count of {"a","b","c"}
强制类型转换
set a to "a"
set b to a as list

追加元素时,第一个是列表才能拼接
set a to {"a"}
set c to a & "b"
如果位置换过来,那么就会变成了拼接字符串
set a to {"a"}
set c to "b" & a

所以需要对"b"类型转换
set a to {"a"}
set c to ("b" as list) & a

追加元素还可以使用
set listA to {1, 2, 3, 4}
set the end of listA to 5
get listA

将字符串的每个字母组成列表
set itemized to every character of "I'm Rose."

通过某个字符分割字符串
通过AppleScript's text item delimiters来实现,将其设置为空格 " ",使用完之后还需要将其改回原来的值
set myString to "Hi there."
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set myList to every text item of myString
set AppleScript's text item delimiters to oldDelimiters
get myList

列表转为字符串
set listA to {"a", "b", "c", "d", "e", "f", "g", "h"}
set listA to listA as string

通过若干字符拼接字符串
set listA to {"a", "b", "c", "d", "e", "f", "g", "h"}
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "~~"
set myList to listA as string
set AppleScript's text item delimiters to oldDelimiters
get myList

5. 记录 record
相当于OC中的字典。
set friend to {age:10, nickName:"张三"}

记录中的单元叫做属性(property),不是元素(item)。不能通过item来取出数据。
查看记录中包含多少个属性
set friend to {age:10, nickName:"张三"}
set propertyCount to count of friend

取出记录中的某个key对应的值
set friend to {age:10, nickName:"张三"}
set temp to age of friend

深浅拷贝
当我们将数据直接赋值给一个变量时,结果不会随age的改变而改变
set age to 30
set resultAge to age
set age to 50
get resultAge

但当我们将数据传入记录或者列表时,结果如下
set recordA to {age:30}
set resultA to recordA
set age of recordA to 50
get resultA

age 会随之改变,为了保证数据被复制,可以使用copy指令
set recordA to {age:30}
copy recordA to resultA
set age of recordA to 50
get resultA

- 注意:
- AppleScript中的变量名由一个词组成,中间不能留有空格。不能以数字开头,但数字可以在变量名中出现。命名允许使用下划线“_”。
- 赋值时使用set to 语句: set 变量名 to 变量值
- AppleScript保留的标识符,不能被用户定义为自己的标识符。AppleScript官方文档关键字说明
网友评论