find/change Grep working in GUI but not script

Home Forums Private Member Forum find/change Grep working in GUI but not script

This topic contains 2 replies, has 2 voices, and was last updated by  sledder 1 month, 3 weeks ago.

  • Author
    Posts
  • #70298

    sledder
    Member

    During my script that processes many documents, I have a routine that attempts to perform 2 Grep find/change scenarios, both which will typically exist in each document. The first is to apply character style “DisappearText” to a caret (^), the second to apply character style “Superscript” to the string of letters following the caret. I can perform this via the Find/Change panel in InDesign, as well as applying it to the Paragraph Style manually (which I don’t want to go through an do on each document). If I check the length of .findGrep() on the active document it always returns zero. I’ve also tried specifically getting the textFrames array of the two layers and iterating through them but same result. Here is the normalized script:

    try{
    //perform Grep operations
    var myDoc = app.activeDocument;
    //Clear the find/change grep preferences.
    app.findGrepPreferences = app.changeGrepPreferences = null;
    //Set the find options.
    app.findChangeGrepOptions.includeFootnotes = true;
    app.findChangeGrepOptions.includeHiddenLayers = true;
    app.findChangeGrepOptions.includeLockedLayersForFind = true;
    app.findChangeGrepOptions.includeLockedStoriesForFind = true;
    app.findChangeGrepOptions.includeMasterPages = false;
    //Regex for finding a caret and superscripting letters after it.
    app.findGrepPreferences.findWhat = “(?<=\^)[\l\u]+”;
    app.changeGrepPreferences.appliedCharacterStyle = “Superscript”;
    myDoc.changeGrep();
    //Clear the find/change preferences after the search.
    app.findGrepPreferences = app.changeGrepPreferences = null;
    //Regex for finding a caret and desappearing it.
    app.findGrepPreferences.findWhat = “\^”;
    app.changeGrepPreferences.appliedCharacterStyle = “DisappearText”;
    myDoc.changeGrep();
    app.findGrepPreferences = app.changeGrepPreferences = null;
    } catch(e) {
    alert(“could not complete Grep operation\n” + e);
    }

    Does anyone have an ideas why it would not find and perform the change via script?

  • #70306

    RorohikoKris
    Member

    Make sure to escape special characters in your JavaScript string.

    E.g. to get a \ into a string, you need to type “\\”.

    So the first findWhat should probably be “(?<=\\^)[\\l\\u]+”

    same applies for all backslashes in any other findWhat strings (or any JavaScript): they need to be doubled-up… The second findWhat also needs the same treatment.

    Try that and let us know if that helps!

  • #70309

    sledder
    Member

    You are a champion!! Thanks so much, worked a charm.

You must be logged in to reply to this topic.