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
  • 6793 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 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.

Community Expert
December 24, 2012

Thanks Peter for Reply

I create Cross-Reference as you told

#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 target_text = app.documents.item ('one.indd').stories[0].paragraphs[0];

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

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

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

It create Cross-Reference sucessfully.

But problem is in line 8

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

I have no storie number and Paragraph number information  only Paragraph text is available as info. I can not change anything in "one.indd" file.

Is it possible to get stories and paragraphs  number with the help of ParagraphText ?

Thanks


Then use findText() or findGrep() to find a paragraph:

app.findTextPreferences = null;

app.findTextPreferences.findWhat ="Adobe Indesign";

found = app.documents.item ('one.indd').findText();

if (found.length > 0)

   target_text = found[0].paragraphs[0];

Peter