Skip to main content
Inspiring
January 16, 2025
Answered

GREP search problems

  • January 16, 2025
  • 1 reply
  • 645 views

Hello. I have a trouble with grep search. When searching manually it works, but when i try to use js script it applyes only the last pattern from three. I do not understand why.


// Run the script
var doc = app.activeDocument;
var charStyleName = "dontChange";

// Create or retrieve the character style once
var charStyle = getOrCreateCharacterStyle(doc, charStyleName);

var extentions =  "(txt|md|rtf|doc|docx|odt|xls|xlsx|csv|ppt|pptx|pdf|epub|mobi|jpg|jpeg|png|gif|bmp|tiff|svg|ai|eps|mp3|wav|flac|mp4|mkv|avi|mov|zip|rar|7z|tar|gz|iso|html|css|js|json|xml|sql|sh|bat|ps1|exe|dll|sys|ini|log|dmp|indd|idml|dwg|dxf|ttf|otf|woff|woff2|eot)"

// Define patterns to apply the character style
var patterns = [
    "\\<[^[:space:]]+://[^[:space:]]*\\>",
    "\\<[^[:space:]]+@[^[:space:]]*\\>",
    "\\<[^[:space:]]+\\." + extentions + "+[^[:space:]]*\\>"];

for (var j = 0; j < patterns.length; j++) {
    // Set the pattern and search with GREP
    app.findTextPreferences.findWhat = patterns[j];
    applyCharacterStyle(doc, patterns[j], charStyle);
}

Thank you.
Correct answer FRIdNGE

Not "Text" researches but "Grep"! To be corrected everywhere.

 

(^/)  The Jedi


I mean:

 

"app.findTextPreferences" ==> "app.findGrepPreferences"

"app.changeTextPreferences" ==> "app.changeGrepPreferences"

 

[I've not checked your patterns!]

 

(^/)

1 reply

m1b
Community Expert
Community Expert
January 16, 2025

Hi @Viktoriia_ you haven't given us enough code to actually run it (there are two functions missing) but hopefully this will give you an idea of what's missing:

// Define patterns to apply the character style
var patterns = [
    "\\<[^[:space:]]+://[^[:space:]]*\\>",
    "\\<[^[:space:]]+@[^[:space:]]*\\>",
    "\\<[^[:space:]]+\\." + extentions + "+[^[:space:]]*\\>"];

// reset grep prefs
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;

for (var j = 0; j < patterns.length; j++) {
    // Set the pattern and search with GREP
    app.findTextPreferences.findWhat = patterns[j];

    // perform the search
    var found = doc.findGrep();

    // `found` is an array of text objects (if anything found)
    for (var k = 0; k < found.length; k++) {

        // I'm assuming that this function takes a text object as the 2nd parameter...
        applyCharacterStyle(doc, found[k], charStyle);

    }
}

If that doesn't help, please post a full (even if getting an error) script. It can be stripped down, but must be runable for us to test.

- Mark

Inspiring
January 16, 2025

Hello, Mark! Thank you for helping. Here is full code, unfortunately I could not run my with your changes, so here is the full unchanged version. It runs without errors, but only applyies the style to the last pattern.

function getOrCreateCharacterStyle(doc, styleName) {
    var charStyle;
    try {
        // Try to find an existing character style with the specified name
        charStyle = doc.characterStyles.itemByName(styleName);
        charStyle.name; // Trigger an error if the style doesn't exist
    } catch (e) {
        // If it doesn't exist, create it
        charStyle = doc.characterStyles.add({ name: styleName });
    }

    // Reset the character style to avoid overrides
    charStyle.fillColor = "Registration"; // or set your preferred color
    return charStyle;
}

// Main script logic
function applyCharacterStyle(doc, pattern, charStyle) {
    app.findTextPreferences = NothingEnum.nothing; // Clear previous find settings
    app.changeTextPreferences = NothingEnum.nothing;

    // Set the GREP pattern for the search
    app.findTextPreferences.findWhat = pattern;

    // Use findGrep() to perform the search and return matching text
    var foundTexts = doc.findGrep();  // Perform the GREP search here directly
    for (var i = 0; i < foundTexts.length; i++) {
        // Apply the character style to each found text
        foundTexts[i].appliedCharacterStyle = charStyle;
    }

    // Clear preferences after the search
    app.findTextPreferences = NothingEnum.nothing;
    app.changeTextPreferences = NothingEnum.nothing;
}

// Run the script
var doc = app.activeDocument;
var charStyleName = "dontChange";

// Create or retrieve the character style once
var charStyle = getOrCreateCharacterStyle(doc, charStyleName);


var extentions =  "(txt|md|rtf|doc|docx|odt|xls|xlsx|csv|ppt|pptx|pdf|epub|mobi|jpg|jpeg|png|gif|bmp|tiff|svg|ai|eps|mp3|wav|flac|mp4|mkv|avi|mov|zip|rar|7z|tar|gz|iso|html|css|js|json|xml|sql|sh|bat|ps1|exe|dll|sys|ini|log|dmp|indd|idml|dwg|dxf|ttf|otf|woff|woff2|eot)";

// Define patterns to apply the character style
var patterns = [
    "\\<[^[:space:]]+://[^[:space:]]*\\>",
    "\\<[^[:space:]]+@[^[:space:]]*\\>",
    "\\<[^[:space:]]+\\." + extentions + "+[^[:space:]]*\\>"
];

for (var j = 0; j < patterns.length; j++) {
    // Set the pattern and search with GREP
    app.findTextPreferences.findWhat = patterns[j];
    applyCharacterStyle(doc, patterns[j], charStyle);
}