Skip to main content
Inspiring
July 15, 2023
Answered

Find/Change script query

  • July 15, 2023
  • 1 reply
  • 486 views

Hi everyone,

 

I was wondering if there's a way to create a script which find text by formatting and change only the 4th text found, so that the 1st, 2nd and 3rd ones remain untoched? Would that be possible?

 

Thanks in advance,

Rogerio

This topic has been closed for replies.
Correct answer m1b

Hi @Rogerio5C09, here is a simple example. Because you don't want the change to apply to every found item, it means you can't use changeGrep or changeText and must do the changes manually.

- Mark

 

This example finds every 4th letter "a" and paints red. You can change the logic by editing the for loop line.

/**
 * Shows processing of every nth found objects (see `step` var).
 * This example uses findGrep, but findText or findObject
 * would be handled the same.
 * Note: This approach precludes the use of changeGrep, so the changes
 * must be done manually, as here for example the fillColor is changed.
 * @discussion https://community.adobe.com/t5/indesign-discussions/find-change-script-query/m-p/13939266#M533847
 */
function main() {

    var findWhat = 'a',
        step = 4;

    var doc = app.activeDocument;

    if (
        doc.selection.length == 0
        || app.selection[0].findGrep == undefined
    ) {
        alert('Please make a selection and try again.')
        return;
    }

    app.findGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.findWhat = findWhat;

    var found = doc.selection[0].findGrep();

    for (var i = step - 1; i < found.length; i = i + step) {
        found[i].fillColor = getRed(doc);
    }

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Find every 4th');


/**
 * Returns 'Red' color.
 * @returns {Color}
 */
function getRed(doc) {

    var red = doc.colors.itemByName('Red');

    if (!red.isValid)
        red = doc.colors.add({ name: 'Red', space: ColorSpace.RGB, colorValue: [255, 0, 0], colorModel: ColorModel.PROCESS });

    return red;

};

 

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
July 15, 2023

Hi @Rogerio5C09, here is a simple example. Because you don't want the change to apply to every found item, it means you can't use changeGrep or changeText and must do the changes manually.

- Mark

 

This example finds every 4th letter "a" and paints red. You can change the logic by editing the for loop line.

/**
 * Shows processing of every nth found objects (see `step` var).
 * This example uses findGrep, but findText or findObject
 * would be handled the same.
 * Note: This approach precludes the use of changeGrep, so the changes
 * must be done manually, as here for example the fillColor is changed.
 * @discussion https://community.adobe.com/t5/indesign-discussions/find-change-script-query/m-p/13939266#M533847
 */
function main() {

    var findWhat = 'a',
        step = 4;

    var doc = app.activeDocument;

    if (
        doc.selection.length == 0
        || app.selection[0].findGrep == undefined
    ) {
        alert('Please make a selection and try again.')
        return;
    }

    app.findGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.findWhat = findWhat;

    var found = doc.selection[0].findGrep();

    for (var i = step - 1; i < found.length; i = i + step) {
        found[i].fillColor = getRed(doc);
    }

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Find every 4th');


/**
 * Returns 'Red' color.
 * @returns {Color}
 */
function getRed(doc) {

    var red = doc.colors.itemByName('Red');

    if (!red.isValid)
        red = doc.colors.add({ name: 'Red', space: ColorSpace.RGB, colorValue: [255, 0, 0], colorModel: ColorModel.PROCESS });

    return red;

};

 

Robert at ID-Tasker
Legend
July 16, 2023

Why 1st "a" - in "Ad" - is ignored in the count? You've not set Case Sensitive option? 

 

m1b
Community Expert
Community Expert
July 16, 2023

That's right.