1 Reply Latest reply on Jun 9, 2010 3:38 PM by Peter Kahrel

    Solve Character Style shifting?

    pathblaster Level 1

      I am using a script in InDesign CS3 and CS4 that places letter headings into an index listing. It works mostly fine, except for one thing: Where there is bold character styling on a paragraph that the index heading is taken from, the addition of the Initial Letter and the return character makes the bold styling shift to the left two characters, leaving the last two characters that were bold now unbolded. It's like having some marbles filling in a tube, you put two marbles in on the left end and two marbles come out on the right end!


      I've been scratching my head to figure how this may be preempted or fixed, but I am not familiar enough with JavaScript to understand how, or what is really triggering this behavior. Any ideas would be greatly appreciated!


      // Add section headings to index

      // Keith Gilbert

      // Modified 07/30/08


      #target indesign-5.0

      var userRedrawSetting = app.scriptPreferences.enableRedraw;

      app.scriptPreferences.enableRedraw = false;


      // Check to see whether any InDesign documents are open.

      // If no documents are open, display an error message.

      if (app.documents.length > 0){

      var myDoc = app.activeDocument;

      //If the selection contains more than one item, the selection

      //is not text selected with the Type tool.

      if (app.selection.length == 1){

      //Evaluate the selection based on its type.

      switch (app.selection[0].constructor.name){

      case "InsertionPoint":

      case "Character":

      case "Word":

      case "TextStyleRange":

      case "Line":

      case "Paragraph":

      case "TextColumn":

      case "Text":

      case "Story":

      //The object is a text object; pass it on to a function.



      case "TextFrame":


      alert("The selected object is not a text object. Select some text and try again.");





      alert("Please select some text and try again.");



      else {

      // No documents are open, so display an error message.

      alert("No InDesign documents are open. Please open a document and try again.")



      app.scriptPreferences.enableRedraw = userRedrawSetting;



      function myProcessText(myStory) {

      for (var myParagraphCounter = myStory.paragraphs.length-1; myParagraphCounter >= 0; myParagraphCounter --) {

      paraA = myStory.paragraphs.item(myParagraphCounter-1).contents;

      paraB = myStory.paragraphs.item(myParagraphCounter).contents;

      // compare the first character of each paragraph

      if (paraA.toLowerCase().charAt(0) != paraB.toLowerCase().charAt()) {

      // the first character of each paragraph is different, so insert a section head between them

      myStory.paragraphs.item(myParagraphCounter).contents = paraB.toLowerCase().charAt()+ "\r" + paraB;

      // Create Index section heading paragraph style

      try {

      var myParaStyle = myDoc.paragraphStyles.add({name:"Index section heading"});


      catch (myError){} // Index section heading paragraph style already exists

      // format the section heading with the Index section heading paragraph style

      myStory.paragraphs.item(myParagraphCounter).appliedCharacterStyle = app.activeDocument.allCharacterStyles[0];

      myStory.paragraphs.item(myParagraphCounter).appliedParagraphStyle = "Index section heading";





      Thanks for your feedback!