Skip to main content
Inspiring
July 20, 2016
Answered

Adding Highlight to script Or Grep Style?

  • July 20, 2016
  • 12 replies
  • 6869 views

Hi

Is it possible to Add a hightlight to a script?  Basically what i want it to do is Highlight text and the tricky part is that the text changes sizes depending on the size of file we're working on.  Say i want to automatically highlight this "FOR PROFESSIONAL USE ONLY" the way we do it now is Make a underline the like this in the underline options menu: Say my font size is 8, Leading 9.6 (These will always change).

Weight:  =   9.6

Offset:  =   -8*.33

Color = Yellow

That is the way we do it now.  Can it be scripted or Grep style even that will automatically do this for certain Lines of text only?  And no matter the size of font calculate what it needs to so whether its 2pt, 10pt 100pt etc... it will Highlight the specified text correctly?

hopefully this is possible.. 

Thank you

This topic has been closed for replies.
Correct answer Trevor:

Doesn't look like the strike though option is good but the above solution looks like it works well.

Edit:

Just to explain the colors

Yellow is the highlighted color

Blue is a conditional text highlight

Black is selected text

The half blue fred was added after the script was run.


The below one is a better.

I removes a bug and also if no "what" is provided to the highlight function and there's no entry in the find what of the find text panel and some text is selected then that text will be highlighted.

An idea in the script is that a keyboard short cut can be applied for it and it will get it's info from the find text panel.

So for the meantime change the correct answer to this one

// Highlight function by Trevor

// http://creative-scripts.com

// Custom Scripts for Adobe InDesign, Illustrator and a lot more

// Beta Version 1

// https://forums.adobe.com/message/8898663#8898663

function highlight(what, color) {

    var range, s, doc;

    doc = app.properties.activeDocument;

    if (!doc) {

        return 'No Active Document';

    }

    s = app.selection && app.selection[0];

    range = (s && s.properties.underline !== undefined && s.characters.length) ? s : doc;

    app.doScript(f, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Highlight Text');

    function f() {

        var finds, find, l, leading, pointSize, currentChangeProps;

        color = color || app.activeDocument.colors.itemByName('Yellow');

        // Store current find change text settings

        currentFindTextWhat = app.findTextPreferences.findWhat;

        currentChangeProps = app.changeTextPreferences.properties;

        what = what || currentFindTextWhat;

        if (what !== '') {

            // if no what is provided then the current entry in the find text panel will be used

            app.findTextPreferences.findWhat = what;

            app.changeTextPreferences.changeTo = '';

            app.changeTextPreferences.underline = true;

            app.changeTextPreferences.underlineColor = color;

            finds = range.findText();

            range.changeText();

            l = finds.length;

            while (l--) {

                find = finds;

                leading = find.leading;

                pointSize = find.pointSize;

                leading = pointSize * find.autoLeading / 100;

                find.underlineWeight = leading + 'pt'; // This is presuming that font and leading size is set to points!

                find.underlineOffset = (pointSize - leading) * 1.5; // The 1.5 might need tweaking but seems to work well

            }

            app.findTextPreferences.findWhat = currentFindTextWhat;

            app.changeTextPreferences.properties = currentChangeProps;

        } else if (range !== app.activeDocument) {

            leading = range.leading;

            pointSize = range.pointSize;

            leading = pointSize * range.autoLeading / 100;

            range.underline = true;

            range.underlineColor = color;

            range.underlineWeight = leading + 'pt'; // This is presuming that font and leading size is set to points!

            range.underlineOffset = (pointSize - leading) * 1.5; // The 1.5 might need tweaking but seems to work well

        }

    }

}

/***************************************************************************************************************************************

** If text is selected then the "highlighting" is only applied within the selected text                                              **

** Otherwise the "highlighting" is applied within the whole document                                                                **

** One could change this to apply to what ever is selected in the Find text document if one wanted to                                **

** To highlight the word(s) in the find text panel just use highlight(); or highlight(undefined, app.activeDocument.swatches[5]);    **

** One can use highlight('Foo'); to highlight "foo" and ignore what's in the find text panel                                        **

** The formating / styles etc. for the find are taken from the find panel                                                            **

** If one provides a swatch as the second argument highlight(undefined, app.activeDocument.swatches[5]); that swatch will be used    **

** otherwise yellow will be used                                                                                                    **

** If one provides a swatch as the second argument highlight(undefined, app.activeDocument.swatches[5]); that swatch will be used    **

** If text is selected and what argument is given and the find what in the find panel is empty then the selected text is highlighted **

**************************************************************************************************************************************/

highlight();

12 replies

cbishop01Author
Inspiring
July 22, 2016

I'm actually running into a problem here.  It highlights a yellow Greenish color.  WHen i do separations preview it comes out As just yellow but when i export to PDF it comes out 84% yellow and 4% cyan.  Making of course a green color.  Any idea how to keep it Pure Yellow?

Trevor:
Braniac
July 22, 2016

That's one o the things I meant when I wrote "depends on your workflow"

The problem is that the conditional colors are rgb and not cmyk.

You need to set the conditions indicatorColor properties as best as you can.

cbishop01Author
Inspiring
July 22, 2016

is there anyway to convert them to CMYK that you know of? the code i'm using to indicate the color is

colorToHighlig = doc.conditions.add ({name:"Highlight",indicatorMethod:ConditionIndicatorMethod.USE_HIGHLIGHT, indicatorColor:UIColors.YELLOW});

When i tried Y=100 it of course does not work.

Trevor:
Braniac
July 20, 2016

Hi Chris

What about applying a conditional text? That would be easy. Then you could just do a manual or scripted find change text to apply the condition.

Set the condition to print if desired.

This solution is going to depend on your workflow.

HTH

Trevor

cbishop01Author
Inspiring
July 21, 2016

This actually works just like i'm wanting.  I'm just going to need to try and put it in a script.  Can this be handled with GREP somehow?

Trevor:
Braniac
July 21, 2016

Hi

You don't need to grep just use.

var doc = app.activeDocument;

app.findTextPreferences = app.changeTextPreferences = null;

app.findTextPreferences.findWhat = 'FOR PROFESSIONAL USE ONLY';

// note the necessary [] around the condition(s)

app.changeTextPreferences.appliedConditions = [doc.conditions.itemByName('Highlight')]; // Change to correct name

doc.changeText();