Discussion:
[CS4 JavaScript] Cross references
(too old to reply)
R***@adobeforums.com
2008-12-18 17:16:57 UTC
Permalink
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
D***@adobeforums.com
2008-12-18 17:42:48 UTC
Permalink
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
R***@adobeforums.com
2008-12-18 19:59:59 UTC
Permalink
Hi Dave,

Thanks for the code. This should get me started.

Rick
R***@adobeforums.com
2008-12-18 20:50:42 UTC
Permalink
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
D***@adobeforums.com
2008-12-18 21:01:03 UTC
Permalink
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
R***@adobeforums.com
2008-12-18 21:46:29 UTC
Permalink
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!
D***@adobeforums.com
2008-12-18 22:00:30 UTC
Permalink
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
R***@adobeforums.com
2009-01-06 20:07:26 UTC
Permalink
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

Continue reading on narkive:
Loading...