- 目錄
- 1.布爾類型 Boolean
- 2.數(shù)字 Number
- 3.字符串 String
- 4.列表 List
- 5.記錄 Record
1.布爾類型 Boolean
True 和 False (不區(qū)分大小寫)
set a to true
set b to false
2.數(shù)字 Number
set x to 25
set y to 4321.234
可以使用算數(shù)運(yùn)算符
+ 加
- 減
* 乘
/ 除
^ 乘方
set x to 10.0
set y to x ^ 3
腳本編輯器會(huì)將結(jié)果顯示在下半部分的結(jié)果區(qū)中级及。
數(shù)字基本上分為兩類:整數(shù)(intergers)和分?jǐn)?shù)(fractional numbers)。整數(shù)用來計(jì)數(shù),比如循環(huán)次數(shù)陪蜻。分?jǐn)?shù)或者稱作實(shí)數(shù) (real numbers,簡寫作reals)用來計(jì)算例如棒球的擊中率贱鼻。整數(shù)和實(shí)數(shù)都可以是負(fù)數(shù)宴卖。
3.字符串 String
字符串必須放到雙引號(hào)里
結(jié)果區(qū)中顯示的字符串也是帶有引號(hào)的。帶有引號(hào)的就表示是字符串邻悬。
拼接字符串
可以通過 '&' 符號(hào)進(jìn)行拼接
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 為關(guān)鍵字症昏,空格也會(huì)占用字符串的長度。
如果字符串中要包含雙引號(hào)父丰,則需要使用轉(zhuǎn)義字符反斜杠 ''
set exampleString to "She said: \"Hi, I'm Rose.\""
強(qiáng)制類型轉(zhuǎn)換
set a to "15" as number
結(jié)果中 15不帶雙引號(hào)肝谭,變成了數(shù)字
set a to 15 as string
結(jié)果中15變成了字符串。
set a to "1.99" as real
set a to "1.99" as integer
integer 為整數(shù)蛾扇,精度丟失
4.列表 List
相當(dāng)于OC中的數(shù)組攘烛。
set exampleList to {123.4, 567, "Rose", "Hello world"}
拼接數(shù)組
和string一樣,通過 '&' 符號(hào)拼接
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
將第二個(gè)元素屁桑,變成了 "c"
set the second item of listA to "c"
set the 2nd item of listA to "c"
也是同樣的作用医寿。
取數(shù)組中的某個(gè)元素
set listA to {"a", "b"}
set secondItem to item 2 of listA
取最后一個(gè)元素
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
取列表中的一個(gè)范圍的元素
set listA to {"a", "b", "c", "d", "e", "f", "g", "h"}
set rangeItems to items 2 through 5 of listA
取第二個(gè)到第五個(gè)元素,并不是5個(gè)長度的
注意:如果使用items 5 through 2 of listA
蘑斧,字面意思是從第五個(gè)到第二個(gè)靖秩,但實(shí)際上取的仍是從第二個(gè)到第五個(gè),并不會(huì)反向的取出竖瘾。
使用列表中的元素反向
set reversedList to reverse of {3, 2, 1}
計(jì)算列表元素個(gè)數(shù)
可以通過以下指令得到
set listLength to the length of {"a","b","c"}
set listLength to the count of {"a","b","c"}
強(qiáng)制類型轉(zhuǎn)換
set a to "a"
set b to a as list
追加元素時(shí)沟突,第一個(gè)是列表才能拼接
set a to {"a"}
set c to a & "b"
如果位置換過來,那么就會(huì)變成了拼接字符串
set a to {"a"}
set c to "b" & a
所以需要對"b"類型轉(zhuǎn)換
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
將字符串的每個(gè)字母組成列表
set itemized to every character of "I'm Rose."
通過某個(gè)字符分割字符串
通過AppleScript's text item delimiters來實(shí)現(xiàn),將其設(shè)置為空格 " ",使用完之后還需要將其改回原來的值
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
列表轉(zhuǎn)為字符串
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
相當(dāng)于OC中的字典捕传。
set friend to {age:10, nickName:"張三"}
記錄中的單元叫做屬性(property)惠拭,不是元素(item)。不能通過item來取出數(shù)據(jù)庸论。
查看記錄中包含多少個(gè)屬性
set friend to {age:10, nickName:"張三"}
set propertyCount to count of friend
取出記錄中的某個(gè)key對應(yīng)的值
set friend to {age:10, nickName:"張三"}
set temp to age of friend
深淺拷貝
當(dāng)我們將數(shù)據(jù)直接賦值給一個(gè)變量時(shí)职辅,結(jié)果不會(huì)隨age的改變而改變
set age to 30
set resultAge to age
set age to 50
get resultAge
但當(dāng)我們將數(shù)據(jù)傳入記錄或者列表時(shí),結(jié)果如下
set recordA to {age:30}
set resultA to recordA
set age of recordA to 50
get resultA
age 會(huì)隨之改變聂示,為了保證數(shù)據(jù)被復(fù)制域携,可以使用copy指令
set recordA to {age:30}
copy recordA to resultA
set age of recordA to 50
get resultA
- 注意:
- AppleScript中的變量名由一個(gè)詞組成,中間不能留有空格鱼喉。不能以數(shù)字開頭秀鞭,但數(shù)字可以在變量名中出現(xiàn)趋观。命名允許使用下劃線“_”。
- 賦值時(shí)使用set to 語句: set 變量名 to 變量值
- AppleScript保留的標(biāo)識(shí)符锋边,不能被用戶定義為自己的標(biāo)識(shí)符皱坛。AppleScript官方文檔關(guān)鍵字說明