7 Replies Latest reply on Jan 6, 2009 12:07 PM by frameexpert

    [CS4 JavaScript] Cross references

    frameexpert Level 4
      Hello All,

      Is the new cross reference feature of InDesign CS4 scriptable? In other words, is it possible to add and edit cross references with JavaScript? Thanks in advance.

      Rick Quatro
      rick at frameexpert dot com
      585-659-8267
        • 1. Re: [CS4 JavaScript] Cross references
          Level 1
          Yes.

          But finding them in the Object Model is an interesting challenge because they're not called cross references, they're called hyperlinks. Here's a snippet of code I used to make a cross reference using a format I called "Page Number Only" that I had previously created (manually in the UI):
          var destination = doc.hyperlinkTextDestinations.add(dest, {name:termName});
          
          var xRefForm = doc.crossReferenceFormats.item("Page Number Only");
          var sourceText = getSource(para);
          if (sourceText == null) {return } // I don't think this can happen, but just in case
          var source = doc.crossReferenceSources.add(sourceText, xRefForm);
          var myLink = doc.hyperlinks.add(source, destination);
          myLink.visible = false;
          So, it is very similar to making any other kind of hyperlink. First, make your destination (dest in that first statement is a text reference into my document) then make a cross reference source, then make a hyperlink joining the two together.

          Dave
          • 2. Re: [CS4 JavaScript] Cross references
            frameexpert Level 4
            Hi Dave,

            Thanks for the code. This should get me started.

            Rick
            • 3. Re: [CS4 JavaScript] Cross references
              frameexpert Level 4
              Hi Dave,

              In this line:

              var destination = doc.hyperlinkTextDestinations.add(dest, {name:termName});

              what does "dest" refer to? Is it a paragraph object? I actually think I want to do this:

              var destination = doc.paragraphDestinations.add(dest);

              The documentation says that dest is Text, but I am not sure how to specify the paragraph that I want the cross reference to point to.

              On a related point, I would want to know how to see if a ParagraphDestination already exists for a particular paragraph object.

              Thanks,
              Rick
              • 4. Re: [CS4 JavaScript] Cross references
                Level 1
                You can specify the paragraph in any of the various ways that a paragraph can be referenced:

                myFinds[j].paragraphs[0];
                myStory.paragraphs[16];
                app.selection[0].paragraphs[0];

                it all depends on the technique you're using to pick out paragraphs.

                I never noticed pargraphDestinations when I was researching this. I probably could have used that instead, although in my case I was pointing at words within paragraphs. That could matter for long paragraphs that might span from one page to another.

                I'm not sure off the top of my head with how to help with whether or not a paragraph destination already exists. Does it do any harm to make another one? Not sure.

                Dave
                • 5. Re: [CS4 JavaScript] Cross references
                  frameexpert Level 4
                  Yes, this works fine, where oPgf is a paragraph object, and oDoc is the document object:

                  var destination = oDoc.paragraphDestinations.add(oPgf,name:"abc"});

                  Do paragraphs in InDesign have a unique id or serial number? If so, I could use this for the name parameter. That way, if I want to point to a paragaph, I could query the ParagraphDestinations using the paragraph's id to see if a ParagraphDestination already exists for that paragraph.

                  Thanks for your help!
                  • 6. Re: [CS4 JavaScript] Cross references
                    Level 1
                    Not unique. A paragraph is a specially recognized text object identified by its start and end indexes within a story. Edit any part of the story prior to the paragraph and those indexes change.

                    Dave
                    • 7. Re: [CS4 JavaScript] Cross references
                      frameexpert Level 4
                      I have the basics of cross-references; now I want to insert some external cross-references. Here is the basics of what I have working.

                      // destDoc = destination document.
                      
                      // xrefDoc = adding cross-reference to this doc.

                      // Get a pre-existing destination from the destination document.
                      var dest = destDoc.paragraphDestinations.item('2916');

                      // Get the cross-reference format.
                      var xrefFmt = xrefDoc.crossReferenceFormats.item('Full Paragraph & Page Number');

                      // Add the cross-reference source.
                      var source = xrefDoc.crossReferenceSources.add(app.selection[0],xrefFmt);

                      // Add the cross-reference.
                      var xref = xrefDoc.hyperlinks.add(source,dest);


                      This works; however, I am trying to find a way to add a cross-reference without first getting the ParagaphDestination from the destination document. I know the name of the destination document and I know the name of the destination, and I would like to build the external cross-reference without having to open the destination document. Does anyone know if this is possible? Thanks in advance.

                      Rick Quatro
                      585-659-8267