10 Replies Latest reply on Aug 4, 2009 3:20 PM by mattaca

    Set all text in brackets as Notes?

    mattaca Level 2

      I created the following search string to find the shortest match of text in brackets in my document.

       

      \[.+?\]

       

      What I'd like to do is set these all as notes so the info is still there, but not in the body text of the document. I don't see a way to do it in the Change Format options...is there a way to do it in JavaScript? I have an existing Find/Change script I could add the command to if someone can help me out.

       

      I appreciate any help!

       

      Matthew

        • 1. Re: Set all text in brackets as Notes?
          Thomas B. Nielsen Level 3

          This will convert all text in brackets (your GREP) to notes, and remove the brackets from the text in the note.

           

          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 = "\\[.+?\\]"; // every backslash should be doubled (special char in javascript).

          var myFoundItems = myDocument.findGrep();

           

          //Clear the find/change preferences after the search.
          app.findGrepPreferences = NothingEnum.nothing;
          app.changeGrepPreferences = NothingEnum.nothing;
          for(i = myFoundItems.length-1; i >= 0; i--) {
              var myNote = myFoundItems[i].convertToNote();
              // Delete the [ ]
              myNote.characters.firstItem().remove();
              myNote.characters.lastItem().remove();
          }

           

          --

          Thomas B. Nielsen

          http://www.nobrainer.dk

          1 person found this helpful
          • 2. Re: Set all text in brackets as Notes?
            mattaca Level 2

            Thank you for the help. The script you provided set many of the brackets to notes, but half-way through I got the following error:

             

             

            JavaScript Error!

             

            Error Number: 41990

            Error String: Can't create object at this location

             

            File: /Users..../Brackets to notes.jsx

            Line: 25

            Source: var myNote = myFoundItems

            [i].convertToNote();

             

             

            It looks like it happened when it reached the end of the document. Does the cursor need to be at the beginning of the document for the script to work correctly?

             

            I should have also mentioned I don't want set as notes brackets that begin a paragraph. For that reason I had put a single space at the beginning of my search string, though you probably could tell that from my initial post. Would it be very hard to change the code to fix those two issues?

             

            I'm sorry I know almost nothing about Javascript. I guess this is a good impetus to learn.

             

            Matthew

            • 3. Re: Set all text in brackets as Notes?
              mattaca Level 2

              Can anyone with the know-how help on this? I would really appreciate it.

              • 4. Re: Set all text in brackets as Notes?
                Thomas B. Nielsen Level 3

                This should atleast give you the string that fails to be converted to a Note, and after the alert continue the script.

                 

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

                 

                //Clear the find/change preferences after the search.
                app.findGrepPreferences = NothingEnum.nothing;
                app.changeGrepPreferences = NothingEnum.nothing;
                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");
                    }

                 

                }

                1 person found this helpful
                • 5. Re: Set all text in brackets as Notes?
                  mattaca Level 2

                  Thanks a lot! It appears to be working now. I do have one follow-up question.

                   

                  Is there a way to also set the paragraphs that begin with brackets as notes at the beginning of the following paragraph? The brackets should always contain the entire paragraph. For example, the text might say something like:

                   

                  [Photo 1: pickup image from ND06, p. 23]

                  Caption 1. Put the chicken in the pan, skin side up, and braise for 10 minutes.

                   

                  I would love it if I can have the bracketed info be a note at the beginning of the caption so it's not in the running text. Is this complicated or easy to do?

                   

                  I really appreciate all your help!

                  • 6. Re: Set all text in brackets as Notes?
                    mattaca Level 2

                    Can anyone help with this? I tried to copy some lines of code and change the search to "\\[.+?\\]~b" but it caused an error! I don't know enough about JavaScript to pull this off on my own.

                    • 7. Re: Set all text in brackets as Notes?
                      Thomas B. Nielsen Level 3

                      I'm not sure what your problem is.

                       

                      You need to make a GREP search that will find all the desired bracketet texts.

                      Do this in the find by grep interface of Indesign.

                       

                      If you need help doing that, please post some text, and describe what the grep should do (i'm no good at grep).

                       

                       

                      Example:

                      [Photo 1: pickup image from ND06, p. 23]
                      Caption 1. Put the chicken in the pan, skin side up, and braise for 10 minutes.

                       

                      To convert "[Photo 1: pickup image from ND06, p. 23]" including the new line char to a note use this grep in the script:

                      \\[.+?\\]\\r

                       

                      (This will not remove the closing bracket, since the last char in the note is now the line feed. It will also not find any multiline text included in brackets)

                      • 8. Re: Set all text in brackets as Notes?
                        mattaca Level 2

                        Sorry, Thomas. Just to catch you up...the script you posted works great. It finds all text in brackets and convert them to notes when they are preceeded by a space (because there is a space in the search string "  \\[.+?\\]"). What I'd like to do is append some additional lines of code to the script you posted that also finds all bracketed text that is not preceeded by a space (because it starts the paragraph) and comprises the entire paragraph. I know that the GREP I need to use is "^\\[.+?\\]$", but I don't know enough Javascript to code it properly.

                         

                        You can not convert text to notes using Find/Change settings, which is why I would like it to be a script. It doesn't matter if the brackets stay in the notes are not, as long as the information that's inside the brackets gets converted.

                        • 9. Re: Set all text in brackets as Notes?
                          Thomas B. Nielsen Level 3

                          This will run two searches, is this what you need?

                           

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

                          • 10. Re: Set all text in brackets as Notes?
                            mattaca Level 2

                            That does it! Thank you thank you thank you!

                             

                            I had to change the $ to a ~b because I want the notes to be inserted at the beginning of the next paragraph, but other than that, it works perfect. Thank you so much!

                             

                            If you ever need any GREP assistance, don't hesitate to ask.

                             

                            Matthew