mercredi 21 janvier 2015

InDesign script to generate text in each page's frame issue


Learning about InDesign scripting I face an issue with trying to loop through an InDesign's indd file and add text to every page's textframe the associated page number.


After referencing many sources, including the Adobe forums and a few books I have on InDesign scripting I am still not able to produce a successful script.


The code:



var theDoc = fileChosenByUser;
var allPages = app.documents[0].pages.everyItem().getElements();

for ( var x = allPages.length-1; x >= 0; x-- ) {
if ( allPages.textFrames.length > 0 ) {
app.select( theDoc[allPages].textFrames[0].insertionPoints[0] );
app.selection[0].contents = "The page name is " + theDoc[allPages].name;
}
}


fileChosenByUser is a result of another part of the script that determines if the file being opened is from Windows or Mac.


allPages trying to count all the pages in the document


After researching I found that in InDesign it could error when using an incrementing for loop so I decided to use a decrementing for loop: for ( var x = allPages.length-1; x >= 0; x-- ) {}


If statement to make sure that the page count has not reached zero: if ( allPages.textFrames.length > 0 ) {}


Trying to select the page's textframe located in the upper left corner: app.select( theDoc[allPages].textFrames[0].insertionPoints[0] );


Adding what I want included in that page: app.selection[0].contents = "The page name is " + theDoc[allPages].name;


The error I am getting: Error String: undefined is not an Object Source: if ( allPages.textFrames.length > 0 ) {





Aucun commentaire:

Enregistrer un commentaire