Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

[CS4 JavaScript] Cross references

Community Expert ,
Dec 18, 2008 Dec 18, 2008
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
TOPICS
Scripting
2.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 18, 2008 Dec 18, 2008
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 18, 2008 Dec 18, 2008
Hi Dave,

Thanks for the code. This should get me started.

Rick
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 18, 2008 Dec 18, 2008
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 18, 2008 Dec 18, 2008
You can specify the paragraph in any of the various ways that a paragraph can be referenced:

myFinds.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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 18, 2008 Dec 18, 2008
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!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 18, 2008 Dec 18, 2008
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 06, 2009 Jan 06, 2009
LATEST
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines