6 Replies Latest reply on Aug 4, 2015 1:44 PM by StevenWilliamsEcklers

    How can I tailor this GREP script to find & replace active open document?

    StevenWilliamsEcklers
      // Get the current selection
      var doc   = app.activeDocument;
      var sel   = doc.selection;
      var frame = sel[0];
      
      
      if (!frame || frame.constructor.name !== "TextFrame") {
            var msg = "Please select a text frame containing";
            msg += " the story you wish to format";
            alert(msg);
      } else {
            var story = frame.parentStory;
      
      
            // Reset the Find/Change options
            app.findGrepPreferences = NothingEnum.nothing;
            app.changeGrepPreferences = NothingEnum.nothing;
      
      
            // Remove all soft returns, then replaces all double spaces to single spaces
            app.findGrepPreferences.findWhat = " \s+\n";
            app.changeGrepPreferences.changeTo = " ";
            story.changeGrep();
          
            // Remove all multiple spaces
            app.findGrepPreferences.findWhat = "\s+";
            app.changeGrepPreferences.changeTo = " ";
            story.changeGrep();
      
      
            // Split Listings WITH Images
            app.findGrepPreferences.findWhat = "(\d\d-\d+.*)\t(?!~a)(.*)\t(.*)\r";
            app.changeGrepPreferences.changeTo = "$2\r$1\t$3\r";
            app.changeGrepPreferences.appliedParagraphStyle = "Description";
            story.changeGrep();
          
      //      // Split Listings WITHOUT Images
      //      app.findGrepPreferences.findWhat = "(?<=~a)(.*)\t(.*)\t(.*)\t(.*)\r";
      //      app.changeGrepPreferences.changeTo = "$1 $2\r$3\t$4\r";
      //      app.changeGrepPreferences.appliedParagraphStyle = "Description";
      //      story.changeGrep();
          
            // Format Pricelines
            app.findGrepPreferences.findWhat = "^(\d\d-\d+-\d\t.*)\r";
            app.changeGrepPreferences.changeTo = "$1\r";
            app.findGrepPreferences.appliedParagraphStyle = "Description";
            app.changeGrepPreferences.appliedParagraphStyle = "Priceline";
            story.changeGrep();    
      }