3 Replies Latest reply on May 18, 2016 5:35 PM by LSH_246

    Find a superscript only if followed by a "normal position" char but not a space!

    Obi-wan Kenobi Adobe Community Professional

      Hi All,

       

      All is in the topic title!

      I'm trying to do it with a [JS].

      There are missing elements as defining the "normal position" of a character!

       

      main();
      
      
      function main() {
      
        var myStyle = "Superscript";
        var myDoc = app.activeDocument;
      
        app.findGrepPreferences = app.changeGrepPreferences = null;  
      
      
        app.findGrepPreferences.appliedCharacterStyle = myStyle;   
        app.findGrepPreferences.findWhat = ".+";   
        var allFounds = myDoc.findGrep(true); 
      
      
        for ( var i = 0; i < allFounds.length; i++ ) {
        var myFound = allFounds[i];
        var firstCharIndex = myFound.characters[0].index;
        var followingChar = myFound.parentStory.characters[firstCharIndex+1];
        if ( followingChar = "\\H") {
        myFound.contents = myFound.contents + " "; 
        }
        }
      }
      

       

      Of course! It won't work as this! … But it becomes an usual situation for me as you know!

       

      Thanks in advance for any help! 

        • 1. Re: Find a superscript only if followed by a "normal position" char but not a space!
          LSH_246 Level 1

          Hi Ben,

           

          There are some errors in your code. Like this line:

           

          if ( followingChar = "\\H") {

           

          This is a common coding bug. You mean to check for equivalence, but you only use one equals sign, so you actually assign "\\H" to the variable followingChar.  I'm not sure what "\\H" is - were you looking for something under SpecialCharacters enumeration, maybe? Plus, I think you actually want to check if the following character is not equal to a space. Finally, you want to look at followingChar.contents, not followingChar.

           

          Here's your corrected code. Do a compare to see all the corrections:

           

          main();  
          
          function main() {  
            var myStyle = "Superscript";  
            var myDoc = app.activeDocument;  
          
            app.findGrepPreferences = app.changeGrepPreferences = null;    
            app.findGrepPreferences.appliedCharacterStyle = myStyle;     
            app.findGrepPreferences.findWhat = ".+";   
          
            var allFounds = myDoc.findGrep(true);   
          
            var foundCount = allFounds.length;
            for ( var i = 0; i < foundCount; i++ ) {  
              var myFound = allFounds[i];
              // check for the last character in the run, not the first 
              var lastCharIndex = myFound.characters.lastItem().index;  
          
              var followingChar = myFound.parentStory.characters[lastCharIndex+1];  
              // check if valid in case the last character is at the end 
              // of a story or cell.
              if (followingChar.isValid && followingChar.contents != " ") {  
                // using followingChar instead of myFound to insert the space 
                // will style it using the character style of followingChar,
                // rather than that of myFound, which is "Superscript".
                // This avoids the script repeatedly inserting spaces
                // if it's run more than once.
                followingChar.contents = " " + followingChar.contents;   
              }  
            }  
          }  
          
          1 person found this helpful
          • 2. Re: Find a superscript only if followed by a "normal position" char but not a space!
            Obi-wan Kenobi Adobe Community Professional

            Cool! Thanks a lot!

             

            Will try later to add an UI!

             

            A last question: I would like to insert the "space" with a specific char style!

            … defining a "var" with all the parameters after the line 05? Right way?

             

            Thanks in advance! 

            • 3. Re: Find a superscript only if followed by a "normal position" char but not a space!
              LSH_246 Level 1

              Sure - getting the character style at the beginning of the script, assigning it to a variable, and then using that variable to assign the character style in your for loop is probably an efficient way to do it.