Skip to main content
Known Participant
November 8, 2021
Answered

Generate a list of all hyperlinks' sources and destinations

  • November 8, 2021
  • 2 replies
  • 1336 views

Hi,

I am working on a document with several "tabs". Each tab has a hyperlink that would lead to a page within the same document. Is there a way to generate a list of all hyperlinks and their destination pages?

I found some posts with similar request, and it seems like there is the solution. But I noticed that the script is for external URL. My document has no external URL (http links), just internal hyperlinks within the same document. 

Thanks.

This topic has been closed for replies.
Correct answer Mike Bro

Hello @SophiaPP,

Re: Is there a way to generate a list of all hyperlinks and their destination pages?

The below script is a play off of @Manan Joshi script from the link below.

https://community.adobe.com/t5/indesign-discussions/script-to-extract-hyperlinks-from-indesign-file-with-page-number/m-p/10919365

 

Give this script a try as I think this what you're after..

var list = [];  
for (a=0; a<app.activeDocument.hyperlinks.length; a++){  
	
    try{  
		d = app.activeDocument.hyperlinks[a].destination.destinationPage; 
        
			var b = app.activeDocument.hyperlinks[a].source;
            var x = app.activeDocument.hyperlinks[a].name;
			var page
			if(b.constructor.name == "HyperlinkPageItemSource")
			page = b.sourcePageItem.parentPage.name
			else if(b.constructor.name == "HyperlinkTextSource")
			page = b.sourceText.parentTextFrames[0].parentPage.name
			
			list.push ("Hyperlink: " + x + " " + "Destination Page:" + d.name);

        } catch(_) {}  
	}
// show the list  
alert ('All links:\r'+list.join('\r'));  
// save the list as a file  
listFile = new File(Folder.myDocuments+"/all_links.txt");  
if (listFile.open("w"))  
{  
  listFile.write(list.join('\n'));  
  listFile.close();  
  listFile.execute();  
} 

Regards,

Mike

 

 

2 replies

Mike BroCorrect answer
Legend
November 11, 2021

Hello @SophiaPP,

Re: Is there a way to generate a list of all hyperlinks and their destination pages?

The below script is a play off of @Manan Joshi script from the link below.

https://community.adobe.com/t5/indesign-discussions/script-to-extract-hyperlinks-from-indesign-file-with-page-number/m-p/10919365

 

Give this script a try as I think this what you're after..

var list = [];  
for (a=0; a<app.activeDocument.hyperlinks.length; a++){  
	
    try{  
		d = app.activeDocument.hyperlinks[a].destination.destinationPage; 
        
			var b = app.activeDocument.hyperlinks[a].source;
            var x = app.activeDocument.hyperlinks[a].name;
			var page
			if(b.constructor.name == "HyperlinkPageItemSource")
			page = b.sourcePageItem.parentPage.name
			else if(b.constructor.name == "HyperlinkTextSource")
			page = b.sourceText.parentTextFrames[0].parentPage.name
			
			list.push ("Hyperlink: " + x + " " + "Destination Page:" + d.name);

        } catch(_) {}  
	}
// show the list  
alert ('All links:\r'+list.join('\r'));  
// save the list as a file  
listFile = new File(Folder.myDocuments+"/all_links.txt");  
if (listFile.open("w"))  
{  
  listFile.write(list.join('\n'));  
  listFile.close();  
  listFile.execute();  
} 

Regards,

Mike

 

 

SophiaPPAuthor
Known Participant
November 12, 2021

Thank you both @Mike Bro and @Manan Joshi for your help!

I briefly tested the script and it seemed to work just as I need. Thanks a lot!

Community Expert
November 16, 2021

Ahh, I was late to respond but it seems @Mike Bro has already provided the solution. Good to know it worked in the end. @Mike Bro do we need the if else in the code now? I see that you are not using the page variable anymore.

-Manan

-Manan
Community Expert
November 9, 2021

Hi @SophiaPP,

I did not understand your comment regarding document with tabs. However, if you have a document with hyperlinks in it to different page destinations then the following code witll give you a prompt with the hyperlink source text and the page it points to. Give it a try and let me know if it works, If it does not work please share a sample document for me to understand the problem statement correctly

var list = [];  
for (var a=0; a<app.activeDocument.hyperlinks.length; a++)  
{ 
	try
	{  
		var hl = app.activeDocument.hyperlinks[a]
		if (hl.destination.constructor.name == "HyperlinkPageDestination")  
		{
			var src=hl.source
			var hlSrcText
			if(src.constructor.name == "HyperlinkPageItemSource" && src.sourcePageItem.constructor.name == "TextFrame")
				hlSrcText = src.sourcePageItem.contents
			else if(src.constructor.name == "HyperlinkTextSource")
				hlSrcText = src.sourceText.contents
			list.push (hlSrcText + " Page :-  " + hl.destination.destinationPage.name); 
		}
	} catch(_) {}  
}  
// show the list  
alert ('All links:\r'+list.join('\r')); 

-Manan

-Manan
SophiaPPAuthor
Known Participant
November 10, 2021

Hi @Manan Joshi ,

Thank you very much for taking your time to help me. I tried the code, and it works for those frames/objects with texts in them. For example, below object with "Overview" text:

 

generated a list as below :

However, in my design, there are many objects/frames without any text on it. So the list generates only " Page: - desitnation page" for those emty objects/frames as below:

 

Is it possible to list the objects/frames' names (ex: "04 Future Log 02" as below screenshot) instead of the text?

 
 

 

So that the result is something like" 03 Overview 02 Page: - 03Ovevw-02".

I hope I made sense. Thank you!