0 Replies Latest reply on Dec 3, 2009 8:42 AM by mattaca

    Change text in parens to notes? (javascript)

    mattaca Level 2

      Hi all. I am newbie to scripting and so I could use some help. On this forum I previously asked for help writing a script converting text in brackets to notes, which is how we want editors to communicate instructions to us that shouldn't be set. Often, though, they forget and put the notes in parentheses. I attempted to solve this problem by adding a couple lines of GREP to the text file referenced by my FindChangeByList script which I use for styling some text before layout:

       

      grep    {findWhat:"\\((.+?)\\)", appliedParagraphStyle:"TasteTest - head"}    {changeTo:"[$1]"}

      grep    {findWhat:"\\((.+?)\\)", appliedParagraphStyle:"Caption - head"}    {changeTo:"[$1]"}

       

      This occasionally causes problems, though, because my find change script isn't perfect. It sometimes applies styles to paragraphs it shouldn't, which in turn means that some text gets converted to notes that shouldn't be. I'm wondering if there's a way to add this function to the script below (graciously provided by Thomas Nielsen!) so that it first looks for parens in paragraph styles that contain the word "head" and converts them to brackets. Or, I supposed it could skip the brackets all together and go straight from parens to notes, but I don't know if that's more complicated as it involves finding parens in multiple paragraph styles.

       

      I am really lost on this one, so any help would be appreciated.

       

      Matthew

       

       

       

       

      var myDocument = app.activeDocument;
      app.findGrepPreferences = NothingEnum.nothing;
      app.changeGrepPreferences = NothingEnum.nothing;
      //Set the find options.
      app.findChangeGrepOptions.includeFootnotes = false;
      app.findChangeGrepOptions.includeHiddenLayers = false;
      app.findChangeGrepOptions.includeLockedLayersForFind = false;
      app.findChangeGrepOptions.includeLockedStoriesForFind = false;
      app.findChangeGrepOptions.includeMasterPages = false;
      //Regular expression for finding an email address.
      app.findGrepPreferences.findWhat = " \\[.+?\\]";
      var myFoundItems = myDocument.findGrep();

       

      for(i = myFoundItems.length-1; i >= 0; i--) {
          try {
              var myNote = myFoundItems[i].convertToNote();
              // Delete the [ ]
              myNote.characters.firstItem().remove();
              myNote.characters.lastItem().remove();
          } catch (e) {
              alert("The string: " + myFoundItems[i].contents + " could not be converted to note");
          }
      }

       

      app.findGrepPreferences.findWhat = "^\\[.+?\\]$";
      var myFoundItemsTwo = myDocument.findGrep();

       

      //Clear the find/change preferences after the search.
      app.findGrepPreferences = NothingEnum.nothing;
      app.changeGrepPreferences = NothingEnum.nothing;

       

      for(i = myFoundItemsTwo.length-1; i >= 0; i--) {
          try {
              var myNote = myFoundItemsTwo[i].convertToNote();
              // Delete the [ ]
              myNote.characters.firstItem().remove();
              myNote.characters.lastItem().remove();
          } catch (e) {
              alert("The string: " + myFoundItemsTwo[i].contents + " could not be converted to note");
          }
      }