Skip to main content
December 17, 2012
Answered

How create CrossReference link to paragraphs or text anchor to another indesign file ?

  • December 17, 2012
  • 1 reply
  • 6821 views

Hi

I am new in indesign scripting.

I want to create CrossReference, which is link to Paragraphs (or Text Anchor) of another indesign file ( new.indd ).

My first problem is how get All Paragraphs or Text Anchor of new.indd file

I have a TextFrame whose content is Adobe Indesign.

I want to apply a CrossReference on Adobe Indesign link to Paragraphs with Destination Documents : new.indd.

How it is done by script ?

I am try using this :

#target indesign

var myDocument = app.documents.add();

var myTextFrame = myDocument.pages.item(0).textFrames.add();

myTextFrame.geometricBounds = ["10p", "15p", "30p", "35p"];

myTextFrame.contents = "Adobe Indesign";

var text =myTextFrame.texts.firstItem();

var hyperlinkURLDestination =myDocument.hyperlinkURLDestinations.add("C/Documents and Settings/Administrator/Desktop/new.indd");

///////////var DestinationDoc=hyperlinkURLDestination.parent();

var xRefForm = myDocument.crossReferenceFormats.item("Page Number");

var source = myDocument.crossReferenceSources.add(text, xRefForm);

var myLink =myDocument.hyperlinks.add(source, hyperlinkURLDestination);

When I run this script it give error  "The destination is invalid. You can only create Cross- Reference to a Text Anchor or paragraph destination".

How I get All Paragrapgs or Text Anchor of new.indd file  and  create Cross-Reference using this.

Thanks.

This topic has been closed for replies.
Correct answer Peter Kahrel

You can create cross-references only to anchors and paragraphs. So you need to create a text anchor in new.indd, then use that as the target of the cross-reference:

var anchor = app.documents.item ('new.indd').hyperlinkTextDestinations.item ('myAnchor');

var source = app.activeDocument.crossReferenceSources.add (text, xRefForm);

app.activeDocument.hyperlinks.add (source, anchor);

Peter

1 reply

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
December 17, 2012

You can create cross-references only to anchors and paragraphs. So you need to create a text anchor in new.indd, then use that as the target of the cross-reference:

var anchor = app.documents.item ('new.indd').hyperlinkTextDestinations.item ('myAnchor');

var source = app.activeDocument.crossReferenceSources.add (text, xRefForm);

app.activeDocument.hyperlinks.add (source, anchor);

Peter

December 18, 2012

Thanks Peter for reply

It work well in Text Anchor case. But in case of  Paragraph what is change in this :

var anchor = app.documents.item ('new.indd').hyperlinkTextDestinations.item ('myAnchor');

I try to replace 'myAnchor'  with paragraph text

#target indesign

var myDocument = app.documents.add();

var myTextFrame = myDocument.pages.item(0).textFrames.add();

myTextFrame.geometricBounds = ["10p", "15p", "30p", "35p"];

myTextFrame.contents = "Adobe Indesign";

var text =myTextFrame.texts.firstItem();

var xRefForm = myDocument.crossReferenceFormats.item("Paragraph Text");

var anchor = app.documents.item ('new.indd').hyperlinkTextDestinations.item ('You can create cross-references only to anchors and paragraphs.');

var source = app.activeDocument.crossReferenceSources.add (text, xRefForm);

app.activeDocument.hyperlinks.add (source, anchor); //give error

but it give error

"Invalid value for parameter 'hyperlinkDestination' of method add. Ex....HyperlinkURLDestination or paragraph destination but recived nothing".

How get paragraph destination from new.indd file to create cross- reference ?

Thanks.

Peter Kahrel
Community Expert
Community Expert
December 18, 2012

Ah, I see, I read your post too quickly. Whether the reference is a whole paragraph or something else is determined solely by the cross-reference format. What you aim at is not important, so long as it is some text object. This works for me:

xRefForm = app.activeDocument.crossReferenceFormats.item("Paragraph Text");

target_text = app.documents.item ('new.indd').hyperlinkTextDestinations.item('par5').destinationText

destination = app.activeDocument.paragraphDestinations.add (target_text);

source_text = app.activeDocument.stories[0].insertionPoints[-1];

source = app.activeDocument.crossReferenceSources.add (source_text, xRefForm);

app.activeDocument.hyperlinks.add (source, destination);

So you need to create a destination to a text object. You tried creating a destination to a text anchor. In other words, create the destination to the anchors destinationText.

But you can get paragraph destinations in various ways. This works too:

target_text = app.documents.item ('new.indd').stories[0].paragraphs[1];

And so does this:

target_text = app.documents.item ('new.indd').findGrep()[0];

Peter