Copy link to clipboard
Copied
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.
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Thanks Peter for reply.
In new.indd file first paragraph in first text frame on first page is "You can create cross-references only to anchors and paragraphs".
I try as you told but it not work for me in case of paragraphs.
I try :
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').stories[0].paragraphs[1];
//var anchor = app.documents.item ('new.indd').stories[0].paragraphs[0];
//var anchor = app.documents.item ('new.indd').findGrep()[0];
//var anchor = app.documents.item ('new.indd').hyperlinkTextDestinations.item ('par1').destinationText;
//var anchor = app.documents.item ('new.indd').hyperlinkTextDestinations.item ('par0').destinationText();
//var anchor = app.documents.item ('new.indd').hyperlinkTextDestinations.item ('par1').destinationText();
var anchor = app.documents.item ('new.indd').hyperlinkTextDestinations.item ('You').destinationText(); //give error Object is invalid
var destination = app.activeDocument.paragraphDestinations.add (anchor);
var source = app.activeDocument.crossReferenceSources.add (text, xRefForm);
app.activeDocument.hyperlinks.add (source, destination);
Give error : Object is invalid
I have Paragraph text of new.indd and not information of paragraph index.
So i try
var anchor = app.documents.item ('new.indd').hyperlinkTextDestinations.item ('You').destinationText();
but give error .
what is 'par5'?
Is it paragraph index ?
plz tell me what is error in my script ? It work for you but not for me.
Thanks.
Copy link to clipboard
Copied
In new.indd, create a text anchor and name it You. Then in your script, use this line:
var anchor = app.documents.item ('new.indd').hyperlinkTextDestinations.item ('You').destinationText
Peter
Copy link to clipboard
Copied
Thanks for reply
In case of Text Anchor as you told, Script work well and create cross-reference
If new.indd file have a Text Anchor "mynewanchor" then below script work well.
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("Page Number");
var anchor = app.documents.item ('new.indd').hyperlinkTextDestinations.item ('mynewanchor');
var source = app.activeDocument.crossReferenceSources.add (text, xRefForm);
app.activeDocument.hyperlinks.add (source, anchor);
But problem is with Paragraphs.
How create cross-reference using new.indd paragraphs ?
I have Paragraph text and do not know the index of the paragraph.
my paragraph text in new.indd is
"You can create cross-references only to anchors and paragraphs."
Thanks
Copy link to clipboard
Copied
The form of the cross-reference is determined by crossReferenceFormat. So use this:
var xRefForm = myDocument.crossReferenceFormats.item("Paragraph Text");
or whichever cr format displays the paragraph.
Peter
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Well, why don't you target that paragraph then? (I'm getting confused now.) In the script I posted, remove the third line:
doc1.hyperlinkTextDestinations.add(doc1....
Then change this one:
target_text = app.documents.item ('new.indd').hyperlinkTextDestinations.item('myAnchor').destinationText;
as follows:
target_text = app.documents.item ('new.indd').stories[0].paragraphs[0];
But you see the problem: you have to address a paragraph via its parent story, and there can be many stories in a document. That's why I thought you added an anchor to the paragraph you wanted to cross-ref.
Peter
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hi Peter
Thanks for reply
I try this but below line give error object is invalid
var found = app.documents.item ('one.indd').findText();
In Adobe InDesign CS5.5 (7.5) Object Model
Document.findText (reverseOrder: Boolean ):Array of Text
Finds text that matches the find what value.
reverseOrder: Data Type: Boolean
If true, returns the results in reverse order. (Optional)
but give error object is invalid
Thanks.
Copy link to clipboard
Copied
That line will say 'object is invalid' if you don't have a document open called 'new.indd'.
Copy link to clipboard
Copied
Thanks for reply
I try this
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.").destinationText; //error object is invalid
It give error object is invalid
Now I try this
var hyperlinkTextDestination = app.documents.item ('new.indd').hyperlinkTextDestinations.item ("You can create cross-references only to anchors and paragraphs."); //no error
app.activeDocument.paragraphDestinations.add (hyperlinkTextDestination.destinationText);
//error object is invalid
How get hyperlinkTextDestination destination text ?
As you told
target_text = app.documents.item ('new.indd').hyperlinkTextDestinations.item('par5').destinationText
I try this, In place of 'par5' I pass the paragraph text "You can create cross-references only to anchors and paragraphs.".
It give error object is invalid.
Problem is to get destinationText from
hyperlinkTextDestination using paragraph text.
Thanks.