0 Replies Latest reply on Mar 24, 2015 12:31 AM by elmer_banate

    Create XML Element Tag BasedOn Found Character Style

    elmer_banate

      Hi,

       

      Good Day!

       

      Basically I want to search only for character styles that contains "ntb-", that is why it is hard coded in my searchString variable.

       

      Although I was able to create XML tag based on selected character style, but it will always add/insert at the end of the parent XML element.

       

      Can anyone could help me how insert/add that element at the insertion point where the text is found?

       

      Thanks,

      --elmer

       

       

      var myDoc = app.documents[0];

      myDoc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.POINTS;

      myDoc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.POINTS;

      var charStyles = myDoc.allCharacterStyles;

      var foundStyles = Array();

      var searchString = String();

          searchString = 'ntb-';

      var tempName = String();

      var tempName1 = String();

      for(var i = 1; charStyles.length > i; i++){

         tempName = charStyles[i].name;

         tempName1 = tempName.toLowerCase();

         if(tempName1.indexOf (searchString.toLowerCase()) != -1){

             foundStyles.push (tempName);

         }

      }

      var myDialog = app.dialogs.add({name: "Convert Footnote Character Styles to XML Tags",canCancel:true});

      with (myDialog) {

          with (dialogColumns.add().borderPanels.add()) {

              with (dialogColumns.add()) {

                  staticTexts.add({staticLabel: "Character style to search : "});        }

              with (dialogColumns.add()) {

                  selCharStyle = dropdowns.add({stringList: foundStyles, selectedIndex: 0, minWidth: 175});

              }

          }

      }

       

      var dialogShown = myDialog.show();

       

      while (dialogShown) {

          if (selCharStyle.selectedIndex == 0) {

              alert("Must have at least a character style to search!");

              dialogShown = myDialog.show();

              continue;

          } else {

              insertXMLTags(selCharStyle.stringList[selCharStyle.selectedIndex], selCharStyle.selectedIndex, "0");

              break;

          }

      }

       

      myDialog.destroy();

      alert("Finished!");

      exit();

       

      function insertXMLTags(cStyle, cStyleIndex, ip) {

          var myFinds = searchStyle(cStyle, cStyleIndex);

          for (i=myFinds.length - 1; i>=0; i--) {

              var myIP = myFinds[i].texts.item(0).insertionPoints.item(0);

              var myParentXML = myIP.associatedXMLElements[0];

              var myChiidXML = myParentXML.xmlElements.add ( cStyle );

              myChiidXML.contents =  myFinds[i].contents;

              //set the selection

              app.selection = myFinds[i].texts.item(0);

              //remove the selected text

              myFinds[i].texts.item(0).remove();

          }

      }

       

      /*--------------------------------

              Utility Functions         

      ----------------------------------*/

      function searchStyle(cStyle, cStyleIndex){

       

          var myFinds;

          // if script version is for Indesign CS2

          if (app.scriptPreferences.version < 5){                                 

              app.findPreferences = NothingEnum.nothing;

              app.changePreferences = NothingEnum.nothing;

              myFinds = myDoc.search(undefined, undefined, undefined, undefined,

              {appliedParagraphStyle: pStyle, appliedCharacterStyle: cStyle});

                                                                   

          // else, for CS3 and CS4

          }else{                                 

              //Clear any existing find/change settings

              app.findTextPreferences = NothingEnum.nothing;

              app.changeTextPreferences = NothingEnum.nothing;

           

              // set character or paragraph style to search

              if (cStyleIndex != 0){ 

                  app.findTextPreferences.appliedCharacterStyle = cStyle;

              }

                                           

              //Set the find options.

              app.findChangeTextOptions.caseSensitive = false;

              app.findChangeTextOptions.includeFootnotes = true;

              app.findChangeTextOptions.includeHiddenLayers = false;

              app.findChangeTextOptions.includeLockedLayersForFind = false;

              app.findChangeTextOptions.includeLockedStoriesForFind = false;

              app.findChangeTextOptions.includeMasterPages = false;

              app.findChangeTextOptions.wholeWord = false;                             

              myFinds = myDoc.findText();

           

          }

       

          return myFinds;

      }