3.1 Using Lists

The simplest data structure in Applescipt is a list.

3.1.1 Define a list

You can set a variable to contain an array using set and to and the curly braces:

set myList to {"apple", 4, "blue" & "berries", 20}

3.1.2 Retrieve an item from a list

To retreive an item from this array just ask for it

set myList to {"apple", 4, "blue" & "berries", 20}
set myVal to item 3 of myList
set output to myVal

3.1.3 Count items in a list

AppleScript arrays are NOT zero-indexed. That's because AppleScript uses natural language to count items in the list.

To count the number of items in a list use: the number of items in

set myList to {"apple", 4, "blue" & "berries", 20}
set myVal to item 3 of myList
set howMany to the number of items in myList
set output to "Item 3 of " & howMany & " is " & myVal

3.1.4 Retrieve a range of items

use natural language to list a range of items from the list "x through y of z"

set myList to {"apple", 4, "blue" & "berries", 20}
set rangeOfItems to items 2 through 4 of myList
set output to rangeOfItems

3.1.5 Change an item in a list based on index

change an item in the list using the item index

set myList to {"apple", 4, "blue" & "berries", 20}
set item 3 of myList to "orange"
set rangeOfItems to items 2 through 4 of myList
set output to rangeOfItems

3.1.6 Add lists together

You can add lists together using the ampersand: &

set myList to {"apple", 4, "blue" & "berries", 20}
set newList to myList & {40, "bananas"}
set myVal to item 3 of myList
set howMany to the number of items in newList
set item 3 of myList to "orange"
set rangeOfItems to items 2 through 4 of newList
 
set output to "My new list containts: " & howMany & " items.
They are: " & newList & ".
Items 2 through 4 are: " & rangeOfItems & ".
Item number 3 is: " & myVal

3.2 Using Records

In AppleScript records are like a hash in ruby or an associative array.

3.2.1 Set the values in an associative array

Set the values in an associative array using set and to and curly braces {}.

set myAlien to {name:"Mr. Largon", type:"Andro-Gruub", language:"Common Borbulian", sex:"Only with Other Gruubs"}

3.2.2 Retrieve a record

set myAlien to {name:"Mr. Largon", type:"Andro-Gruub", language:"Common Borbulian", sex:"Only with Other Gruubs"}
set thisName to name of myAlien
set output to "The name of this Alien is: " & thisName

3.2.3 Change a value in the array

To do this specify the item you want to change and the array in which you want to change it

set sex of myAlien to "No Thankyou!"

as in:

set myAlien to {name:"Mr. Largon", type:"Andro-Gruub", language:"Common Borbulian", sex:"only with other Gruubs"}
set thisName to name of myAlien
 
-- Change an item in the existing array
set sex of myAlien to "No Thankyou!"
 
set thisSex to sex of myAlien
 
set output to "Central Computer: 'Hello " & thisName & "'
" & thisName & ": 'Hello Computer'
" & "Central Computer: 'Would you like some sex?'
" & thisName & ": '" & thisSex & "'"

3.2.4 Perform an operation on a record

In this case the operation is "count" using the syntax: "set variable to count of array name"

set myAlien to {name:"Mr. Largon", type:"Andro-Gruub", language:"Common Borbulian", sex:"only with other Gruubs"}
 
-- perform operation on the record
set myLength to count of myAlien
 
set output to myLength

3.3 Listing files in a folder

3.3.1 Listing files in a folder

You know how to create a folder with a file in it:

tell application "Finder"
 
    -- todo check if Test_folder exists from a previous run, if so delete it.
 
    -- start on the desktop and create a folder
    set myStartPath to (path to desktop folder)
    make new folder at myStartPath with properties {name:"Test_folder"}
 
    -- make a test file in the folder
    set myFolderPath to (path to desktop folder as string) & "Test_folder"
    make new file at myFolderPath with properties {name:"Test_File", type:"jpg"}
 
end tell

In the above script the variable myFolderPath holds the path to the folder as a string. Of course you cannot interact with the files in the variable: Test_folder, because "a string" is "a string" and not a folder.

But you can cast this variable as an alias of the folder to which the path points instead of a string of the path to the folder. An alias points to the actual folder data on the HD so you can actually interact with the folder and its contents

tell application "Finder"
 
    -- todo check if Test_folder exists from a previous run, if so delete it.
 
    -- start on the desktop and create a folder
    set myStartPath to (path to desktop folder)
    make new folder at myStartPath with properties {name:"Test_folder"}
 
    -- make a test file in the folder
    set myFolderPath to (path to desktop folder as string) & "Test_folder"
    make new file at myFolderPath with properties {name:"Test_File", type:"jpg"}
 
    -- access the Test_folder as an alias
    set myFolder to ((path to desktop folder) & "Test_folder" as string as alias)
    set myFiles to the name of every file of myFolder
 
    set output to myFiles
 
end tell