12.01.2013 Views

Adobe InDesign CS5 Scripting Tutorial

Adobe InDesign CS5 Scripting Tutorial

Adobe InDesign CS5 Scripting Tutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Adobe</strong> <strong>InDesign</strong> <strong>CS5</strong> <strong>Scripting</strong> <strong>Tutorial</strong> <strong>Scripting</strong> and the <strong>InDesign</strong> object model 13<br />

JavaScript //To convert from a number to a string:<br />

myNumber = 2;<br />

myString = myNumber + "";<br />

//To convert from a string to an integer:<br />

myString = "2";<br />

myNumber = parseInt(myString);<br />

//If your string contains a decimal value, use "parseFloat" rather than "parseInt":<br />

myNumber = parseFloat(myString);<br />

//You can also convert strings to numbers using the following:<br />

myNumber = +myString;<br />

VBScript Rem To convert from a number to a string:<br />

myNumber = 2<br />

myString = cstr(myNumber)<br />

Rem To convert from a string to an integer:<br />

myString = "2"<br />

myNumber = cInt(myString)<br />

Rem If your string contains a decimal value, use "cDbl" rather than "cInt":<br />

myNumber = cDbl(myString)<br />

Variables<br />

A variable is a container for a value. They are called “variables” because the values they contain might<br />

change. A variable might hold a number, a string of text, or a reference to an <strong>InDesign</strong> object. Variables<br />

have names, and you refer to a variable by its name. To put a value into a variable, you assign the data to<br />

the variable.<br />

In our first sample script, above, we used the variables myDocument and myTextFrame to avoid having to<br />

provide the full specification of the object (such as text frame 1 of page 1 of document 1 or<br />

app.documents.item(0).pages.item(0).textFrames.item(0)) every time we refer to the object.<br />

In all the sample and tutorial scripts that come with <strong>InDesign</strong>, all variables start with my. This way, you can<br />

easily differentiate variables we created in a script from the scripting language terms.<br />

Assigning a value to a variable<br />

Assigning values or strings to variables is fairly simple, as shown in the following table:<br />

Language Examples of assigning a value to a variable<br />

AppleScript set myNumber to 10<br />

set myString to "Hello, World!"<br />

set myTextFrame to make text frame at page 1 of myDocument<br />

JavaScript var myNumber = 10;<br />

VBScript myNumber = 10<br />

var myString = "Hello, World!";<br />

var myTextFrame = myDocument.pages.item(0).textFrames.add();<br />

myString = "Hello, World!"<br />

Set myTextFrame = myDocument.Pages.Item(1).TextFrames.Add

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!