Skip to content

Instantly share code, notes, and snippets.

@thomasjao
Last active December 28, 2015 05:09
Show Gist options
  • Save thomasjao/7447717 to your computer and use it in GitHub Desktop.
Save thomasjao/7447717 to your computer and use it in GitHub Desktop.
-- using list in AppleScript
-- define a list
set emptyList to {}
set d to {"c", "d"}
set e to {"e", "f"}
set ne to {"a", "b", 1, 3, d, e}
-- count items in a list
count ne
--> 6
length of ne
--> 6
-- get count of each type in a list
count ne each number
--> 2 {1, 3}
count ne each text
--> 2 {"a", "b"}
count ne each list
--> 2 {d, e}
-- get items expect the first item of a list
rest of ne
--> {"b", 1, 2, {"c", "d"}, {"e", "f"}}
reverse of ne
--> {{"e", "f"}, {"c", "d"}, 3, 1, "b", "a"}
-- show items in a list
items in ne
--> {"a", "b", 1, 3, {"c", "d"}, {"e", "f"}}
text in ne
--> {"a","b"}
lists in ne
--> {{"c","d"},{"e","f"}}
numbers in ne
--> {1,3}
-- display & concatenate parts of list (or two lists)
lists in ne & numbers in ne
--> {{"c","d"},{"e","f"},1,3}
item 6 of ne & item 5 of ne
--> {"e", "f", "c", "d"}
item 3 of ne & item 5 of ne
--> {1, "c", "d"}
item 3 of ne & item 1 of ne
--> {1, "a"}
-- Note: Comparing results of following two expression
item 1 of ne & item 3 of ne
--> "a1"
(item 1 of ne as list) & item 3 of ne
--> {"a", 1}
items 1 through 3 of ne
--> {"a", "b", 1}
-- Judge data types in a list
class of item 1 of ne
--> text
class of item 3 of ne
--> integer
class of item 5 of ne
--> list
-- Check if some item exists in a list
"a" is in ne
--> true
{"c", "d"} is in ne
--> false
{{"c", "d"}} is in ne
--> true
ne contains {{"e", "f"}}
--> true
{{"c", "d"}} is in ne
--> true
{{"c", "d"}} is in ne
--> true
-- Create a new list by selecting some items of an existing list
set ni to (items 1 through 3 of ne)
--> {"a", "b", 1}
-- Delete some items from original list
copy (item 1 of ne as list) & items 3 through -1 of ne to ne
--> {"a", 1, 3, {"c", "d"}, {"e", "f"}} -- delete item 2 and keep as a list
-- Comparing some items in a list
item 1 of ne = "a"
--> true
-- Check start item of a list
ne starts with "a"
-- Check end item of a list
ne ends with {"e", "f"}
--> false
ne ends with {{"e", "f"}}
--> true
-- append new item to end of a list
copy (month of (current date)) to end of ne
--> {"a", 1, 3, {"c", "d"}, {"e", "f"}, November}
-- replace specified item with new data
copy (day of (current date)) to (item 3 of ne)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment