14 Replies Latest reply on Jan 23, 2009 7:27 AM by (huksy)

    Adding Bookmarks

    Glen_Bal Level 1
      Hello,

      I am a Newbie at Indesign scripting and I am trying to add bookmarks.
      I have 2 problems i'm stuck with:
      Problem 1:
      When i create a bookmark from a paragraph, with the text of that paragraph as the name of the bookmark. The special characters like tabs, non-breakable-spaces and returns get transformed into squares in the name of the bookmark. Am I doing something wrong.

      Problem 2 (not really a problem, but a question):
      What is the correct way to point to a specific paragraph? Do i make a hyperlinkTextDestination (the InsertionPoint) first and assign the bookmark to it? Or am i doing it wrong?

      Can you help me?
      Thanks in advance!!!

      OS: Windows
      app: Indesign
      language: javascript

      Glen Bal
        • 1. Re: Adding Bookmarks
          [Jongware] Most Valuable Participant
          >squares in the name of the bookmark. Am I doing something wrong.

          No. Bookmarks are designed to hold simple text only -- no formatting, font, or (indeed) tabs and other special spaces. I solve this by storing the text in a string, replacing all of these with a simple single space, and using the string as bookmark name.

          >What is the correct way to point to a specific paragraph? Do i make a hyperlinkTextDestination (the InsertionPoint) first and assign the bookmark to it?

          It is a bit confusing. I remember going through it line by line until it finally worked. This comes from my code:

          h = app.activeDocument.hyperlinkTextDestinations.add(theStory.paragraphs[i]);
          
          bm = level2bm.bookmarks.add(h);
          bm.destination.name = "Hi!";
          bm.name = "Actual text for the bookmark";
          • 2. Re: Adding Bookmarks
            Glen_Bal Level 1
            Thank you for your help!!!

            Greetz,
            Glen
            • 3. Re: Adding Bookmarks
              Glen_Bal Level 1
              I still have one more question.

              What is the best way to do replacements in a string? (For changing the special spaces and tabs to a simple space)

              Thanks
              Glen
              • 4. Re: Adding Bookmarks
                [Jongware] Most Valuable Participant
                "Best" way would be using inline grep in your javascript. I'm getting pretty good with grep but not using it inside JS ... So what I do is locate each 'bad' character with indexOf, then cut up the string with substr. A typical generic programmer's solution -- JS (aficiano.. afici..) admirers may laugh.
                • 5. Re: Adding Bookmarks
                  Peter Kahrel Adobe Community Professional & MVP
                  To replace all kinds of spaces to 'normal' spaces, do this:

                  s = s.replace (/\s+/g, " ");

                  \s matches all white space, including em- and en-spaces, tabs, and returns.

                  Peter
                  • 6. Re: Adding Bookmarks
                    [Jongware] Most Valuable Participant
                    That's exactly what I meant. Cheers, Peter!
                    • 7. Re: Adding Bookmarks
                      Glen_Bal Level 1
                      Thanks Peter and jongware!
                      • 8. Re: Adding Bookmarks
                        Is there also a possibility to replace XML-Tags? Otherwise there are plenty of squares in the bookmark-name. :-/

                        H.
                        • 9. Re: Adding Bookmarks
                          [Jongware] Most Valuable Participant
                          Sure. For some reason, XML tags are substituted with 0xFEFF when converted to JS strings (both open and close tags). Knowing that,

                          >s = s.replace (/[\s+\ufeff]/g, " ");

                          should do the trick. [Untested.]
                          • 10. Re: Adding Bookmarks
                            Level 1
                            Right! Tags are coded as "0xFEFF"! <br />But with your suggestion you just find "e" and "f"!?<br /><br />... I already tried <FEFF> but this failed too!? :-(<br /><br />:-)<br />M.
                            • 11. Re: Adding Bookmarks
                              [Jongware] Most Valuable Participant
                              Oh blast. The [Untested] gave it away, didn't it?<br />In InDesign and its immediate surroundings there are several different syntaxes to refer to a Unicode character.<br /><br />* Inside InDesign (its Search & Replace box, for example) it is "<FEFF>".<br /><br />* In GREP Find it is "\x{feff}" (although you can use "\xAB" if the higher byte is zeros).<br /><br />However, you cannot <br />i try<br />these methods, because .. in the InDesign document, the tag is not recoded to FEFF (yet).<br /><br />* In JavaScript it should've been "\ufeff" -- this inserts the actual character into the javascript stream, so your JS parser should've read "\", "s", "<FEFF>".<br /><br />* I'm probably missing a few ways.<br /><br />Ehm -- and it tries to replace the tag with a space, which is quite possibly not what you want. Does this<br /><br />>s = s.replace (/\ufeff/g, "");<br /><br />replace it with nothing? If it doesn't, you'll have to find an alternative way of specifying the blasted character.
                              • 12. Re: Adding Bookmarks
                                Level 1
                                Nope, didn't work at all!?
                                • 13. Re: Adding Bookmarks
                                  [Jongware] Most Valuable Participant
                                  As I said,

                                  >you'll have to find an alternative way [..]

                                  Some (a lot) educated guesses later:

                                  >s = s.replace (/\uFEFF/g, "");

                                  i does
                                  work -- suddenly, slash-u notation is case-specific. It must be because this is a grep expression inside a javascript inside indesign. Or something.
                                  • 14. Re: Adding Bookmarks
                                    Level 1
                                    Many thanks, Jong!
                                    ... and sorry, I read your last answer too fast!

                                    Now I've got another issue: I didn't found a method to sort the bookmarks? Did anyone got an idea how to solve this problem? ... my bookmarks are pretty messed up! :-/

                                    H.