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> Adding features to “Hello World” 27<br />

VBScript<br />

1. Enter the following JavaScript in a new text file:<br />

//Improved Hello World!<br />

//Enter the name of a font on your system, if necessary.<br />

try{<br />

myFont = app.fonts.item("Arial");<br />

}<br />

catch (myError){};<br />

var myDocument = app.documents.item(0);<br />

with(myDocument){<br />

var myPage = pages.item(0);<br />

var myBounds = myGetBounds(myPage,myDocument);<br />

with(myDocument.pages.item(0)){<br />

//Get a reference to the text frame.<br />

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

//Change the size of the text frame.<br />

myTextFrame.geometricBounds = myBounds;<br />

var myParagraph = myTextFrame.paragraphs.item(0);<br />

myParagraph.appliedFont = myFont;<br />

myParagraph.justification = Justification.centerAlign;<br />

myParagraph.pointSize = 48;<br />

}<br />

}<br />

//myGetBounds is a function that returns the bounds<br />

//of the "live area" of a page.<br />

function myGetBounds(myDocument, myPage){<br />

var myPageWidth = myDocument.documentPreferences.pageWidth;<br />

var myPageHeight = myDocument.documentPreferences.pageHeight<br />

if(myPage.side == PageSideOptions.leftHand){<br />

var myX2 = myPage.marginPreferences.left;<br />

var myX1 = myPage.marginPreferences.right;<br />

}<br />

else{<br />

var myX1 = myPage.marginPreferences.left;<br />

var myX2 = myPage.marginPreferences.right;<br />

}<br />

var myY1 = myPage.marginPreferences.top;<br />

var myX2 = myPageWidth - myX2;<br />

var myY2 = myPageHeight - myPage.marginPreferences.bottom;<br />

return [myY1, myX1, myY2, myX2];<br />

}<br />

2. Save the script as a plain text file with the file extension .jsx in the Scripts Panel folder (see “Installing<br />

scripts” on page 6).<br />

Once you’ve opened or created the script file, you can run the script from the ExtendScript Toolkit or from<br />

the <strong>InDesign</strong> Scripts panel.<br />

Make sure you have the Hello World document open—this script relies on objects we created in that<br />

script. If you closed the document without saving it, simply run the HelloWorld.vbs script again to make<br />

a new document.

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

Saved successfully!

Ooh no, something went wrong!