5 Replies Latest reply on Mar 31, 2010 3:04 PM by [Jongware]

    Finding notes and converting to text

    jmw107 Level 1

      Hi-- I was hoping someone could help me consider my options on this one:


      Here's a script that finds text using GREP and converts the found text into notes:


      var the_story = app.selection[0].parent;
      app.findGrepPreferences = NothingEnum.nothing;
      app.changeGrepPreferences = NothingEnum.nothing;
      app.findChangeGrepOptions.includeFootnotes = false;
      app.findChangeGrepOptions.includeHiddenLayers = false;
      app.findChangeGrepOptions.includeLockedLayersForFind = false;
      app.findChangeGrepOptions.includeLockedStoriesForFind = false;
      app.findChangeGrepOptions.includeMasterPages = false;
      app.findGrepPreferences.findWhat = "<\/\\w>";
      var myFoundItems = the_story.findGrep();
      app.findGrepPreferences = NothingEnum.nothing;
      app.changeGrepPreferences = NothingEnum.nothing;

      for(i = myFoundItems.length-1; i >= 0; i--) {




      Now, let's say I want to reverse this and find notes that match this GREP and convert them to text. Simply changing convertToNote() to convertToText() is not going to work because I would be telling the script to convert text into text.


      Any ideas how I could approach this?



        • 1. Re: Finding notes and converting to text
          [Jongware] Most Valuable Participant

          GREP cannot search for notes, so that's a dead end. Approach it from the other end: loop over a document's collection of notes (actually, each stories' collection) and convert them to text, one by one.


          for (story=0; story<app.activeDocument.stories.length; story++)
               while (app.activeDocument.stories[story].notes.length > 0)

          • 2. Re: Finding notes and converting to text
            jmw107 Level 1

            Thanks ... that works nicely for converting all notes to text (which is a handy script to have).


            We are in a situation where we only want notes that meet a certain criteria converted to text. Since we cannot use GREP for this, I am not sure there's an answer -- unless we can find some way to use the notes "contents" (but again, without GREP, doesn't seem likely).

            • 3. Re: Finding notes and converting to text
              [Jongware] Most Valuable Participant

              GREP doesn't find text in notes (wait ... let me test that first!


              Okay, time to interrupt myself. GREP doesn't find text in notes, except when you are in the Story Editor. (!! -- that did ask for a few exclamation points, didn't it). However, it's pretty worthless, because you cannot tell right away if you are inside a note. I bet someone else could come up with a way, but perhaps approaching it from the Notes collection might work as well.


              End of interrupting myself.)


              A "Note"'s contents is not, as you might expect, a simple text string. It can contain fully formatted InDesign text, which is easily proven, as you can select a paragraph with an italic word somewhere in it, convert to note, convert back to text -- and hey! the italics is still in place.


              The JS Help lists its contents as "texts". You still can't find text in "texts" (it's an array of formatted objects), but you can using its "contents" (which is, finally, the contents of the note in plain text -- which is what you need for Javascript).


              So you can re-write the script to inspect all notes for your criteria, and convert only those that match. I'm assuming, by the way, that the criterium is some text inside the note?


              for (story=0; story<app.activeDocument.stories.length; story++)
                  currNote = app.activeDocument.stories[story].notes.length;
                  while (currNote > 0) 
                      if (testMyCriterium (app.activeDocument.stories[story].notes[currNote-1]) == true)
              function testMyCriterium (some_note)
                 // test for the presence of "jongware" ...
                 if (some_note.texts[0].indexOf("jongware") > -1) // Found!
                  return true;
                return false;


              I completely made this up right now, so it might contain bugs But the general gest should be sound.

              • 4. Re: Finding notes and converting to text
                jmw107 Level 1

                Thanks very much.


                My apologies, I am a bit of a newbie on javascripting. I am getting a "not a function" error on "if (some_note.texts[0].indexOf("jongware") > -1)".


                What's the best way to work out that error?

                • 5. Re: Finding notes and converting to text
                  [Jongware] Most Valuable Participant

                  Good question. That line ought to work ...

                  Try this: throw in an alert, so you can see if the value that it gets fed with is okay:

                  alert (some_note);

                  ought to say "[Object Note]" (or something to that effect).

                  alert (some_note.texts[0]);

                  ought to say "[Object Text]", and

                  alert (some_note.texts[0].length);

                  ideally should say something like "2500". That is, if there is actually any text at all in that particular note.


                  Ow forget that!


                  Sorry -- in the explanation I explain you cannot test the "object text" with Javascript, because it's not a string, and add that you should test its "contents" instead. Then I did it wrong myself:


                  if (some_note.texts[0].contents.indexOf("jongware") > -1)



                  and the error simply states that "indexOf" is not a part of the Text class