2 Replies Latest reply on Sep 1, 2016 10:24 AM by borise78101190

    insertTextAsContent and apply characterStyle

    borise78101190 Level 1

      Hallo!

       

      I am working on Importing a XML-file into InDesign CC.

      After some transformation with XSL I do some scripting with Javascript.

       

      What I want to achieve is adding a label to some elements.

      e.g. <element type="phone">+123 456 789</element>

      in InDesign it should look like

      Phone: +123 456 789

       

      My Javascript

      function AddPhoneLabel(){

          this.name = "AddPhoneLabel";

          var myLabel = 'Phone: ';

          myLabel.appliedCharacterStyle = "bold";

       

          //Match the Ingredients elements.

          this.xpath = "//communication[@type = 'telephone']";

          this.apply = function(myElement, myRuleProcessor){

          with(myElement){

            insertTextAsContent(myLabel, XMLElementPosition.ELEMENT_START);

          }

          return false;

          } }

          }

       

      insertTextAsContent uses a string, but I have no clue about getting this done.

       

      Any hint is more than appreciated!

       

      Boris

       

        • 1. Re: insertTextAsContent and apply characterStyle
          csm_phil Level 4

          Hi borise78101190,

           

          Try the below code.

           

           

          function AddPhoneLabel(){  
              var myLabel = 'Phone: ';
              this.name = "AddPhoneLabel";  
              this.xpath = "//communication[@type = 'telephone']";
              this.apply = function(myElement, myRuleProcessor){  
                  with(myElement){  
                      var myAttribute = myElement.xmlContent;
                      myAttribute.insertionPoints[0].contents  = String(myLabel);
                      myAttribute.words[0].appliedCharacterStyle = app.activeDocument.characterStyles.item("bold");
                      }  
                  return true;  
                  }  
              }  
          

           

          thx,

          csm_phil

          • 2. Re: insertTextAsContent and apply characterStyle
            borise78101190 Level 1

            Hallo csm_phil,

             

            works like a charme.

            Thank you very much for your help.

             

            Best regards,

            Boris