Find hyperlink text source from destination

Contributor ,
Sep 23, 2012 Sep 23, 2012

Copy link to clipboard

Copied

If you go to a hyperlink text destination and look in the story editor you should be able to see a little symbol that looks like a rounded corner square with a circle and cross hairs in it. I assume this is an invisible text element which is used to link it to the source.

What I want to be able to do is select one of these elements and using JavaScript select the source element... However I am having a hard time getting the script to recognise this element as anything... Let alone find out its relationship to a source.

I hope this is explanatory enough.

TOPICS
Scripting

Views

6.8K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Oct 10, 2012 Oct 10, 2012

Hi all

Back from my break.

I figured out the 2 problem.

1) How to get the hyperlink sources from the destinations, i.e. you can't so do it the other way round like Pickory suggested.

2) How to search for the Destination symbol that can be seen on the story editor but doesn't have a hidden character symbol.

This cannot be done with the indesign GREPs as there is no symbol on the main screan (Peter correct me if I'm wrong) but can be found with a javascript regex

reg = /\ufeff/g;

q = mySelection.contents

...

Likes

Translate

Translate
Guru ,
Sep 23, 2012 Sep 23, 2012

Copy link to clipboard

Copied

var doc = app.activeDocument;

// the 1st hyperlink

var h1 = doc.hyperlinks[0];

// Jumps to the hyperlink source and selects it

h1.showSource();

// Jumps to the hyperlink destination but doesn't select it

h1.showDestination();

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 24, 2012 Sep 24, 2012

Copy link to clipboard

Copied

Hi Kasyan,

Can you fill in the gap?

I.e. How to get the contents of a specific hyperlinks destination?

var myHyperlinks = app.selection[0].findHyperlinks(), l = myHyperlinks.length, myAlert = "";

while (l--) myAlert += "Source Text: " + myHyperlinks.contents

               + "\tDestination Text: " + ??????????? +"\r"; // What to put here ??????

alert (myAlert)

Thanks

Trevor

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 24, 2012 Sep 24, 2012

Copy link to clipboard

Copied

Hi Trevor,

Assuming we’re talking about hyperlinks pointing to text anchors -- in theory, this should work but in practice it doesn’t.

Main();

function Main() {

    var hyperlink, destination, source,

    doc = app.activeDocument,

    hyperlinks = doc.hyperlinks;

    for (var i = 0; i < hyperlinks.length; i++) {

        hyperlink = hyperlinks;

        $.writeln("-------------------------------\r" + i + "\tHyperlink Name: " + hyperlink.name);

        destination = hyperlink.destination;

        source = hyperlink.source;

        if (destination.destinationText.constructor.name == "InsertionPoint") {

            $.writeln("\tDestination -- Insertion Point");

        }

        else {

            $.writeln("\tDestination Text: " + destination.destinationText.contents);

        }

   

        $.writeln("\tSource Text: " + source.sourceText.contents);

   

    }

}

According to the scripting reference, destinationText can be either InsertionPoint or Text. In fact, it always returns Insertion Point so we can't get the contents.

Regards,

Kasyan

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 24, 2012 Sep 24, 2012

Copy link to clipboard

Copied

Hi Kasyan,

Thanks for that,

Destination.name will give the text PROVIDING that the name is the text contents.

There were some interesting things I obseved on this topic, but I'm off for a couple of weeks so will not be in touch until then

Bye

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 24, 2012 Sep 24, 2012

Copy link to clipboard

Copied

I kinda pointed this out in my OP. If you look in the story editor a destination is shown by a single icon (rounded corner square with a circle and cross hairs in it)... as opposed to the source which is identified by a pair of icons with a chain link icon in them. In the case of a source these wrap around content... The destination is a single point, it shold not contain text.

What I want to be able to do is select the destination, the single icon (you can do this most easily through story editor) and then using JavaScript identify the source or sources.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 25, 2012 Sep 25, 2012

Copy link to clipboard

Copied

I think it's impossible to do .

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Sep 25, 2012 Sep 25, 2012

Copy link to clipboard

Copied

Hello,

Maybe try it the other way round. Get all the hyperlink sources and find which goes to this destination.

P.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 25, 2012 Sep 25, 2012

Copy link to clipboard

Copied

Ok I looked up the technical name of the symbol in Adobe's hidden character list so I can refer to it correctly.

http://help.adobe.com/en_US/indesign/cs/using/images/H_Bookmark_Story.png

This symbol is called the Bookmark or hyperlink destination marker.

It has to be possible to find the source from the destination marker. It is possible possible to find the source from a destination... So all i realy need to do is work out how to get a destination from a destination marker.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 25, 2012 Sep 25, 2012

Copy link to clipboard

Copied

Not all text anchors (a.k.a. hyperlink text destinations) are linked to hyperlinks or cross-references. When you delete a cross-reference or a hyperlink, InDesign leaves any attached text anchor alone, and over time you can (and will) end up with numerous orphaned anchors. (Anchors linked to bookmarks are deleted when the corresponding bookmarks are deleted.) I forget the details now, but this script (http://www.kahrel.plus.com/indesign/text_anchors.html) shows how you can find the connections between anchors and hyperlinks/cross-references.

Peter

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 28, 2012 Sep 28, 2012

Copy link to clipboard

Copied

Thank you, Peter, for another wonderful script.

David

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Oct 14, 2012 Oct 14, 2012

Copy link to clipboard

Copied

McShaman

Was there something not answered here?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 29, 2012 Nov 29, 2012

Copy link to clipboard

Copied

Trevor, sorry for the late responce, I have been out of the country for a while and just going through responce now. Lot of fantastic information there... Thank you very much for your time.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 30, 2012 Nov 30, 2012

Copy link to clipboard

Copied

Hi,

Can someone please help, i need to find hyperlink text destination from source.

I have pargarph object, so i can find HyperlinkTextSource from paragraph using findHyperlinks(), but i need its corresponding HyperlinkTextDestination.

Thanks,


Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 30, 2012 Nov 30, 2012

Copy link to clipboard

Copied

Poo, see this thread: http://forums.adobe.com/thread/1070892?tstart=0 -- I didn't read it in its entirety but your problem is discussed in there, with several solutions and at least one marked "answered".

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Dec 01, 2012 Dec 01, 2012

Copy link to clipboard

Copied

See here for more on this

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 14, 2022 Sep 14, 2022

Copy link to clipboard

Copied

LATEST

Hi Kasyan and others, in search for issues with destinationText I landed to this post and noticed that the issue you mentioned is still an issue in 2022. Even explit setting of destinationText property does not return the Text object. It returns only the InsertionPoint object. Do you or anyone have found any workaround to get the entire destinationText value and not just the first InsertionPoint?

Below is a quick script to depict the issue.

app.documents.add();
var myDoc = app.activeDocument;

myTextFrame = myDoc.textFrames.add({geometricBounds: ["6p", "6p", "15p", "20p"]})
myTextFrame.contents = "This is sample text for target link destination text";

app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "text";

var myFound = myDoc.findGrep();
var sourceText = myFound[0];
var targetText = myFound[1];
myLinkSource = myDoc.hyperlinkTextSources.add(sourceText);
myLinkDestination = myDoc.hyperlinkTextDestinations.add(targetText, {name: "myTarget", destinationText: targetText});
myDoc.hyperlinks.add(myLinkSource, myLinkDestination, {name: "myLink"});

myDoc.textFrames.add({geometricBounds: ["6p", "20p", "15p", "30p"]}).contents = "destinationText value: " + myLinkDestination.destinationText;

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Oct 10, 2012 Oct 10, 2012

Copy link to clipboard

Copied

Hi all

Back from my break.

I figured out the 2 problem.

1) How to get the hyperlink sources from the destinations, i.e. you can't so do it the other way round like Pickory suggested.

2) How to search for the Destination symbol that can be seen on the story editor but doesn't have a hidden character symbol.

This cannot be done with the indesign GREPs as there is no symbol on the main screan (Peter correct me if I'm wrong) but can be found with a javascript regex

reg = /\ufeff/g;

q = mySelection.contents.search (reg);

All this does is find the symbol but which is just a symbol that contains no information, to find out to which destination it is associated one has to compare the story and insertion point coordinated of the symbol with the values given in the destinations.

To do this quickly I make a table (see script)

There is a problem with this is that the values or static which is ok for creating an index or something like that but if one changes the main text the story / insertion point of the symbol will change and the script won't find the destination associated with the symbol.  I this is a problem enjoy re-scripting it.

Regards,

Trevor.

P.s.

MAKE SURE THAT THE JAVASCRIPT CONSOLE IS OPEN OR YOU WILL NOT SEE THE SCRIPT DO ANYTHING!!!

// Hyperlink Destination Source Script byTrevor http://forums.adobe.com/message/4721797#4721797

// MAKE SURE THAT THE JAVASCRIPT CONSOLE IS OPEN !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

// SCROLL  UP THE CONSOLE UNTIL YOU SEE (:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)

var myDoc = app.documents[0],

myDestinations = myDoc.hyperlinkTextDestinations.everyItem().getElements(),

mySources = myDoc.hyperlinkTextSources.everyItem().getElements(),

myHyperLinks = myDoc.hyperlinks.everyItem().getElements(),

dl = dll = myDestinations.length,

hl = hll = myHyperLinks.length,

myDestinationsSearchTable = [],

myHypers = {}, hyps = [], dests = [], n,

mySelection = app.selection[0];

// Make an index of destionation sources from the hyperlinks that contain both sources and destinations

// This will miss any Destinations that don't have sources

$.writeln ("\r\r(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)\r(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)(:-)\r\rHyperlinks Sources from Destinations Script\r\rThese are my look up keys")

while (hll--)

    {

        if (myHyperLinks[hll].destination != null)

        {

            if (hyps[myHyperLinks[hll].destination.index] == null)

            {

                hyps[myHyperLinks[hll].destination.index] = {};

                hyps[myHyperLinks[hll].destination.index]["Destination"] = myHyperLinks[hll].destination;

                hyps[myHyperLinks[hll].destination.index]["Sources"] = [];

             }

            hyps[myHyperLinks[hll].destination.index]["Sources"].push(myHyperLinks[hll].source);

        }

     }

// make an array of all destinations with what every properties you want to extract i.e. name location sources page number etc.

// add or remove as prperties as needed "DestinationID" is used here for the search look up table

while (dl--)

    {

        dests[dl] = {};

        destIndex = myDestinations[dl].index;

        dests[dl]["DestinationID"] = myDestinations[dl].destinationText.parentStory.index+"x"+myDestinations[dl].destinationText.insertionPoints.lastItem ().index;

        myDestinationsSearchTable[dests[dl]["DestinationID"]]=dl; // makes a look up table to use for search

        $.writeln("myDestinationsSearchTable ["+dests[dl]["DestinationID"]+"] = "+dl);

        if (hyps[dl] != null)

        {

            dests[dl]["HasSource"] = 1;

            dests[dl]["DestinationName"] = hyps[destIndex]["Destination"].name;

            dests[dl]["DestinationIndex"] = destIndex;

            dests[dl]["DestinationInsertionPoint"] = hyps[destIndex]["Destination"].destinationText.insertionPoints;

            dests[dl]["DestinationCharater"] = hyps[destIndex]["Destination"].destinationText.insertionPoints.lastItem ().index;

           

            dests[dl]["DestinationStory"] = hyps[destIndex]["Destination"].destinationText.parentStory.index;

            dests[dl]["DestinationCharaterCode"] = myDoc.stories[dests[dl]["DestinationStory"]].characters[dests[dl]["DestinationCharater"]].contents.charCodeAt(0).toString(16);

            dests[dl]["DestinationPage"] = (hyps[destIndex]["Destination"].destinationText.parentTextFrames[0].parentPage != null) ?  hyps[destIndex]["Destination"].destinationText.parentTextFrames[0].parentPage.name : "Off the Page";;

            dests[dl]["SourceNames"] = [];

            dests[dl]["SourceIndexs"] = [];

            dests[dl]["SourcePages"] = [];

            l = hyps[destIndex]["Sources"].length;

            dests[dl]["Destination"] = hyps[destIndex];

            while (l--)

                {

                    dests[dl]["SourceNames"].push(hyps[destIndex]["Sources"].sourceText.contents);

                    dests[dl]["SourceIndexs"].push(hyps[destIndex]["Sources"].index);

                    (hyps[destIndex]["Sources"].sourceText.parentTextFrames[0].parentPage != null) ?

                    dests[dl]["SourcePages"].push(hyps[destIndex]["Sources"].sourceText.parentTextFrames[0].parentPage.name) :

                    dests[dl]["SourcePages"].push("Off the page");

                  }

             

        }

       else dests[dl]["HasSource"] = 0;

     }

// if there's text selected search for destination anchors

if ( app.documents.length && app.selection.length && mySelection.hasOwnProperty ( 'baseline' ) && mySelection.constructor.name != "InsertionPoint")

    {

        var mySelectionDestinations = [],

        // this is the unicode for the destination symbol

        q=0,

        indexAdjust = mySelection.parentStory.texts.itemByRange (mySelection.parentStory.characters[0], mySelection.characters[0]).characters.length-1,

        myStory=mySelection.parentStory.index,

        reg = /\ufeff/g;

        $.writeln("*********** Destinations in Selection **********")

        while (q != -1)

            {

                q = mySelection.contents.search (reg)

                if (q != -1) // when used in practise remove the if condition and pop array after loop

                    {

                        mySelectionDestinations.push(dests[myDestinationsSearchTable[myStory+"x"+(q+indexAdjust)]]);

                        $.writeln("mySelectionDestinations ["+(mySelectionDestinations.length-1)+"] = myDestinationsSearchTable ["+[myStory+"x"+(q+indexAdjust)]+"] = "+myDestinationsSearchTable[myStory+"x"+(q+indexAdjust)])

                    }

            }

        $.writeln ("################\rThese are these are the destinations in the selection: "+mySelectionDestinations+"\r")

        for (n in mySelectionDestinations) $.writeln (mySelectionDestinations.toSource()+"\r###########\r\r")

    }

  

      

$.writeln ("%%%%%%%%%%%%%%\rThese are all the document's hyperlinks that have destination\r");

for (n in hyps) $.writeln (n+"\t"+hyps.toSource()+"\r--------------------------");

$.writeln("++++++++++++++++++++++++++++++++++++\r\r"+

    "These are all the document's hyperlink destinations with source and other properties\r");   

for (n in dests) $.writeln (n+"\t"+dests.toSource()+"\r**********************");

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines