Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Highlight text with some condition, is this possible by script?

Participant ,
Feb 14, 2019 Feb 14, 2019

Hi,

I want to highlight the following text with some particular condition. Is this possible by script?

Highlights are required for (red colour for reference):

1. Hyphenated text

2. Em-dash text (with space and without space)

3. superscript number with empty space (space may be in superscript or may not superscript)

thanks in advance.

by

hasvi

2.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Feb 15, 2019 Feb 15, 2019

I can't advise on the script you are referring to.

But I'm not sure you need a script if you can achieve what you want using Grep styles or Find/Replace.

About the cases where there could be a character which is not a word character before or after the em dash, you can give this regex a try:

\w+.?\h?—\h?.?\w+

Translate
Guide ,
Feb 14, 2019 Feb 14, 2019

Hy Hasvi

For points 1 and 2, it's easy to configure either a grep style or a grep Find/Replace.

\w+(-\w+)+ will catch compound words.

\w+\h?—\h?\w+ will catch words surrounding an em dash. (FYI: it wouldn't deal with compound words but you already have, so...)

Now, point 3 is a little bit more tricky, because you can't catch both spaces and subscript numbers in one regex.

You could do a Grep Find/Replace to catch superscript numbers, but it will also catch the ones with no space before, which is not exactly what you asked for. Or you could look for space then number, even if not superscript, but again, not what you asked for...

Maybe you could tell us more about what you want to achieve at the end? Is it some sort of typographic warning highlight?

Cheers

Vinny

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 15, 2019 Feb 15, 2019

Hi Vinny,

Thanks for your swift reply. I applied your referred greps in below script but getting output like this, and another rounded em-dash word has not been highlighted by this script. Please advice.

If possible please correct this script to get this highlights properly on x-axis of text.

Also please advice, is this long written script really required for highlights words? Please share if there is any short scripts available for highlight words with grep preference.

// 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

        currentFindGrepWhat = app.findGrepPreferences.findWhat;

        currentChangeProps = app.changeGrepPreferences.properties;

        what = what || currentFindGrepWhat;

        if (what !== '') {

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

            app.findGrepPreferences.findWhat = what;

            app.changeGrepPreferences.changeTo = '';

            app.changeGrepPreferences.underline = true;

            app.changeGrepPreferences.underlineColor = color;

            finds = range.findGrep();

            range.changeGrep();

            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.findGrepPreferences.findWhat = currentFindGrepWhat;

            app.changeGrepPreferences.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('\\w+(-\\w+)+');

highlight('\\w+\\h?—\\h?\\w+');

function color3PlusHyphenateted() { 

    var doc, stories, n, l, lastWords, firstWords, hyphenateted, e, i, lwl, HReg; 

    HReg = /HHH+/g; 

    doc = app.activeDocument; 

    stories = doc.stories.everyItem().getElements().slice(0); 

    l = stories.length; 

    for (n = 0; n < l; n++) { 

        lastWords = [0].concat([], stories.lines.everyItem().words[-1].getElements().slice(0)); 

        firstWords = stories.lines.everyItem().words[0].getElements().slice(0); 

        lwl = lastWords.length; 

        hyphenateted = []; 

        for (i = 1; i < lwl; i++) { 

            hyphenateted = (lastWords === firstWords) ? 'H' : '0'; 

        } 

 

        hyphenateted = hyphenateted.join(''); 

        while (e = HReg.exec(hyphenateted)) { 

            for (i = e.index + 1; i <= HReg.lastIndex; i++) { 

                lastWords.fillColor = doc.colors[3]; 

            } 

        } 

        HReg.lastIndex = 0; 

    } 

app.doScript(color3PlusHyphenateted,ScriptLanguage.JAVASCRIPT,undefined, UndoModes.ENTIRE_SCRIPT, 'color3PlusHyphenateted'); 

by

hasvi

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Feb 15, 2019 Feb 15, 2019

I can't advise on the script you are referring to.

But I'm not sure you need a script if you can achieve what you want using Grep styles or Find/Replace.

About the cases where there could be a character which is not a word character before or after the em dash, you can give this regex a try:

\w+.?\h?—\h?.?\w+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 28, 2019 Feb 28, 2019
LATEST

Hi Vinny,

I am using below greps to find the special characters such as "comma, end period, colon, semi colon" between the text without space, which is not allowed as per our client instruction. But I am getting superscript also while find this, is there any way to ignore superscript?

[\w\.,](\:|\;|\,|\.|\\”|\“)(?=\w)

thanks in advance.

by

hasvi

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 15, 2019 Feb 15, 2019

Hi Vinny, thanks it works fine.

by

hasvi

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines