14 Replies Latest reply on Jan 12, 2015 2:15 AM by sreekarthik.k@gmail.com

    Unicode

    Level 1

      How to find the Unicode for non keyboard character using indesign script

        • 1. Re: Unicode
          Level 1

          I found an answer for Unicode replacement

           

          var itemFound =funFindGrep("([^A-z0-9\r \>\<\n\t\-\.\,:;\(\)])",null, null)

          for (ifound=0;ifound<itemFound.length;ifound++)

          {

              try

              {

                  app.select(itemFound[ifound]);

                 if (app.selection[0].contents.charCodeAt()>150)

                 {

              var str=app.selection[0].contents.charCodeAt().toString(16)

            if (str.length==3)

              {

                          str="0"+str;

                 //alert("f"+str)

                  }

              else if (str.length==2)

              {

                  str="00"+str;

                  }

              app.selection[0].contents="&#x  "+str.toUpperCase()+";";

              }

              }

          catch (e)

          {

              }

          }

          • 2. Re: Unicode
            pixxxel schubser Level 5

            Does this code really works for you? I don't know.

             

            I think your code is not complete.

            • 3. Re: Unicode
              Level 1

              Here is my complete code

              var selFold=Folder.selectDialog("Choose folder for Application File");

              if (selFold!=null)

              {

                  app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;

                  var allFile=selFold.getFiles("*.indd");

                  for (noF=0; noF<allFile.length;noF++)

                  {

                      var cur_doc=app.open(File(allFile[noF],false));

               

               

              var itemFound =funFindGrep("([^A-z0-9\r \>\<\n\t\-\.\,:;\(\)])",null, null)

              for (ifound=0;ifound<itemFound.length;ifound++)

              {

                  try

                  {

                      app.select(itemFound[ifound]);

                     if (app.selection[0].contents.charCodeAt()>150)

                     {

                  var str=app.selection[0].contents.charCodeAt().toString(16)

                if (str.length==3)

                  {

                              str="0"+str;

                     //alert("f"+str)

                      }

                  else if (str.length==2)

                  {

                      str="00"+str;

                      }

                  app.selection[0].contents="&#x  "+str.toUpperCase()+";";

                  }

                  }

              catch (e)

              {

                  }

              }

              funFindChangeText("<201C>","&#x201C;",null, null);

              funFindChangeText("<201D>","&#x201D;",null, null);

              funFindChangeText("<2018>","&#x2018;",null, null);

              funFindChangeText("<2019>","&#x2019;",null, null);

              funFindChangeText("<2013>","&#x2013;",null, null);

              funFindChangeText("<2014>","&#x2014;",null, null);

              funFindChangeText("<003E>","&#x003E;",null, null);

              funFindChangeText("<003E>","&#x003E;",null, null);

              funFindChangeText("<003C>","&#x003C;",null, null);

               

               

              funFindChangeText(" "," ",null, null);

              funFindChangeText(" ","",null, null);

              funFindChangeText("","",null, null)

                  //  cur_doc.close(SaveOptions.YES)

                   }

              }

               

               

              function funFindChangeText(argFind,argRep,argPStyle, argCStyle)

              {

                app.findTextPreferences=NothingEnum.nothing;

                  app.changeTextPreferences=NothingEnum.nothing;

                   app.findTextPreferences.findWhat=argFind;

                  (argPStyle!=null)?app.findTextPreferences.appliedParagraphStyle = argPStyle:false;

                (argCStyle!=null)?app.findTextPreferences.appliedCharacterStyle = argCStyle:false;

                  app.changeTextPreferences.changeTo=argRep;

                  cur_doc.changeText();

                return itemFound;

              }

                 

               

               

               

               

               

               

               

               

              function funFindGrep(argFind,argPStyle, argCStyle)

              {

                app.findGrepPreferences=NothingEnum.nothing;

                  app.changeGrepPreferences=NothingEnum.nothing;

                   app.findGrepPreferences.findWhat=argFind;

                  (argPStyle!=null)?app.findGrepPreferences.appliedParagraphStyle = argPStyle:false;

                (argCStyle!=null)?app.findGrepPreferences.appliedCharacterStyle = argCStyle:false;

                  var itemFound=cur_doc.findGrep();

                return itemFound;

              }

              • 4. Re: Re: Unicode
                Marc Autret Level 4

                Hi sreekarthik,

                 

                My two pennies regarding your core routine:

                 

                1. Do not select stuff that you can simply handle 'in the background'.

                 

                2. When you replace text, go from the end (to prevent invalid indexes).

                 

                3. Consider that InDesign characters may

                (a) have SpecialCharacters values

                (b) lead to U+FFFD (REPLACEMENT CHARACTER) when no Unicode value is associated

                (c) lead to UTF-16 surrogate pairs based on two bytes in JavaScript strings (for high Unicode values)

                 

                [Non-exhaustive list, I'm afraid.]

                 

                So my first approach would be something like this:

                 

                //================================================
                // HELPERS
                //================================================
                
                var findGrep = function(/*Document*/doc, /*str*/argFind, /*str?*/pStyle, /*str?*/cStyle)
                //------------------------------------------------
                {
                    var t;
                
                    app.findGrepPreferences = NothingEnum.nothing;
                
                    (t=app.findGrepPreferences).findWhat = argFind;
                    pStyle && t.appliedParagraphStyle = pStyle;
                    cStyle && t.appliedCharacterStyle = cStyle;
                
                    return doc.findGrep();
                };
                
                // ---
                // Keep in mind that JavaScript uses UTF-16 code units for strings,
                // so s might have 2 chars (surrogate pairs) for the supplementary
                // multilingual plane. Here you have to take a decision related
                // to the HTML side (UTF-8 vs. UTF-16 encoding?)
                // ---
                // Also, InDesign uses U+FFFD when some replacement is done, that is,
                // when no Unicode code point is associated to the 'character'
                // ---
                
                var htmlEncode = function F(/*str*/s)
                //------------------------------------------------
                {
                    // Cache
                    // ---
                    F.PREFIX_SZ || (
                        (F.PREFIX_SZ = "ZZ".toSource().replace(/ZZ.+$/,'').length),
                        (F.SUFFIX_RE = /["']\)+$/),
                        (F.BSLASHU_RE = /\\u/g)
                        );
                    
                    // Translate forms like '(new String("\uABCD"))'
                    // into '&#xABCD' -- Multiple chars are supported too
                    // ---
                    return s.toSource().substr(F.PREFIX_SZ).
                        replace(F.BSLASHU_RE,'&#x').
                        replace(F.SUFFIX_RE,';');
                };
                
                
                //================================================
                // MAIN ROUTINE
                // This will encode any character from U+00A0
                //================================================
                
                const TARGET = "[^\x00-\x9F]";
                var doc = app.activeDocument,
                    chars = findGrep(doc, TARGET),
                    i = chars.length,
                    c, s;
                while( i-- )
                    {
                    s = (c=chars[i].texts[0]).contents;
                    c.contents = htmlEncode(s);
                    }
                

                 

                Hope that helps.

                 

                @+

                Marc

                • 5. Re: Re: Unicode
                  Level 1

                  Thank you so much Marc.

                  • 6. Re: Re: Unicode
                    pixxxel schubser Level 5

                    Hi ,

                    cool stuff.

                     

                    But one question:

                    your code works perfect, if running from InDesigns script palette.

                     

                    Running from ESTK (target InDesign) --> ESTK is frozen after done all changes in InDesign

                    Do you have a presumption why?

                    (InDesign CS5.0 under Windows)

                    • 7. Re: Re: Unicode
                      Marc Autret Level 4

                      Sorry I have no clue on ESTK. (I must confess I never use it.)

                       

                      @+

                      Marc

                      • 8. Re: Re: Re: Unicode
                        Trevorׅ Adobe Community Professional

                        Hi all,

                         

                        My 1/2 pennies worth

                         

                        IF keeping formatting (including frame breaks) is not a problem then this one-liner could be hundreds to thousands (depending on the number of replacements) times quicker.

                         

                        app.activeDocument.stories[0].contents = app.activeDocument.stories[0].contents.replace(/[^\x00-\x9F]/g, function (f) {return "&#x" + (f.charCodeAt(0)).toString(16).toUpperCase() + ";"});
                        

                         

                        I tested both the above line and Marc's script running from the ESTK (Version 4+) on windows 7 with CS5 ME without any problem

                         

                        @Marc

                         

                        Sorry I have no clue on ESTK. (I must confess I never use it.)

                         

                        I also avoid the ESTK as much as possible and like others have written how pathetic it is but I certainly wouldn't say that I never use it.

                        Although I have builds for Sublime Text for both Windows and Mac to execute the scripts directly from their, for debugging purposes ($.writeln) I generally run the scripts from the estk.

                        To avoid this debugging one could:

                        1) Write to a debug file

                        2) Use alerts

                        3) Avoid bugs.

                         

                        It wouldn't surprise me if you go for option 3, I am nevertheless wondering if you might be using another option and if so what?

                         

                        Regards

                         

                        Trevor 

                         

                        P.s. Sreekarthik mark Marc's answer as correct.

                        • 9. Re: Re: Re: Unicode
                          Marc Autret Level 4

                          Hi Trevor,

                           

                          Thanks for your comment.

                           

                          Just to be clear: saying I do not use ESTK doesn't mean, at all, 'one shouldn't use ESTK.' Each script dev has its own practices and habits. For my part I use for a long time UltraEdit to write code—but SublimeText is a great software, and unquestionably much more cutomizable.

                          • 10. Re: Unicode
                            TᴀW Adobe Community Professional & MVP

                            What's wrong with ESTK? It's got really nice debugging features: a Call

                            Stack, customizable, programmable break points, and a very useful

                            built-in Help. All useful stuff.

                            • 11. Re: Unicode
                              Trevorׅ Adobe Community Professional

                              Hi Ariel,

                               

                              You are correct that the ESTK has some plus points.

                              In addition to what you wrote it is free no need to sign up to CC or buy any packages.

                              I just finished a project for kol haloshon involving amongst other things moving around about 20,000+ files which could be done very easily using the estk which handles unicode file names very well and because of the unicode issues were getting somewhat impossible to deal with in the batch, vbs and vba that the main project was being written in.

                              Of course that's not what were talking about here.

                               

                              I'm not going to make a comparison on the particular features and capabilities of ESTK verses Sublime Text or Ultra Edit, I'll just say that I personally find working with ST so much quicker and easier.  If you learn the tricks you can do with ST then you will really understand and appreciate this.

                               

                              Two analogies ;-)

                              Like a 25 year old Subaru to a new Mercedes, Like Word Pad is to InDesign so to is ESTK to Sublime Text.

                               

                              Either-way I highly recommend you making a 20 Minute journey up the 346 (Road) and I can demonstrate to you just how efficient my work set up is.   I'll send you a mail with my details and be have for you to pop by if you want to see my pretty (or not so pretty) face.

                               

                              Regards

                               

                              Trevor

                               

                              P.s. ESTK on Windows does work somewhat better than on the Mac. (At least by me and so I gather from others).

                              • 12. Re: Unicode
                                Level 1

                                Hi Trevor / Marc

                                 

                                I am using indesign cs6 and the script is not running from the ESTK (Version 4.2.12) on windows 8.

                                 

                                ESTK is frozen after done all changes in InDesign

                                • 13. Re: Unicode
                                  Trevorׅ Adobe Community Professional

                                  Hi sreekarthik,


                                  Does this only happen when you run the script from the ESTK or also if you run it from the scripts panel in InDesign itself?

                                  Does this happen with both Marc's and my script?

                                  Approximately how many Unicode characters are being replaced and how long do(es) the script(s) take to run?

                                  What message do you get in the windows resource monitor when you look into the wait chain of the indesign process?Unicode _ Adobe Community_2015-01-12_08-43-22.png


                                  Trevor

                                  • 14. Re: Unicode
                                    Level 1

                                    It's working in the script path.

                                     

                                    Thanks for your great support. Have a nice day. Regards Sreekarthik.K