1 Reply Latest reply on Feb 15, 2011 6:02 PM by Elmer Banate

    Find/Change Script using Glyph

    Elmer Banate Level 1

      Hello all,

       

      First of all Good Day to all of you!

       

      This is my first time to post here. I'm still new to InDesign Scripting and hoping someone could help me to solve my problem.

       

      I'm trying to change the glyphID of a certain character from glyphID "123" to glypID "1031" with the font family of the Arno Pro. I've already found some topic here about mass replace of glyphID's which I tried and it works. Although, FindChangeByList.jsx could also make mass replace given this parameters:

           glyph {fontStyle:"Italic Caption", appliedFont:"Arno Pro", glyphID:123} {fontStyle:"Italic Caption", appliedFont:"Arno Pro", glyphID:1031}.

       

      But, as I've said those are mass replace and I need only to replace the glyphID "123" when it is in a certain place, like if it comes after "ě" or "ā" etc..

       

      I've posted sample of search text file and the java script below (note some these codes are copied from other script which I've got from downloads and tried to modify them).

       

      Thanks,

      --elmer

       

       

      Sample of search Text file tab delimited "\t":

      ========================================================================================== =======================

      //Find String     Font Family     Font Style          Position     glyphIDtoSearch     glyphIDtoReplace
      ě´                    Arno Pro          Italic Caption     2               123                        1031
      ė´                    Arno Pro          Italic Caption     2               123                        1031
      ā´                    Arno Pro          Italic Caption     2               123                        1031
      ========================================================================================== =======================

       

      InDesign Script:

      ========================================================================================== =======================

      var myScriptFileName, myFindChangeFile, myFindChangeFileName, myScriptFile, myResult;

       

      myDoc = app.activeDocument;

      var myFindChangeFile=File.openDialog("Choose the file containing your find/change list");

      if(myFindChangeFile != null){

      var myResult = myFindChangeFile.open("r", undefined, undefined);

      if(myResult == true){

       

      if (app.scriptPreferences.version < 5){

      //footnotes2textframes();

      }

       

      //Loop through the find/change operations.

      do{

      myLine = myFindChangeFile.readln();

       

      //Ignore comment lines and blank lines.

      if((myLine.substring(0,2)!="//")&&(myLine != "")){

      myFindChangeArray = myLine.split("\t");

       

      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(myFindChangeArray[0],undefined,true,undefined);

       

       

                     // else, for CS3 and CS4

      }else{

                     //Clear any existing find/change settings

      app.findTextPreferences = NothingEnum.nothing;

      app.changeTextPreferences = NothingEnum.nothing;

      app.findGrepPreferences = NothingEnum.nothing;

      app.changeGrepPreferences = NothingEnum.nothing;

      app.changeGlyphPreferences = NothingEnum.nothing;

      app.findGlyphPreferences = NothingEnum.nothing;

      //Search the frame for the specified string

      app.findTextPreferences.findWhat = myFindChangeArray[0];

      //Set the find options.

      app.findChangeTextOptions.caseSensitive = true;

      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();

      }

       

      try {

      for (k=0; k < myFinds.length; k++) {

      //check character of second character, to change glyphID

      var myChar = myFinds[k].characters[parseInt(myFindChangeArray[3])-1];

       

      //set the findGlyphPreferences.glyphID from 5th column of the text file

      app.findGlyphPreferences.glyphID = parseInt(myFindChangeArray[4]);

       

      //set the findGlyphPreferences. appliedFont from 2nd column of the text file

      app.findGlyphPreferences.appliedFont = app.fonts.item(myFindChangeArray[1]);

       

      if (myChar.appliedFont.fontFamily == myFindChangeArray[1] && myChar.fontStyle == myFindChangeArray[2]) {

      //set the changeGlyphPreferences.glyphID from 5th column of the text file

      app.changeGlyphPreferences.glyphID = parseInt(myFindChangeArray[5]);

       

      //set the changeGlyphPreferences. appliedFont from 2nd column of the text file

      app.changeGlyphPreferences.appliedFont = app.fonts.item(myFindChangeArray[1]);

       

      //the line below not sure if this command is valid command

      myFinds[k].characters[parseInt(myChar)].changeGlyph();

      }

      }

       

      } catch (myError){}

      }

      } while(myFindChangeFile.eof == false);

       

      if (app.scriptPreferences.version < 5){

      //textframes2footnotes();

      }

      // close kerning list file

      myFindChangeFile.close();

      }

      alert("Done.");

      }

      ========================================================================================== =======================

       

        • 1. Re: Find/Change Script using Glyph
          Elmer Banate Level 1

          Hello all,

           

          Sorry as I was not able to go back here as early as I have figured-out this problem. But anyway,  I've just posted the script if incase any one would need this script. It is now working on my end.

           

          Thank a lot for everyone.

          --elmer

           

          var myScriptFileName, myFindChangeFile, myFindChangeFileName, myScriptFile, myResult;
          
          myDoc = app.activeDocument;
               var myFindChangeFile=File.openDialog("Choose the file containing your find/change list");
               if(myFindChangeFile != null){
                    var myResult = myFindChangeFile.open("r", undefined, undefined);
                    if(myResult == true){
                         
                         //Loop through the find/change operations.
                         do{
                              myLine = myFindChangeFile.readln();
                              //Ignore comment lines and blank lines.
                              if((myLine.substring(0,2) !="//") && (myLine != "")){
                                   myFindChangeArray = myLine.split("\t");
                                                                           
                                   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(myFindChangeArray[0],undefined,true,undefined);
                                                                                     
                                   // else, for CS3 and CS4
                                   }else{                                             
                                        //Clear any existing find/change settings
                                        app.findTextPreferences = NothingEnum.nothing;
                                        app.changeTextPreferences = NothingEnum.nothing;
                                        app.findGrepPreferences = NothingEnum.nothing;
                                        app.changeGrepPreferences = NothingEnum.nothing;
                                        app.findGlyphPreferences = NothingEnum.nothing;
                                        app.changeGlyphPreferences = NothingEnum.nothing;                                             
                                        //Search the frame for the specified string
                                        app.findTextPreferences.findWhat = myFindChangeArray[0]; 
                                        //Set the find options.
                                        app.findChangeTextOptions.caseSensitive = true;
                                        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();
                                   }
                                   
                                   try {                                             
                                        for (k=0; k < myFinds.length; k++) {                                                                                                                                                                                              
                                             //check character style of second character, to change glyphID or not
                                             var myChar = myFinds[k].characters[parseInt(myFindChangeArray[3])-1];
                                             var myIP = parseInt(myFindChangeArray[3]) - 1;
                                             var myFindGlyph = parseInt(myFindChangeArray[4]);
                                             var myChangeGlyph = parseInt(myFindChangeArray[5]);
                                             app.findGlyphPreferences.glyphID = myFindGlyph;
                                             app.findGlyphPreferences.appliedFont = app.fonts.item(myFindChangeArray[1] + "\t" + myFindChangeArray[2]);
                                             app.changeGlyphPreferences.appliedFont = app.fonts.item(myFindChangeArray[1] + "\t" + myFindChangeArray[2]);
                                                                                               
                                             if (myChar.appliedFont.fontFamily == myFindChangeArray[1] && myChar.fontStyle == myFindChangeArray[2]) {
                                                  app.changeGlyphPreferences.glyphID = myChangeGlyph;
                                                  myChar.changeGlyph();
                                             }
                                        }
                                        
                                   }catch (myError){}
                              }
                         } while(myFindChangeFile.eof == false);
                         
                         if (app.scriptPreferences.version < 5){
                              alert("This script will run only in CS3 or later.");
                         }
                         // close glyph list file
                         myFindChangeFile.close();          
                    }
               alert("Done.");
               }