5 Replies Latest reply on Jul 16, 2009 3:34 PM by Martin Fischer

    CS3 / Javascript / Highlight specified text

    sudar1983_14 Level 2

      Hi Geeks,

       

      I want to highlight some specifix text, that is suppose if i want to highlight "XXX" in the indesign file. Like ms-word highlight, Is there any option available in indesign?

       

      Also i want to remove the highlight.

       

      Regards,

      sudar.

        • 1. Re: CS3 / Javascript / Highlight specified text
          Martin Fischer Level 2

          Hi Sudar,

           

          just create a character style with underline = true.

           

          Play around with some properties of underline:

               underlineWeight near the value of leading,

               underlineOffset about -2.5 pt,

               underlineType,

               underlineColor,

               underlineTint ...

           

           

          Martin  

          1 person found this helpful
          • 2. Re: CS3 / Javascript / Highlight specified text
            sudar1983_14 Level 2

            Hi Martin,

             

            Thanks for your quick reply.

             

            I have already tried this one, when i try to remove the highlight, it is gonna be a problem, that is

             

            with (app.findTextPreferences)

            {

            underline=true;

            underlineOffset=-2;

            underlineTint=100;

            underlineWeight=7;

            underlineColor=aDoc.swaches.item('Highlight');

            findWhat="";

            }

             

            the Error "Required failed because object is from a different document or workspace".

             

            Do you have any answer for this error?

             

             

            Regards,

            sudar.

            • 3. Re: CS3 / Javascript / Highlight specified text
              Martin Fischer Level 2

              Hi Sudar,

               

              what are you trying to do with settings for underline in the 'app.findTextPreferences'?

              Just create the character style as told above and then search for some text to apply the character style to the text that has been found (e.g. app.changeTextPreferences.appliedCharacterStyle = myHighlightCS;).

              Search for some content, for a regular expression or another special property of the text.

               

              The problem you have told is a known bug in CS3.

              Workaround: add the color your are looking for to the swatches of the application and look for 'app.swatches.item('Highlight');'.

               

              But - as I suppose - this is not, what you are looking for.

               

              Martin

              • 4. Re: CS3 / Javascript / Highlight specified text
                sudar1983_14 Level 2

                Hi Martin,

                 

                I don't want to apply character style.

                 

                Suppose i want to remove the highlight then, i want to find the highlighted text and then remove the highlight, this is what i want to do.

                 

                In a Single script, I want to add and remove the highlight, I have created two option buttons for add and remove.

                 

                when i add app.colors or swatches, i can't highlight the text using the below code.

                 

                var swatchName='Highlight';

                var aDoc=app.activeDocument;

                highlightSwatch=app.swatches.item(swatchName);

                 

                //Add Highlight

                if (highlightSwatch == null)

                {

                var swatchColor=aDoc.colors.add();

                swatchColor.colorValue=[0, 0, 100, 0];

                swatchColor.name='Highlight';

                highlightSwatch=app.swatches.item("Highlight");

                }

                 

                app.findGrepPreferences=app.changeGrepPreferences=null;

                app.findGrepPreferences.findWhat="x[x]+";

                matchedItems=app.findGrep();

                for (var mtch=0; mtch < matchedItems.length; mtch++)

                {

                matchedItems[mtch].select();

                with (app.selection[0])

                {

                underline=true;

                underlineColor=highlightSwatch;

                underlineOffset=-2;

                underlineTint=100;

                underlineWeight=7;

                }

                }

                 

                 

                //Remove highlight

                if (highlightSwatch==null)

                {

                alert("Swatches name not matching with Script defined,\nSo can you can the name of the highlight swatch and then try");

                exit(0);

                }

                else

                {

                app.findTextPreferences=app.changeTextPreferences=null;

                with (app.findTextPreferences)

                {

                underline=true;

                underlineOffset=-2;

                underlineTint=100;

                underlineWeight=7;

                underlineColor=aDoc.swaches.item('Highlight');

                findWhat="";

                }

                app.changeTextPreferences.underline=true;

                app.changeTextPreferences.changeTo="";

                app.changeTextPreferences.underlineColor=app.colors.item('Highlight');

                app.changeText();

                }

                 

                It shows the same error.

                 

                Finally if i working with app.swatches, find preferences worked and selection object not worked.

                document.swatches worked for selection object and find preferences not working for this situation.

                 

                what is the solution for this?

                 

                 

                Regards,

                sudar.

                • 5. Re: CS3 / Javascript / Highlight specified text
                  Martin Fischer Level 2

                  Hi Sudar,

                   

                  sorry for the delay.

                   

                  First there is a typo in your code.

                  'swaches' should be 'swatches' in underlineColor=aDoc.swaches.item('Highlight');

                   

                  Second, please take a look at my sample-code how to deal with the apps' swatches to be used in the find-replace command.

                  This is not an extension of your code, but it shows the way how to integrate and address a swatch into the change-command in InDesign CS3.

                   

                  // get swatch
                  var myCol = getCol('Highlight');
                  
                  // --------------- do the task ---------------------------
                  app.findGrepPreferences = app.changeGrepPreferences = null;
                  app.findGrepPreferences.findWhat = 'xx+'
                  app.changeGrepPreferences.fillColor = myCol.col;
                  app.activeDocument.changeGrep();
                  // --------------- end of the task -----------------------
                  
                  // remove swatch if app.swatches.item(aColName) has been added
                  if (myCol.added == true)
                       myCol.col.remove();
                  
                  
                  // function to get swatch; 
                  // add swatch if app.swatches.item(aColName) doesn't exist 
                  function getCol(aColName)
                  {
                       var aCol = null;
                       var aColAdded = false;
                       aCol = app.swatches.item(aColName);
                       if (aCol == null)
                       {
                            aCol = app.colors.add({name:aColName});
                            aColAdded = true;
                       }
                       return {col:app.swatches.item(aColName), added:aColAdded}
                  }
                  

                   

                  Note: the apps' swatch will be added while processing the active script if it does not exist.

                  If this swatch has been added while processing the active script it will be removed before finish.

                   

                  Hope this helps to understand the workaround how to deal with swatches in change-commands in CS3 I have told above.

                   

                  Martin