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

GREP search problems

Explorer ,
Jan 16, 2025 Jan 16, 2025

Copy link to clipboard

Copied

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.
TOPICS
Bug , Scripting

Views

226

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 1 Correct answer

Guide , Jan 16, 2025 Jan 16, 2025

I mean:

 

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

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

 

[I've not checked your patterns!]

 

(^/)

Votes

Translate

Translate
Community Expert ,
Jan 16, 2025 Jan 16, 2025

Copy link to clipboard

Copied

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

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
Explorer ,
Jan 16, 2025 Jan 16, 2025

Copy link to clipboard

Copied

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);
}

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
Explorer ,
Jan 16, 2025 Jan 16, 2025

Copy link to clipboard

Copied

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(docstyleName) {
    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(docpatterncharStyle) {
    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 = 0i < foundTexts.lengthi++) {
        // 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(doccharStyleName);


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 = 0j < patterns.lengthj++) {
    // Set the pattern and search with GREP
    app.findTextPreferences.findWhat = patterns[j];
    applyCharacterStyle(docpatterns[j], charStyle);
}

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
Guide ,
Jan 16, 2025 Jan 16, 2025

Copy link to clipboard

Copied

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

 

(^/)  The Jedi

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
Guide ,
Jan 16, 2025 Jan 16, 2025

Copy link to clipboard

Copied

I mean:

 

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

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

 

[I've not checked your patterns!]

 

(^/)

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
Explorer ,
Jan 17, 2025 Jan 17, 2025

Copy link to clipboard

Copied

Oh God, that was so dumb mistake... Thank you very much for helping! 

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 17, 2025 Jan 17, 2025

Copy link to clipboard

Copied

LATEST

Haha, yeah I didn't spot that either! Doh!

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