3 Replies Latest reply on Jun 20, 2008 10:16 AM by (Olav_Kvern)

    [JS - CS3]  Not able to add 'Superscript' style to a character within a string

    Level 1
      Hello fellow experts...

      I'm stuck with trying to change the style of a character from Normal to Superscript!

      b Situation:

      I have a string - 'myCourseTitle' - that has both CharacterStyles & ParagraphStyles applyed and could include the following character/Word:
      > '®' (Character)

      'OperateIT' (Word)

      b Requirements:

      I am wanting to add the style 'Superscript' to both the '®' characters and 'IT' from the words 'OperateIT', while keeping their initial CharacterStyles & ParagraphStyles!

      b My Starting Block:

      if (myCourseTitleField.editContents == ""){
      var myCourseTitle = "-no title has been defined-";
      }else{
      var myCourseTitle = myCourseTitleField.editContents;
      // The contents should now be checked for '®' characters and 'OperateIT'
      // And set to Superscript if found!
      if (myCourseTitle.indexOf("®") != 0){
      alert("Registered Trade Mark '®' found in Course Title at position:"+myCourseTitle.indexOf("®")+"\rThis will be set to 'Superscript' formatting", "WARNING '®' within Course Title",)
      }
      }

      I have tried many scripts, including the attached sample 'FindChangeByList.jsx' script - but to no avail!
      Can anyone help me - point me in the right direction to start looking?

      Thanks in advance
      ,
      Lee
        • 1. Re: [JS - CS3]  Not able to add 'Superscript' style to a character within a string
          Level 1
          Hi Lee,

          In the example, you're trying to apply InDesign formatting to a JavaScript string (from an InDesign dialog box text field). That won't work, because the JavaScript string doesn't know anything about InDesign text objects.

          I'm assuming, however, that what you want is to change the formatting of the text on your page. To do that, you can use the findText method on the text, story, or document. Here's an example (the relevant part is the "mySnippet" function--the rest is just setting up an example):

          main();
          
          function main(){
          mySetup();
          mySnippet();
          }
          function mySnippet(){
          //Clear find text preferences.
          app.findTextPreferences = NothingEnum.nothing;
          app.changeTextPreferences = NothingEnum.nothing;
          app.findTextPreferences.findWhat = "®";
          app.changeTextPreferences.appliedCharacterStyle = app.documents.item(0).characterStyles.item("superscript");
          app.documents.item(0).changeText();
          //Reset find/change text preferences.
          app.findTextPreferences = NothingEnum.nothing;
          app.changeTextPreferences = NothingEnum.nothing;
          //Reset find/change GREP preferences.
          app.findGrepPreferences = NothingEnum.nothing;
          app.changeGrepPreferences = NothingEnum.nothing;
          //There's probably a way to do this in a single pass, but I'm short on time...
          app.findGrepPreferences.findWhat = "\\l(?<=)IT";
          app.changeGrepPreferences.appliedCharacterStyle = app.documents.item(0).characterStyles.item("superscript");
          app.documents.item(0).changeGrep();
          app.findGrepPreferences.findWhat = "\\l";
          app.findGrepPreferences.appliedCharacterStyle = app.documents.item(0).characterStyles.item("superscript");
          app.changeGrepPreferences.appliedCharacterStyle = app.documents.item(0).characterStyles.item("[None]");
          app.changeGrepPreferences.position = Position.normal;
          app.documents.item(0).changeGrep();
          app.findGrepPreferences = NothingEnum.nothing;
          app.changeGrepPreferences = NothingEnum.nothing;
          }
          //mySetup simply takes care of setting up the example document.
          function mySetup(){
          var myDocument = app.documents.add();
              var myPage = app.activeWindow.activePage;
          //Create a text frame on page 1.
          var myTextFrame = myPage.textFrames.add();
          //Set the bounds of the text frame.
          myTextFrame.geometricBounds = myGetBounds(myDocument, myPage);
          //Fill the text frame with placeholder text.
          myTextFrame.contents = TextFrameContents.placeholderText;
          myTextFrame.insertionPoints.item(0).contents = "OperateIT®\r";
          myTextFrame.paragraphs.item(-1).insertionPoints.item(0).contents  = "OperateIT®\r";
          var myHeadingStyle = myDocument.paragraphStyles.add({name:"heading"});
          var mySuperscriptStyle = myDocument.characterStyles.add({name:"superscript", position:Position.superscript});
          }
          function myGetBounds(myDocument, myPage){
          var myPageWidth = myDocument.documentPreferences.pageWidth;
          var myPageHeight = myDocument.documentPreferences.pageHeight
          if(myPage.side == PageSideOptions.leftHand){
            var myX2 = myPage.marginPreferences.left;
            var myX1 = myPage.marginPreferences.right;
          }
          else{
            var myX1 = myPage.marginPreferences.left;
            var myX2 = myPage.marginPreferences.right;
          }
          var myY1 = myPage.marginPreferences.top;
          var myX2 = myPageWidth - myX2;
          var myY2 = myPageHeight - myPage.marginPreferences.bottom;
          return [myY1, myX1, myY2, myX2];
          }


          Thanks,

          Ole
          • 2. Re: [JS - CS3]  Not able to add 'Superscript' style to a character within a string
            Level 1
            Thanks for the detailed scripting.

            I have tried your script and it seems to work fine on a normal document (new blank file) or the 'mySetup' from yourself...

            ...but...

            unfortunatly when I apply the 'mySnippet' function onto my document I get the following error:
            > Error Number: 30477
            Error String: Invalid value for set property 'appliedCharacterStyle', Expected String, CharacterStyle or Nothing.......

            If I copy the text out of my document and pasted it into a blank new file it works????
            BUT - the origional 'Character Styling' has been changed to the newly added 'superscript' Character Style!
            This isn't good for me as I need the text to keep the origional Character Style - it is used (Character Style) to search for in other scripts, so when I change the style - the search will not find it any more!
            :-(

            What is happening - and why does he not like the formatting im my Document?

            Thanks in advance.
            Lee
            • 3. Re: [JS - CS3]  Not able to add 'Superscript' style to a character within a string
              Level 1
              Hi Lee,

              Ah, I see. Here's a new "mySnippet":

              function mySnippet(){
              
              //Clear find text preferences.
              app.findTextPreferences = NothingEnum.nothing;
              app.changeTextPreferences = NothingEnum.nothing;
              app.findTextPreferences.findWhat = "®";
              app.changeTextPreferences.appliedCharacterStyle = app.documents.item(0).characterStyles.item("superscript");
              app.documents.item(0).changeText();
              //Reset find/change text preferences.
              app.findTextPreferences = NothingEnum.nothing;
              app.changeTextPreferences = NothingEnum.nothing;
              //Reset find/change GREP preferences.
              app.findGrepPreferences = NothingEnum.nothing;
              app.changeGrepPreferences = NothingEnum.nothing;
              //There's probably a way to do this in a single pass, but I'm short on time...
              //First step: change any lowercase character followed by "IT" to superscript.
              app.findGrepPreferences.findWhat = "\\l(?lt;=)IT";
              app.changeGrepPreferences.position = Position.superscript;
              app.documents.item(0).changeGrep();
              //Second step: change any lowercase character formatted as superscript to normal.
              app.findGrepPreferences.findWhat = "\\l";
              app.changeGrepPreferences.position = Position.normal;
              app.documents.item(0).changeGrep();
              app.findGrepPreferences = NothingEnum.nothing;
              app.changeGrepPreferences = NothingEnum.nothing;
              }

              Thanks,

              Ole