Creating and Running Scripts


At this point, I'm assuming that the appropriate scripting software is installed on your computer. If this is the case, you're ready to begin. For your first trick, you're going to make InDesign roll over ‚ sort of. Actually, you're going to rotate an EPS graphic. First, you'll prepare InDesign for its role. Launch the program, and then create a new document (do not check Automatic Text Box). In the middle of the first page, place an EPS graphic. Make sure that it remains active after you place it.

Writing simple scripts

The following three scripts, taken from Adobe's InDesign script examples, do the same thing in AppleScript and VBA: Rotate an EPS graphic and its frame. (For JavaScript versions, go to the companion Web site at www.INDDcentral.com .)

Cross-Reference ‚  

You can get the example scripts shown in this chapter, as well as other samples, from the PDF scripting manual that comes on the InDesign CS CD. Just cut and paste them from the PDF file into the appropriate scripting editor. Adobe's user forums are also a good place to go for scripting help.

Enter the lines that follow this paragraph exactly as they're written for the scripting language you've chosen . Enter 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 enter the text: Typos are script killers.

AppleScript

 tell application "InDesign CS"   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 tell 
Note ‚  

Make sure to enter the name of your InDesign program exactly as it appears on the desktop. 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 in an adventurous mood, try substituting the statement set rotation angle of myFrame to 30 in the preceding script with each of the following statements:

 set text wrap of myFrame to off set shear angle of myFrame to 30 set vertical scale of myFrame to 200 

If you want to get really fancy, combine all the set statements into a single script, so you can use the script to make all the changes at once.

VBA

 Dim myInDesign As InDesign.Application Set myInDesign = CreateObject("InDesign.Application.CS") Set mySelection = myInDesign.Selection If TypeName(mySelection.Item(1)) = "EPS" Then   mySelection.Parent.RotationAngle = 30 Else   mySelection.RotationAngle = 30 End If 

If you're in an adventurous mood, try 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 

If you want to get really fancy, combine all the set statements into a single script, so you can use the script to make all the changes at once.

Note ‚  

Perhaps you noticed the chain of command used in the preceding scripts. First, the script addresses InDesign, then the active document (layout), and finally the active frame. If you understand this concept, you'll be scripting like a pro in no time.

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 pane (Window Scripting Script Label). The process is easy: Select the object, then enter a name in the pane. 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.

AppleScript

 select (page item 1 of page 1 of myTargetDocument whose label  is "TargetFrame") 

VBA

 Set myAsset = myLibrary.Assets.Item("TargetFrame") 

Writing conditional scripts

Some scripts simply automate a set of tasks in documents whose content is predictable. But more often than not, documents differ , and so you need conditional statements to evaluate if certain things are true before applying a script's actions. Otherwise, you'll 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 will get 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 will need 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 if in fact any documents are open, and delivers an error message that the user can understand if none are 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, it is the command If that you use to set up such conditionals.

AppleScript

 tell application "InDesign CS" set myNumberOfDocuments to (count documents) if myNumberOfDocuments = 0 then display dialog "No InDesign publications are open!" end if end tell 

VBA

 Dim myInDesign as InDesign.Application   Set myInDesign = CreateObject ("InDesign.Application.CS")   If myInDesign.Documents.Count     MsgBox "No InDesign publications are open!"   End If End Sub 

Another 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 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.

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 

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 



Adobe InDesign CS Bible
Adobe InDesign CS3 Bible
ISBN: 0470119381
EAN: 2147483647
Year: 2003
Pages: 344
Authors: Galen Gruman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net