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

December 20, 2012

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


Thank You very much Peter for reply

I was wating for your reply.

Sorry Peter my problem is not solve

First I want to tell you, what I want to do.

I want to create a cross reference using another file (say  "one.indd" ) paragraph .

In "one.indd" file I create a text Frame and insert a paragraph  and save the file("one.indd") and nothing  done.

Now I crate a another file, in this file I create a text frame with text "Adobe Indesign".

Now I select this text "Adobe Indesign" and go to in menubar

Type -> Hyperlinks & Cross-Refernces -> Insert Cross-Refernce..

New Cross-Reference Dialog is open

In this dialog I select

Link To: Paragraph

Destination

    Document:one.indd

[Basic Pragraph] "one.indd file paragraph"

Cross-Refernce Format

    Format:Paragraph text

Then press Ok

It create a cross-Reference and replace "Adobe Indesign" with "one.indd file paragraph".

I want to do it with Script.

I try  as You told me but problem is that in one.indd I have only paragraph "one.indd file paragraph" no HyperlinkTextDesination or Text Anchor.

so I use paragraph text "one.indd file paragraph" as parameter in :

app.documents.item ('one.indd').hyperlinkTextDestinations.item('one.indd file paragraph').destinationText;

but it give error

To create cross-reference manually there is no need to add hyperlinkTextDestination.

I think it is poaaible because it is done manually.

Thanks