二、AppleScript的数据类型

  • 目录
  • 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

脚本编辑器会将结果显示在下半部分的结果区中。


2144458-b84db9daa87db1a1.png
43EF8341-1A12-4611-9247-9C24F960E7BA.png

数字基本上分为两类:整数(intergers)和分数(fractional numbers)。整数用来计数,比如循环次数。分数或者称作实数 (real numbers,简写作reals)用来计算例如棒球的击中率。整数和实数都可以是负数。


3.字符串 String

字符串必须放到双引号里


2144458-65f20cd671d180b1.png
20BAA94B-CA67-46AF-BAA1-9F06E4E8C4C1.png

结果区中显示的字符串也是带有引号的。带有引号的就表示是字符串。

拼接字符串

可以通过 '&' 符号进行拼接

set x to "abc"
set y to "def"
set z to x & "连接" & y
2144458-5f24148a92ccdc71.png
DABB84A1-7FC8-4B3C-8E9D-3E2FC421FF70.png

查看字符串长度
length of / the length of

set theLength to the length of "I'm Rose."
2144458-b003267011f65785.png
4D3A5482-C665-4317-8C15-91D12FD6A2DD.png

length 为关键字,空格也会占用字符串的长度。

如果字符串中要包含双引号,则需要使用转义字符反斜杠 ''

set exampleString to "She said: \"Hi, I'm Rose.\""

强制类型转换

set a to "15" as number
2144458-da33239615c574f0.png
5F4CA888-3EFE-4E81-B82E-07CEE045DCBE.png

结果中 15不带双引号,变成了数字

set a to 15 as string
2144458-b5726574b1760dc5.png
7EE95B76-3FED-4E9E-8345-F23703AC53EE.png

结果中15变成了字符串。

set a to "1.99" as real
2144458-b2cf198681d38a90.png
48848958-4D48-4B5A-B55F-86BB73500BFA.png
set a to "1.99" as integer
2144458-b86622c5d93323bb.png
918EF52B-A476-4E8D-B14E-DAC7E7E89664.png

integer 为整数,精度丢失


4.列表 List

相当于OC中的数组。

set exampleList to {123.4, 567, "Rose", "Hello world"}
2144458-6b1c49e4816330e1.png
BA6B91D4-0EBF-4FDB-BE1B-2E26C7E27C2D.png

拼接数组

和string一样,通过 '&' 符号拼接

set a to {"a"}
set b to {"b"}
set c to {"c"}
set d to a & b & c
2144458-c9eb5d405e5a6268.png
413ADF2A-ADBE-4C57-AE72-62E688CD3FB7.png

追加元素

set a to {"a"}
set c to a & "b"
2144458-1691005d7878932c.png
FAB959A7-8B02-4039-A620-0E0188698FBD.png

取代元素

set listA to {"a", "b"}
set item 2 of listA to "c"
get listA
2144458-87a1817bc8688b74.png
86434645-00C0-44ED-985B-F003FEFFE449.png

将第二个元素,变成了 "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
2144458-4e028f03988d7588.png
95CE6C5A-8F4D-470C-9DD5-54DF0F8CA501.png

取最后一个元素

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
2144458-1dc37362b751d34a.png
D91DA630-C38E-494F-8C0E-85BCE55CBEEE.png

取列表中的一个范围的元素

set listA to {"a", "b", "c", "d", "e", "f", "g", "h"}
set rangeItems to items 2 through 5 of listA

取第二个到第五个元素,并不是5个长度的


2144458-21cd7c71f9c7477f.png
EEAC7227-FF4E-4696-A884-21DAD81CCD30.png

注意:如果使用items 5 through 2 of listA,字面意思是从第五个到第二个,但实际上取的仍是从第二个到第五个,并不会反向的取出。

使用列表中的元素反向

set reversedList to reverse of {3, 2, 1}
2144458-3cb062cb7105269c.png
5B9C990D-876F-4867-AA66-3D56326EA346.png

计算列表元素个数
可以通过以下指令得到

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
2144458-fca58a88eb9b8279.png
E68D74DD-7C7D-4549-A5EC-A2AE283919FE.png

追加元素时,第一个是列表才能拼接

set a to {"a"}
set c to a & "b"

如果位置换过来,那么就会变成了拼接字符串

set a to {"a"}
set c to "b" & a
2144458-c8b694b80739c87a.png
D1C4AFE5-E958-4165-ADA0-AD5E86513981.png

所以需要对"b"类型转换

set a to {"a"}
set c to ("b" as list) & a
2144458-88537ec77b26bb8e.png
60F2996F-C706-4548-9F75-F38D958184C7.png

追加元素还可以使用

set listA to {1, 2, 3, 4}
set the end of listA to 5
get listA
2144458-1c795b2921b83807.png
1D24A980-6217-4CAA-AC25-1C836B50CFD1.png

将字符串的每个字母组成列表

set itemized to every character of "I'm Rose."
2144458-a0af61673025d832.png
C827F79B-F309-4904-8A33-185C9EFE61E0.png

通过某个字符分割字符串
通过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
2144458-d1c2106d2314d879.png
85B81A3E-36E4-459D-AAD4-9841FB1A1FAD.png

列表转为字符串

set listA to {"a", "b", "c", "d", "e", "f", "g", "h"}
set listA to listA as string
2144458-7e2a6f9e1360446b.png
B0BE0F87-B0C3-4854-99C9-B94587340ED3.png

通过若干字符拼接字符串

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
2144458-9ec62c961c720757.png
BDF08AD2-7B58-41D4-BFCB-5F9855D70D58.png

5. 记录 record

相当于OC中的字典。

set friend to {age:10, nickName:"张三"}
2144458-799361caad33adf1.png
445F9981-1CE1-4871-B902-64BE1575583D.png

记录中的单元叫做属性(property),不是元素(item)。不能通过item来取出数据。

查看记录中包含多少个属性

set friend to {age:10, nickName:"张三"}
set propertyCount to count of friend
2144458-41aab090d1cfc008.png
F2A0E040-B15F-4A9C-8473-B0CE5653A3B8.png

取出记录中的某个key对应的值

set friend to {age:10, nickName:"张三"}
set temp to age of friend
2144458-d39823e2bb7fd03b.png
1A47A77F-438C-4098-9535-400365214E7B.png

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

set age to 30
set resultAge to age
set age to 50
get resultAge
2144458-02438c4b755f9129.png
63EDBBF6-79C2-491F-AB60-FDB50AB644B2.png

但当我们将数据传入记录或者列表时,结果如下

set recordA to {age:30}
set resultA to recordA
set age of recordA to 50
get resultA
2144458-8115107bba5775be.png
D4BC30FE-4366-469E-8CA8-32834DC8C98E.png

age 会随之改变,为了保证数据被复制,可以使用copy指令

set recordA to {age:30}
copy recordA to resultA
set age of recordA to 50
get resultA
2144458-19ee5b1933371758.png
CD56B063-BF63-47B8-833D-58EE25045D6E.png
  • 注意:
  • AppleScript中的变量名由一个词组成,中间不能留有空格。不能以数字开头,但数字可以在变量名中出现。命名允许使用下划线“_”。
  • 赋值时使用set to 语句: set 变量名 to 变量值
  • AppleScript保留的标识符,不能被用户定义为自己的标识符。AppleScript官方文档关键字说明

猜你喜欢

转载自blog.csdn.net/weixin_34194317/article/details/87417701