4 Replies Latest reply on Nov 10, 2014 10:51 AM by KenWK

    See a bookmark with a script

    KenWK Level 1

      I have a script that adds XML tags and I want to be able to know whether the paragraph has a bookmark at the start of it.

       

      I'm working in Windows with VBA and if I get the AscW (the Unicode character code) of the first character in the paragraph it returns -257 for a bookmark. The trouble is it also returns that for the start or end of an XML tag.

       

      I've checked the idSpecialCharacters enumeration but I don't see anything there. I also tried searching for a bookmark but I don't see anyway to do that even manually.

       

      Is there a way to determine what the special thing is in a script?

       

      Thanks,

      Ken

        • 1. Re: See a bookmark with a script
          Jump_Over Level 5

          Hi,

           

          Did you try to check paragraph.notes[0].storyOffset.index?

           

          if it is equal to paragraph.index ==> note is placed at paragraph's starting insertionPoint.

           

          Jarek

          • 2. Re: See a bookmark with a script
            KenWK Level 1

            Currently I've started this production method using bookmarks, not notes. The notes, however, might be a more viable option since their in-line indicator's are more visible and, as you point out, Jump_Over, I can check a paragraph or even an insertion point to see whether it contains notes.

             

            Going to investigate the notes a bit but my original question is still open. Any way to know that three is a bookmark in a particular paragraph or other piece of text.

             

            Thanks again,

            Ken

            • 3. Re: See a bookmark with a script
              [Jongware] Most Valuable Participant

              There seems to be no link from a certain Bookmark code back to the bookmark -- much like with XML markers, hyperlinks, and index markers. Fortunately, typically there would not be very many bookmarks in a single document, and so you can test each one's destination index (which points to a Text index) against your current character position, or simply against the start and end of the current paragraph (only if you are sure to have only one bookmark per paragraph).

               

              Traversing the bookmark list linearly is not easy because it is a nested tree structure. You could walk the entire tree every time you need to find an index, but it's easier to build a flat list only once, when starting your script, and then test on that. Flattening the list can be done with a simple recursive function; see makeBmList below.

              I think the only type you need to test for is HyperlinkTextDestination; the other destination types will never occur as a code inside text.

               

              My example code shows all bookmark candidates (those that point at text) and then scans the list again, comparing it against the current paragraph bounds. Be careful: this code assumes all bookmarks point into the same story! If not, you need to add a check and compare the bookmark's destination parentStory against the one in your selection.

               

              allBms = makeBmList (app.activeDocument);
              t = [];
              for (i=0; i<allBms.length; i++)
                  t.push (allBms[i].name);
              alert (t.join('\r'));
              
              par = app.selection[0].paragraphs[0];
              for (i=0; i<allBms.length; i++)
              {
                  if (allBms[i].destination.destinationText.index >= par.index &&
                      allBms[i].destination.destinationText.index < par.characters[-1].index)
                      alert (allBms[i].name);
              }
              
              function makeBmList (source)
              {
                  var i, list = [];
                  for (i=0; i<source.bookmarks.length; i++)
                  {
                      if (source.bookmarks[i].destination instanceof HyperlinkTextDestination)
                        list.push (source.bookmarks[i]);
                      list = list.concat (makeBmList (source.bookmarks[i]));
                  }
                  return list;
              }
              
              • 4. Re: See a bookmark with a script
                KenWK Level 1

                Thanks Jongware, that's what I had come down to. I was planning on going through the bookmarks collection and using, as you suggest, the insertion point to figure out where things were.

                 

                I ended up, however, having to use the Notes Jump_Over mentioned. Another part of the script breaks stories that flow through many text frames into a story for each text frame by duplicating the frame (among other things) which deletes the bookmarks. I'm assuming because they need to have unique names. It took some doing to get that split story part workng as needed and I'm not about to try and rewrite that.

                 

                Fortunately the Notes "survive" the split.

                 

                Thanks again for the responses,

                Ken