列表 list

1.创建list
set myList to {"iMac","iPad","iPhone",2,3,5}

2.打印list

set myList to {"iMac","iPad","iPhone",2,3,5}` 
get myList

3.拼接List
跟拼接字符串一样,使用&

set mac to {"iMac"}
set pad to {"iPad"}
set phone to {"iPhone"}
set numberList to {2, 3, 5}
set myList to mac & pad & phone & numberList
get myList
--{"iMac", "iPad", "iPhone", 2, 3, 5}

4.修改List
以下都是讲list中的"spring"修改成"monsoon"

set seasons to {"summer", "spring", "winter"}
set item 2 of seasons to "monsoon"
get seasons
--{"summer", "monsoon", "winter"}
set seasons to {"summer", "spring", "winter"}
set 2nd item of seasons to "monsoon" --2nd 3rd 4th 英语不好不建议用这种写法,注意不要越界
get seasons
--{"summer", "monsoon", "winter"}

修改首个item和最后一个item

set seasons to {"summer", "spring", "winter"}
set first item of seasons to "monsoon"
set last item of seasons to "monsoon"
get seasons
--{"monsoon", "spring", "monsoon"}

5.获取列表元素

set numberList to {1,2,3,4,5}
set myNumber to the first item of bag --1
set myNumber to the last item of bag --5
set myNumber to item 1 of bag --1
set myNumber to item -1 of bag --5
set myNumber to item -2 of bag --4

6.列表长度

set numberList to {1,2,3,4,5}
set listSize to the length of numberList 
--5

7.截取列表

set numberList to {1,2,3,4,5}
set subList to items 2 through 4 of numberList

{2, 3, 4}
8.反转列表

set reverseList to reverse of {1, 2, 3, 4, 5}
-- {5, 4, 3, 2, 1}

9.随机获取item

set myList to {"iMac", "iPad", "iPhone"}
set myDevice to some item of myList

10.字符串list互转

set myStr to "Hello Apple Script"
set myList to myStr as list
-- {"Hello Apple Script"}
set myList to {"Hello ", "Apple "," Scrip" }
set myStr to myList as string
-- "Hello Apple  Scrip"

11.拼接字符串和list

set myStr to "Hello Apple Script"
set myList to {"iMac", "iPad", "iPhone"}
set newList to myList & myStr      -- {"iMac", "iPad", "iPhone", "Hello Apple Script"} 结果是list
set newStr to myStr & myList      -- "Hello Apple ScriptiMaciPadiPhone" 结果是string
set newList to (myList as string) & myStr      -- "iMaciPadiPhoneHello Apple Script"  
set newStr to (myStr as list) & myList      -- {"Hello Apple Script", "iMac", "iPad", "iPhone"}

12.字符串转list
按字符拆分

set myLetters to every character of "Hello AppleScript"
-- {"H", "e", "l", "l", "o", " ", "A", "p", "p", "l", "e", "S", "c", "r", "i", "p", "t"}

13.字符串转list
通过指定分隔符拆分字符串为数组
使用文本限定符 AppleScript's text item delimiters 设置分割符

set myStr to "Hello Apple Script"
set AppleScript's text item delimiters to " "  --设置分隔符
set myList to every text item of myStr
get myList
-- {"Hello", "Apple", "Script"}

13.list转字符串
通过指定连接符将list的item连接成数组
使用文本限定符 AppleScript's text item delimiters 设置分割符

set myList to {"Hello", "Apple", "Script"}
set AppleScript's text item delimiters to " "
set myStr to myList as string
-- "Hello Apple Script"
posted on 2020-12-07 21:50  付心武士  阅读(122)  评论(0编辑  收藏  举报