DON’T FORGET!

Create Now, December 11 starting at 10 a.m. PST.

Hear what’s coming next to Adobe® Creative Cloud™. Meet innovators like Kelby Media CEO Scott Kelby, karlssonwilker co-founder Hijalti Karlsson, and animation producer Justin Weyers. They will also be available after their sessions to answer your questions using the #CreateNow hashtag.

Chat with Adobe Evangelists Rufus Deuchler, Jason Levine, Paul Trani, and Terry White on #CreateNow starting at 11:45 a.m. PST.

This is one event you do not want to miss!

Meanwhile, we will continue to compare AppleScript and JavaScript.

LISTS AND ARRAYS

When writing a script you often need to work with a group of similar objects. For this both AppleScript and JavaScript have a mechanism for representing a group of similar objects with a single variable. For AppleScript, this structure is called a list and is created using the keyword set:

set thisList to {1, 2, 3, 4, 5}

For JavaScript, the structure is called an array and is created by assigning element values using var:

var thisArray = [1, 2, 3, 4, 5];

Notice that AppleScript uses curly braces for a list while JavaScript uses square brackets to designate items of an array.

INDEXING

AppleScript is one-based, meaning that indexes of items within a list start with the number one.

set thisList to {1, 2, 3, 4, 5}
set firstItem to item 1 of thisList
firstItem --value of firstItem is 1
JavaScript, on the other hand, is zero-based:
var thisArray = [1, 2, 3, 4, 5];
var firstItem = thisArray[0]; //value of firstItem is 1

REPEAT LOOPS

To process items within a list or array, a number of repeat loops can be used. For AppleScript, the most popular is a repeat with loop:

set thisList to {1, 2, 3, 4, 5}
repeat with counter from 1 to length of thisList
	display alert ("Item is now " & item counter of thisList)
end repeat

The corresponding loop in JavaScript is a called a for loop and performs three processes within the introductory for statement:

var thisArray = [1, 2, 3, 4, 5];
for (var counter = 0; counter < thisArray.length; counter++){
     alert ("Item is now " + thisArray[counter]);
}

PROPERTIES AND METHODS

In the above examples, we see that both AppleScript and JavaScript have a length property which returns the number of items within the list/array.

Both languages have similar methods for working with a list/array and its elements. One method involves restructuring the list/array by removing the first element each time through the loop (iteration). This is an efficient process since the target element is always the first item.

For AppleScript the method is rest of list.

set thisList to {1, 2, 3, 4, 5}
repeat while thisList is not {} --list is not empty
     display alert ("Item 1 is now " & item 1 of thisList)
     set thisList to rest of thisList --drop first item
end repeat

For JavaScript the method is shift.

var thisArray = [1, 2, 3, 4, 5];
while (thisArray.length > 0) { //while there are items in array
     alert ("Item 1 is now " + thisArray[0]); //display first item
     thisArray.shift(); //drop first item
}

When it comes to adding an item to a list/array, the two languages are quite different. With AppleScript you simply add an item to the beginning or end of a list using either set or copy:

set thisList to {1, 2, 3, 4, 5}
set the beginning of thisList to 0
thisList --result is {0, 1, 2, 3, 4, 5}
set thisList to {1, 2, 3, 4, 5}
copy 6 to the end of thisList
thisList --result is {1, 2, 3, 4, 5, 6}

For this JavaScript uses two methods:

//unshift adds the item to the front of the array
var thisArray = [1, 2, 3, 4, 5];
thisArray.unshift(0); 
thisArray; //result is [0, 1, 2, 3, 4, 5]
//push adds the item to the end of the array
var thisArray = [1, 2, 3 , 4, 5];
thisArray.push(6);
thisArray; //result is [1, 2, 3, 4, 5, 6]

One method available for JavaScript which has no corresponding method in AppleScript is sort().

var thisArray = ["John", "Adam", "Mary"];
thisArray.sort(); //result is ["Adam", "John", "Mary"]

To do the same in AppleScript takes a little more doing. There are a number of sort routines that can be used; some are more efficient than others. The following is not the most efficient, but perhaps easiest to understand:

set thisList to {"John", "Adam", "Mary"}
set sortedList to {item 1 of thisList}
repeat with counter from 2 to length of thisList
     --if less than the first item, add it to the beginning of the list
     if item counter of thisList < (item 1 of sortedList) then
          set the beginning of sortedList to item counter of thisList
     else 
     --repeat through sorted list and restructure when comparison is true
          repeat with i from 2 to length of sortedList
               if item counter of thisList ≤ item i of sortedList then
                    set listBegin to items 1 through (i - 1) of sortedList
                    set listEnd to items i through length of sortedList
                    set end of listBegin to item counter of thisList
                    set sortedList to listBegin & listEnd
                    exit repeat
               else --no less than comparison found; add element to end of list
                    set end of sortedList to item counter of thisList
               end if
          end repeat
     end if
end repeat
sortedList --result {"Adam", "John", "Mary"}

As you can see, each language has its strengths and weaknesses. Next, we will explore how you can take advantage of the strengths of both languages no matter which one you happen to be working with.