2 Replies Latest reply on Apr 17, 2011 12:05 PM by rphh

    Script won't apply paragraph styling


      Hi there,


      I'm am new to scripting in InDesign (CS3), and I'm trying to figure out why the scipt below won't apply styling to the following text file?:




      <ParaStyle:Heading 1>Who we are?<ParaStyle:>



      For ease of reading, I've shortend the text file to just two lines.

      The script I am using to open and format the text file is:




      //Create a new document.


      var myDocument = app.documents.add();

      //Set the measurement units and ruler origin.

      myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;

      myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;

      myDocument.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;

      //Get a reference to the first master spread.

      var myMasterSpread = myDocument.masterSpreads.item(0);

      //Get a reference to the margin preferences of the first page in the master spread.

      var myMarginPreferences = myMasterSpread.pages.item(0).marginPreferences;

      //Now set up the page margins and columns.

      myMarginPreferences.left = 4;

      myMarginPreferences.top = 70;

      myMarginPreferences.right = 70;

      myMarginPreferences.bottom = 78;

      myMarginPreferences.columnCount = 2;

      myMarginPreferences.columnGutter = 14;

      //Page margins and columns for the right-hand page.

      var myMarginPreferences = myMasterSpread.pages.item(1).marginPreferences;

      myMarginPreferences.left = 48;

      myMarginPreferences.top = 70;

      myMarginPreferences.right = 70;

      myMarginPreferences.bottom = 78;

      myMarginPreferences.columnCount = 3;

      myMarginPreferences.columnGutter = 14;


      // Adding Master Page Items


      var myMasterSpread = myDocument.masterSpreads.item(0);

      var myLeftPage = myMasterSpread.pages.item(0);

      var myRightPage = myMasterSpread.pages.item(1);

      var myLeftFooter = myLeftPage.textFrames.add();

      myLeftFooter.geometricBounds = [728, 70, 742, 528];

      myLeftFooter.textFramePreferences.firstBaselineOffset = FirstBaseline.leadingOffset;

      myLeftFooter.contents = SpecialCharacters.autoPageNumber;

      myLeftFooter.parentStory.characters.item(0).pointSize = 11;

      myLeftFooter.parentStory.characters.item(0).leading = 14;

      var myRightFooter = myRightPage.textFrames.add();

      myRightFooter.geometricBounds = [728, 48, 742, 542];

      myRightFooter.textFramePreferences.firstBaselineOffset = FirstBaseline.leadingOffset;

      myRightFooter.contents = SpecialCharacters.autoPageNumber;

      myRightFooter.parentStory.characters.item(0).pointSize = 11;

      myRightFooter.parentStory.characters.item(0).leading = 14;

      myRightFooter.parentStory.characters.item(0).justification = Justification.rightAlign;


      // Adding Master Text Frames


      var myLeftPage = myMasterSpread.pages.item(0);

      var myRightPage = myMasterSpread.pages.item(1);

      var myLeftTextFrame = myLeftPage.textFrames.add();

      myLeftTextFrame.geometricBounds = [70, 70, 714, 528];

      myLeftTextFrame.textFramePreferences.firstBaselineOffset = FirstBaseline.leadingOffset;

      myLeftTextFrame.textFramePreferences.textColumnCount = 3;

      myLeftTextFrame.textFramePreferences.textColumnGutter = 14;

      //Add a label to make the frame easier to find later on.

      myLeftTextFrame.label = "BodyTextFrame";

      var myRightTextFrame = myRightPage.textFrames.add();

      myRightTextFrame.geometricBounds = [70, 48, 714, 542];

      myRightTextFrame.textFramePreferences.firstBaselineOffset = FirstBaseline.leadingOffset;

      myRightTextFrame.textFramePreferences.textColumnCount = 3;

      myRightTextFrame.textFramePreferences.textColumnGutter = 14;

      //Add a label to make the frame easier to find later on.

      myRightTextFrame.label = "BodyTextFrame";

      //Link the two frames using the nextTextFrame property.

      myLeftTextFrame.nextTextFrame = myRightTextFrame;


      // Overriding Master Page Items and Add Text


      var myTextFrame = myDocument.masterSpreads.item(0).pages.item(1).textFrames.item(0).override(myDocument.pag es.item(0));

      //Add text by setting the contents of an insertion point to a string.

      //In JavaScript, "\r" is a return character.

      myTextFrame.insertionPoints.item(0).contents = "dok\r";


      // Adding and Applying a Paragraph Style


      var myParagraphStyle = myDocument.paragraphStyles.item("Heading 1");

      try {

      var myName = myParagraphStyle.name;


      catch (myError){

      //The paragraph style did not exist, so create it.

      myParagraphStyle = myDocument.paragraphStyles.add({name:"Heading 1"});


      //We'll need to create a color. Check to see if the color already exists.

      var myColor = myDocument.colors.item("Green");

      try {

      myName = myColor.name;


      catch (myError){

      //The color did not exist, so create it.

      myColor = myDocument.colors.add({name:"Green", model:ColorModel.process, colorValue:[100,0,100,0]});


      //Now set the formatting of the paragraph style.

      myParagraphStyle.appliedFont = "Arial";

      myParagraphStyle.fontStyle = "Bold";

      myParagraphStyle.pointSize = 24;

      myParagraphStyle.spaceAfter = 24;

      myParagraphStyle.spaceBefore = 24;

      myParagraphStyle.fillColor = myDocument.colors.item("Green");

      //Apply the style to the paragraph.

      myDocument.pages.item(0).textFrames.item(0).paragraphs.item(0).applyParagraphStyle(myParag raphStyle, true);

      //You could also use:

      //myDocument.pages.item(0).textFrames.item(0).paragraphs.item(0).appliedParagraphStyle = //myParagraphStyle;



      // Import a Text File


      //Display a standard open file dialog box to select a text file.

      var myTextFile = File.openDialog("Choose a text file");

      //If a text file was selected, and if you didn't press Cancel,

      //place the text file at the first insertion point after the headline.

      if((myTextFile != "")&&(myTextFile != null)){




      // Placing a Graphic


      //Display a standard open file dialog box to select a graphic file.

      var myGraphicFile = File.openDialog("Choose a graphic file");

      //If a graphic file was selected, and if you didn't press Cancel,

      //place the graphic file on the page.

      if((myGraphicFile != "")&&(myGraphicFile != null)){

      var myGraphic = myDocument.pages.item(0).place(myGraphicFile);

      //Since you can place multiple graphics at once, the place method

      //returns an array. To get the graphic you placed, get the first

      //item in the array (JavaScript arrays start with item 0).

      myGraphic = myGraphic[0];

      //Create an object style to apply to the graphic frame.

      var myObjectStyle = myDocument.objectStyles.item("GraphicFrame");

      try {

      var myName = myObjectStyle.name;


      catch (myError){

      //The object style did not exist, so create it.

      myObjectStyle = myDocument.objectStyles.add({name:"GraphicFrame"});


      myObjectStyle.enableStroke = true;

      myObjectStyle.strokeWeight = 3;

      myObjectStyle.strokeType = myDocument.strokeStyles.item("Solid");

      myObjectStyle.strokeColor = myDocument.colors.item("Green");

      //The frame containing the graphic is the parent of the graphic.

      var myFrame = myGraphic.parent;

      myFrame.applyObjectStyle(myObjectStyle, true);

      //Resize the frame to a specific size.

      myFrame.geometricBounds = [0,0,600,600];

      //Fit the graphic to the frame proportionally.


      //Next, fit frame to the resized graphic.


      var myBounds = myFrame.geometricBounds;

      var myGraphicWidth = myBounds[3]-myBounds[1];

      //Move the graphic frame.

      var myPageWidth = myDocument.documentPreferences.pageWidth;

      var myTopMargin = myDocument.pages.item(0).marginPreferences.top;

      myFrame.move([myPageWidth-myGraphicWidth, myTopMargin]);

      //Apply a text wrap to the graphic frame.

      myFrame.textWrapPreferences.textWrapType = TextWrapTypes.boundingBoxTextWrap;

      myFrame.textWrapPreferences.textWrapOffset = [24, 12, 24, 12];




      I hope someone can point me in the right direction.
      Many thanks in advance.
        • 1. Re: Script won't apply paragraph styling
          John Hawkinson Level 5

          Hi, Ron.


          The problem has nothing to do with your script. You can see this if you try to place the indesign tagged text file by hand.


          Your instinct to divide and conquer for debugging is a good one -- having a short text file to work with is very helpful -- of course that's true of the script too.


          When posting scripts on the forum, please do use the Insert Syntax Highlighting > Java feature that is behind the >> arrow in the toolbar. Otherwise cutting and pasting your script is difficult requiring manual fixups.


          Anyhow, your problem is that every paragraph can only have a single paragraph style, and it is the last one you set.

          So in your IDTT file, you set the paragraph style to Heading 1, enter the contents of the paragraph, and then set the paragraph style to none.

          If you included a line break before setting the paragraph style to none, then you would set the style to none for the next paragraph. But since you don't, it just resets the style for the existing paragraph.


          In short: hit return before <ParaStyle:>.

          • 2. Re: Script won't apply paragraph styling
            rphh Level 1

            Hi John


            Thanks for your prompt response to my query.

            Works like a treat now.