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
  • 6792 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 KahrelCorrect 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.

Community Expert
December 20, 2012

Hi Peter

thanks for reply

I try

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

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

var string =hyperlinkTextDestination.toSource();

var destination=app.activeDocument.paragraphDestinations.add (string);

to get hyperlinkTextDestination and then add it into paragraphDestinations.

But it give error for invalid parameter  for destination add method.

I think that You do not understand my problem.

I have sucessfully create cross-reference using Text-Anchor of another file ( new.indd).With the help of Your script as you told me.

But i want to create cross-reference using paragraphs of another file ( new.indd).Whose ( new.indd) paragraph text is "You can create cross-references only to anchors and paragraphs."

Thanks.


An anchor (a.k.a. hyperlinkDestination) is an object, which in itself cannot be used for a cross-reference. To create a cross-ref, you need a text object. A text anchor's textDestination is an insertion point, which is a text object. That insertion point sits in a paragraph. By creating a cross-reference to a text anchor's destinationText, using the 'Paragraph Text' format, the result is the text of the full paragraph that the anchor sits in.

Run this script:

// Create a document, insert a paragraph, add a text anchor, and save the doc

doc1 = app.documents.add();

doc1.textFrames.add ({contents: 'Text in "new.indd".', geometricBounds: [0,0,'30mm','50mm']});

doc1.hyperlinkTextDestinations.add(doc1.stories[0].insertionPoints[0], {name: 'myAnchor'});

doc1.save (File('/d/test/new.indd'));

// Create another document and add some text

doc2 = app.documents.add();

doc2.textFrames.add ({geometricBounds: [0,0,'30mm','50mm']});

doc2.stories[0].contents = 'The first paragraph in the other document is: ';

// Get a reference to the text anchor in new.indd

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

destination = doc2.paragraphDestinations.add (target_text);

// Create a cross-reference

source_text = doc2.stories[0].insertionPoints[-1];

xRefForm = doc2.crossReferenceFormats.item("Paragraph Text");

source = doc2.crossReferenceSources.add (source_text, xRefForm);

doc2.hyperlinks.add (source, destination);

You should now see The first paragraph in the other document is: “Text in “new.indd”.” in the document.

Peter