Copy link to clipboard
Copied
Hi All,
I found this script to retrieve all the hyperlinks in a document and it works perfectly. But instead of pages, I'd like the word to which the hyperlink is applied.
var list = [];
for (a=0; a<app.activeDocument.hyperlinks.length; a++)
{
try
{
d = app.activeDocument.hyperlinks[a].destination.destinationURL;
if (d.match(/^http/))
{
var b = app.activeDocument.hyperlinks[a].source
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
$.writeln(page)
list.push ("Page Name " + page + " " + d);
}
} 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.writeln(list.join('\n'));
listFile.close();
listFile.execute();
}
Thanks
Laurent
Here you go:
list = [];
links = app.activeDocument.hyperlinks.everyItem().getElements();
for (a = 0; a < links.length; a++) {
try {
if (/^http/.test (links[a].destination.destinationURL)) {
list.push ("Link name: " + links[a].name);
list.push ("Link: " + links[a].destination.destinationURL + '\r\r');
}
} catch (_) {
}
}
// show the list
//alert ('All links:\r'+list.join('\r'));
// save the list as a file
listFile = File (Folder.myDocuments+"/all_links.txt");
if (list...
Copy link to clipboard
Copied
Hi @laurent tourniel7664515, you're almost there. Try this:
var list = [];
for (a = 0; a < app.activeDocument.hyperlinks.length; a++) {
try {
d = app.activeDocument.hyperlinks[a].destination.destinationURL;
if (d.match(/^http/)) {
var b = app.activeDocument.hyperlinks[a].source;
var text;
if (b.constructor.name == "HyperlinkPageItemSource")
text = b.sourcePageItem.parentPage.name;
else if (b.constructor.name == "HyperlinkTextSource")
text = b.sourceText.contents;
// $.writeln(text)
list.push("Hyperlink source text:\t" + text + "\t" + d);
}
} 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.writeln(list.join('\n'));
listFile.close();
listFile.execute();
}
You just need the "contents" of the "sourceText".
- Mark
Copy link to clipboard
Copied
Hello Laurent -- This script considers only URL hyperlinks. The words that these links are applied to are the names you see in the Hyperlinks panel. So you want a list of those words? In that case, use this version:
list = [];
links = app.activeDocument.hyperlinks.everyItem().getElements();
for (a = 0; a < links.length; a++) {
try {
if (/^http/.test (links[a].destination.destinationURL)) {
list.push ("Link name:" + links[a].name);
}
} catch (_) {
}
}
// show the list
alert ('All links:\r'+list.join('\r'));
// save the list as a file
listFile = File (Folder.myDocuments+"/all_links.txt");
if (listFile.open("w")) {
listFile.writeln(list.join('\n'));
listFile.close();
listFile.execute();
}
Peter
PS: Mark beat me to it, though in a different way. Let's see. . .
Copy link to clipboard
Copied
Thank you for your two answers, but neither of them is appropriate. Sorry.
Here is the result of the original script.
m1d, no data in the .txt file (Notepad++).
Peter, the hyperlinks are missing.
Copy link to clipboard
Copied
Instead of Page name ### (from the original script), you'd need Peter's Link name, with the added hyperlink
Merci
Copy link to clipboard
Copied
Here you go:
list = [];
links = app.activeDocument.hyperlinks.everyItem().getElements();
for (a = 0; a < links.length; a++) {
try {
if (/^http/.test (links[a].destination.destinationURL)) {
list.push ("Link name: " + links[a].name);
list.push ("Link: " + links[a].destination.destinationURL + '\r\r');
}
} catch (_) {
}
}
// show the list
//alert ('All links:\r'+list.join('\r'));
// save the list as a file
listFile = File (Folder.myDocuments+"/all_links.txt");
if (listFile.open("w")) {
listFile.writeln (list.join('\n'));
listFile.close();
listFile.execute();
}
Copy link to clipboard
Copied
With some GREP behind to optimize the file for use with another script (which creates links from a tab-separated list of names and hyperlinks), it's perfect. Thanks Peter
Copy link to clipboard
Copied
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more