I am often asked by those new to the idea of automation as to which language to use: AppleScript or JavaScript. Similarly, in the early days of the computer I would be asked whether to purchase a Macintosh or PC. My answer, then, as now is “it depends.”

It Depends

The language of choice depends primarily on what you want to do with it. If you are a designer and work with Macintosh, you may want to start with AppleScript. If you are a Windows person or want your scripts to work on both platforms, you will need to use JavaScript. There are advantages to both. Both can be used for automating Adobe InDesign, Photoshop, and Illustrator. If you want to automate Adobe Acrobat, you will need to use JavaScript.

If you want to add some of Apple’s applications to your automation, only AppleScript will work. AppleScript can also be used to automate database programs such as FileMaker and Valentina.

Language Structure

The language structure for AppleScript is, in my opinion, a little easier for the beginner to start with. Relationships within the object hierarchy are established using sentence structure with the word “of.” To establish an action, you “tell” an object to perform an action.

tell application "Adobe InDesign CS6"
set docRef to document 1
set frameRef to text frame 1 of page 1 of spread 1 of docRef
tell frameRef to set text 1 to "Hello World"
end tell

JavaScript is a more formal language, using dot notation to show relationships:

var docRef = app.documents.item(0);
var frameRef = docRef.spreads.item(0).pages.item(0).textFrames.item(0);
frameRef.contents = "Hello World"

Other than that, the languages are fairly similar in structure with AppleScript using handlers (or subroutines) and JavaScript using functions to break code into manageable (and reusable) procedures.

--AppleScript:
set docRef to getDocRef()
(*Returns reference to active document*)
on getDocRef()
tell application "Adobe InDesign CS6"
if (exists document 1) then
return document 1
else
return missing value
end if
end tell
end getDocRef
//JavaScript:
var docRef = getDocRef();
/*Returns reference to active document*/function getDocRef(){
if (app.documents.item(0).isValid) {
return app.documents.item(0);
} else {
return null;
}
}

Both languages are similar in the way they throw and catch error conditions.

--AppleScript:
try
set docRef to getDocRef()
on error errStr
display alert errStr
end try
(*Returns reference to active document*)
on getdocRef()
tell application "Adobe InDesign CS6"
if (exists document 1) then
return document 1
else
error "No document open"
end if
end tell
end getDocRef
//JavaScript:
var docRef = getDocRef();
} catch (e) {
alert (e);
}
/*Returns reference to active document*/
function getDocRef() {
if (app.documents.item(0).isValid) {
return app.documents.item(0);
} else {
throw ("No document open");
}
}

This is just a tip of the iceberg comparison of the two languages, but should give those of you still considering which language to use some points to consider. More importantly, it is hoped that the examples demonstrate how relatively simple writing code in either language can be. Now you just need to decide on what you want to do with your tools.

AND, DON’T FORGET: CREATE NOW LIVE, DECEMBER 11, 10AM PST

Look up #CreateNow on Twitter to get in on the conversation. Register to attend at www.adobeeventsonline.com. You won’t want to miss this event.