Adobe InDesign CS5 : Creating and Running Scripts
1. Writing simple scripts
The following three scripts, taken from Adobe's InDesign script examples, do the same thing in JavaScript, AppleScript, and VBA — they rotate an EPS graphic and its frame.
Type the lines that follow this paragraph exactly as they're written for the scripting language you've chosen. Type a return character at the end of each line. Note also the use of straight quotation marks instead of curly typesetter's quotes (the script editor does this for you). Be very careful when you type the text: Typos are script killers.
NOTE
In the code samples below, the ⌝ symbol indicates where a line of code is broken to fit the page width; in the actual script, there would be no ⌝ character, and the line that follows would be combined with the line that now ends in ⌝ .
1.1. JavaScript
var myL ink, myEPs, myFrame;
if(app.documents.length != 0){
if(app.activeDocument.links.length != 0){
for(var myLinkCounter = 0; myLinkCounter < ⌝
app.activeDocument.links.length; myLinkCounter ++){
myLink = app.activeDocument.links.item(myLinkCounter);
if(myLink.linkType == "EPS"){
myEPS = myLink.parent;
myFrame = myEPS.parent;
myFrame.rotationAngle = 30;
}
}
}
}This script first searches through all the items on the page and checks whether any are EPS; if so, it sets the rotation of the item to 30. (Thanks to Adobe's InDesign scripting guru, Olav Martin Kvern, for developing the JavaScript code example.) Note the sequence of actions: First you verify that there is a nonempty document open, and then you check the content type of objects. For those objects of the EPS type, you activate the frame and then apply the rotation to that frame. JavaScripts require you to set your own variables (the var statements) that define what the object is. (Here, myLink is used for each object, myEPS is the object that contains the EPS attribute, and myFrame is the parent frame containing the EPS graphic.) You work with those variables to see what their attributes are and then change those attributes.
If you're in an adventurous mood, try substituting the statement myFrame.rotationAngle = 30; in the preceding script with each of the following statements:
myFrame.shearAngle = 30; myFrame.verticalScale = 200;
If you want to get really fancy, combine all the myFrame. statements into a single script so that you can use the script to make all the changes at one time.
1.2. AppleScript
tell application "Adobe InDesign CS5"
activate
set myPageItems to {EPS, oval, rectangle, polygon}
set mySelection to selection
if class of item 1 of mySelection is in myPageItems ⌝
and (count mySelection) > 0 then
if class of item 1 of mySelection is EPS then
set myFrame to parent of mySelection
else
set myFrame to item 1 of mySelection
end if
set rotation angle of myFrame to 30
end if
end tellNOTE
Be sure that a document is open and that you have selected at least one frame of any type before starting the script. Also in the script itself, make sure to type the name of your InDesign program exactly as it appears in the Finder. Because you're free to rename your program, the name may not match the name in the first line of the script.
If you're feeling daring, try substituting the statement set rotation angle of myFrame to 30 in the preceding script with each of the following statements:
set shear angle of myFrame to 30 set vertical scale of myFrame to 200
Here again, you can get fancy by combining all the set statements into a single script so that you can use the script to make all the changes at one time.
1.3. VBA
Dim myInDesign As InDesign.Application
Set myInDesign = CreateObject("InDesign.Application.CS5")
Set mySelection = myInDesign.Selection
If TypeName(mySelection.Item(1)) = "EPS" Then
mySelection.Parent.RotationAngle = 30
Else
mySelection.RotationAngle = 30
End IfYou might satisfy your adventurous streak by substituting the statement mySelection. RotationAngle = 30 in the preceding script with each of the following statements:
set the color of the current box to "Blue" set the shade of the current box to 50 set the width of the frame of the current box to 10 set the box shape of the current box to ovular
Go to town if you want: Combine all the set statements into a single script so that you can use the script to make all the changes simultaneously.
|
2. Labeling items
As you can see from the examples in the previous section, scripts often refer to items by their type and location in the document. But there's another way to refer to objects that makes sure you can select an item precisely: You can label, or name, an item. You do so in the Script Label panel (choose Window => Utilities => Script Label). The process is easy: Select the object and then type a name in the panel. That's it!
When writing scripts, you refer to the labeled object as follows. In these examples, the label is TargetFrame, and don't worry that the samples seem to do different things — they in fact are unrelated examples, not variations of the same command.
2.1. JavaScript
with(app.docum ents.item(0).pages.item(0)){
myTargetFrame = textFrames.item("myTargetFrame");
}2.2. AppleScript
select (page item 1 of page 1 of myTargetDocument whose ⌝ label is "TargetFrame")
2.3. VBA
Set myAsset = myLibrary.Assets.Item("TargetFrame")3. Writing conditio nal scripts
Some scripts simply automate a set of tasks in documents whose content is predictable. But more often than not, documents differ, so you need conditional statements to evaluate certain things to see whether they are true before applying a script's actions. Otherwise, you get an error message when something turns out not to be true. As a simple example, a script that does a search and replace needs to have a document open and a frame selected. If no frame is selected, the script won't know what to search, and the user gets an error message.
The same issue arises for repeated series of actions, where you want the script to do something for all occurrences. The script needs to know what to do when it can't find any more such occurrences. As an example, look at the following script, which counts all open documents. For it to work, at least one document has to be open, so the script checks first to see whether in fact any documents are open and then delivers an error message that the user can understand if none is open. The rotate-EPS-graphic script earlier also used a conditional to make sure there was an EPS graphic in the document. Notice that in all three scripting languages, you use the command if to set up such conditionals.
3.1. JavaScript
if (app.document s.length==0){
alert("No InDesign documents are open!"); }NOTE
JavaScript uses == for comparing values (as in the example above) and = for assigning values. Visual Basic and AppleScript use = for both purposes.
3.2. AppleScript
tell application "Adobe InDesign CS5" activate set myNumberOfDocuments to (count documents) if myNumberOfDocuments = 0 then display dialog "No InDesign publications are open!" end if end tell
3.3. VBA
Dim myInDesign as InDesign.Application
Set myInDesign = CreateObject ("InDesign.Application.CS4")
If myInDesign.Documents.Count
MsgBox "No InDesign publications are open!"
End If
End SubAnother form of conditional is what's called a control loop, in which an action occurs either for a specified number of iterations or until a condition is met. The following scripts show an example of each for each language. Note the use of comments in the scripts — a handy way to document what you're doing for later reference. In JavaScript, a single-line comment begins with //, whereas a multiline comment begins with /* and ends with */. In AppleScript, a comment begins with --and continues until you press Enter or Return. In VBA, it begins with Rem followed by a space, and it continues until you press Enter or Return.
3.4. JavaScript
for (var myCounter = 0; myCounter < 20; myCounter++){
//do something
}
while (myStop == false){
/* do something, at some point setting myStop to true
to leave the loop. */
}3.5. AppleScript
repeat with counter from 1 to 20 --do something end repeat set myStop to false repeat while myStop = false --do something, at some point setting myStop to true to ⌝ leave the loop. end repeat
3.6. VBA
For counter = 1 to 20 Rem do something Next counter Do While myStop = false Rem do something, at some point setting myStop to true ⌝ to leave the loop. loop