AppleScript 程式設計/列表和記錄
外觀
為了獲得最佳效果,您應該開啟指令碼編輯器,以便在閱讀時測試程式碼和結果。
Applescript 有兩種獨立的資料結構類用於表示專案集合:list 類和 record 類。列表是物件的有序集合,可以是 AppleScript 理解的任何值或資料結構。說列表內容是有序的,意味著列表中的每個專案都佔據一個編號位置。此編號稱為專案的索引,可用於從列表中檢索特定專案。
列表建立很簡單,您在{ 和} 之間放置的任何內容都是列表。
set myList to {} -- make a new list
set myList to myList & {1, "two", {7}, {fred:"barney", wilma:"betty", foo:"bar"}, 5} -- add a bunch of items to the list
set k to {6, "three", {8}, {george:"jetson", elroy:"jetson", judy:"jetson"}, 0}
在上例中,我們建立了兩個列表。第一個列表l 包含 5 個專案,數字 1,字串“two”,包含數字 7 的列表,包含三個屬性的記錄,fred、wilma 和foo,具有字串值,以及數字 5。
我們可以透過多種方式訪問這些專案。語句
item 3 of myList
third item of myList
myList's third item
myList's item 3
將全部返回相同的值,{7},即包含數字 7 的列表。
這按字元一次工作。(字串是字元序列,並相應地表現。)
set my_string to "freedom is not freedom fries"
repeat with counter_variable_name from 1 to count of my_string
set current_character to item counter_variable_name of my_string
end repeat
記錄是屬性的列表。您可以按名稱從記錄中檢索專案,但不能按索引檢索。例如,要檢索屬性列表{scooby:"doo", elroy:"jetson",grape:"ape"} 的elroy,我可以按名稱檢索它。
elroy of {scooby:"doo", elroy:"jetson", grape:"ape"}
這將返回字串"jetson"
但不能按索引檢索。
item 2 of {scooby:"doo", elroy:"jetson", grape:"ape"}
這將返回錯誤。