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

How to find some characters with grep and then apply object style to that textbox?

Advisor ,
Jan 07, 2025 Jan 07, 2025

Copy link to clipboard

Copied

Hello, everyone.

For example, some specific textboxes have characters like "001-002@" "001-003@" or something like that.
I want to use Grep to find them and then apply object styles to the textboxes they are in.

 

Thank you very much.

TOPICS
Bug , Feature request , How to , Performance , Scripting , Type

Views

146

Translate

Translate

Report

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 2 Correct answers

Engaged , Jan 07, 2025 Jan 07, 2025

Hi @dublove 

Use this:

 

// Define the pattern
var pattern = /\d{3}-\d{3}@/;

// Define the object style name
var objStyleName = "AB";

// Get the active document
var doc = app.activeDocument;

// Loop through all text frames
for (var i = 0; i < doc.textFrames.length; i++) {
    var textFrame = doc.textFrames[i];
    var text = textFrame.contents;

    // Search for the pattern
    if (pattern.test(text)) {
        // Apply the object style to the text frame
        textFrame.appliedObjectStyle = d
...

Votes

Translate

Translate
Community Expert , Jan 08, 2025 Jan 08, 2025

Hi @dublove, you already have a good answer from Anantha, but here is an extra way to do it, using Indesigns findGrep. I have made an array of "queries" so you can specify the object styles to apply to different find strings.

- Mark

 

 

/**
 * Find Grep And Apply Object Style.js
 * @author m1b
 * @version 2025-01-08
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-to-find-some-characters-with-grep-and-then-apply-object-style-to-that-textbox/m-p/15076832
 */
function main() 
...

Votes

Translate

Translate
Engaged ,
Jan 07, 2025 Jan 07, 2025

Copy link to clipboard

Copied

Hi @dublove 

Use this:

 

// Define the pattern
var pattern = /\d{3}-\d{3}@/;

// Define the object style name
var objStyleName = "AB";

// Get the active document
var doc = app.activeDocument;

// Loop through all text frames
for (var i = 0; i < doc.textFrames.length; i++) {
    var textFrame = doc.textFrames[i];
    var text = textFrame.contents;

    // Search for the pattern
    if (pattern.test(text)) {
        // Apply the object style to the text frame
        textFrame.appliedObjectStyle = doc.objectStyles.item(objStyleName);
    }
}
Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.

Votes

Translate

Translate

Report

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
Advisor ,
Jan 08, 2025 Jan 08, 2025

Copy link to clipboard

Copied

Thank you very much, I have used it.
Efficiency has multiplied.
It's unbeatable.

Votes

Translate

Translate

Report

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
Community Expert ,
Jan 08, 2025 Jan 08, 2025

Copy link to clipboard

Copied

Hi @dublove, you already have a good answer from Anantha, but here is an extra way to do it, using Indesigns findGrep. I have made an array of "queries" so you can specify the object styles to apply to different find strings.

- Mark

 

 

/**
 * Find Grep And Apply Object Style.js
 * @author m1b
 * @version 2025-01-08
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-to-find-some-characters-with-grep-and-then-apply-object-style-to-that-textbox/m-p/15076832
 */
function main() {

    var queries = [
        { findWhat: '\\d{3}-001@', objectStyleName: 'My Style 1' },
        { findWhat: '\\d{3}-002@', objectStyleName: 'My Style 2' },
    ];

    var doc = app.activeDocument;

    for (var q = 0; q < queries.length; q++) {

        app.findGrepPreferences = NothingEnum.NOTHING;
        app.findGrepPreferences.findWhat = queries[q].findWhat;

        // find the dayNumbers
        var found = doc.findGrep(),
            style = doc.objectStyles.itemByName(queries[q].objectStyleName);

        if (!style.isValid) {
            alert('Bad object style: "' + queries[q].objectStyleName + '"');
            continue;
        }

        for (var i = 0; i < found.length; i++) {
            if (undefined != found[i].parentTextFrames[0])
                // apply object style
                found[i].parentTextFrames[0].appliedObjectStyle = doc.objectStyles.itemByName(queries[q].objectStyleName);
        }

    }

}
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set Object Styles');

 

Votes

Translate

Translate

Report

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
Advisor ,
Jan 08, 2025 Jan 08, 2025

Copy link to clipboard

Copied

Hi m1b

This script is not running.

Is there something wrong with your "findWhat"?

 

If there were panels where you could type Grep.
If there were drop-down lists to select object styles
That would be great.

 

Votes

Translate

Translate

Report

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
Community Expert ,
Jan 08, 2025 Jan 08, 2025

Copy link to clipboard

Copied

LATEST

Hi @dublove, of course you have to set the "findWhat" greps and objectStyleNames. Just for example, to match Ananda's script, set to this:

var queries = [
    { findWhat: '\\d{3}-\\d{3}@', objectStyleName: 'AB' },
];

 

Adding a UI would be cool. I might do that when I have time.

- Mark

Votes

Translate

Translate

Report

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