Copy link to clipboard
Copied
Hi,
I've used the 'convert URL's to hyperlinks' command in InDeign to convert multiple URL's, all in their own text frames, to Hyperlinks. I'd rather the hyperlinks were assigned to the actual frames themselves, rather than the URL text within them. Is there a way to do this with a script?
I did find this script: https://community.adobe.com/t5/indesign-discussions/how-to-change-automatically-frame-with-hyperlink... which says it will do this but it doesn't work (i get a string error). Can anyone help?
Thanks
There's a mistake in the code. Here's a fixed version. I tried to make it more easy to understand.
if (app.documents.length > 0) main();
function main() {
var hyperlink, sourceText, textFrame, storedName, newSource, newHyperlink,
doc = app.activeDocument,
hyperlinks = doc.hyperlinks.everyItem().getElements();
for (var i = 0; i < hyperlinks.length; i++) {
hyperlink =hyperlinks[i];
if (!hyperlink.source.hasOwnProperty("sourceText") || hyperlink.source.sourceText == undefined) continu
...
Copy link to clipboard
Copied
If you give us the error you are receiving - then maybe someone will be able to help you... we are not mind readers...
Copy link to clipboard
Copied
Copy link to clipboard
Copied
There's a mistake in the code. Here's a fixed version. I tried to make it more easy to understand.
if (app.documents.length > 0) main();
function main() {
var hyperlink, sourceText, textFrame, storedName, newSource, newHyperlink,
doc = app.activeDocument,
hyperlinks = doc.hyperlinks.everyItem().getElements();
for (var i = 0; i < hyperlinks.length; i++) {
hyperlink =hyperlinks[i];
if (!hyperlink.source.hasOwnProperty("sourceText") || hyperlink.source.sourceText == undefined) continue;
sourceText = hyperlink.source.sourceText;
textFrame = sourceText.parentTextFrames[0];
storedName = hyperlink.name;
newSource = doc.hyperlinkPageItemSources.add(textFrame);
newHyperlink = doc.hyperlinks.add(newSource, hyperlink.destination);
hyperlink.remove();
// clear the "Hyperlink" char style
if (sourceText.appliedCharacterStyle.name == "Hyperlink") {
sourceText.appliedCharacterStyle = doc.characterStyles.item("[None]");
sourceText.clearOverrides();
}
newHyperlink.name = storedName;
}
}
Before
After
Copy link to clipboard
Copied
Copy link to clipboard
Copied
This is not a start up script! Move it from the "Startup Scripts" to the "Scripts Panel" folder.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
.
Copy link to clipboard
Copied
Hi @Barry23053734dbr6, If you insert this line:
if (hyperlink.source.sourceText == undefined) continue;
after:
hyperlink = hyperlinks[i];
It will ignore any hyperlinks that don't have sourceText.
Copy link to clipboard
Copied
I tested it against a very simple file shown on the screenshot. Did you try m1b's reccomendation?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
It's difficult for me to see what goes wrong without having a doc that causes the error.
I would try the following:
1) Change the line to:
if (typeof hyperlink.source.sourceText == "undefined") continue;
2) Enclose the line into a try-catch block so that if an error occurs, it skips the problematic hyperlink and goes on to the next one:
try {
if (hyperlink.source.sourceText == undefined) continue;
}
catch(err) {
continue;
}
Copy link to clipboard
Copied
Hi Kasyan,
The previous version does look like the answer. Don't know why it came up with the error on my first example but but there's been no error on the other versions I've done since and has worked a treat!
Thanks for all your help guys!
Copy link to clipboard
Copied
Sorry, this is what I should have written for the check:
if (
!hyperlink.source.hasOwnProperty('sourceText')
|| hyperlink.source.sourceText == undefined
)
continue;
Copy link to clipboard
Copied
It would be nice to have a clean answer for future readers of this thread. @Kasyan Servetsky, if you update your code in your initial answer I will mark it as correct. Hopefully I will be notified. - Mark
Copy link to clipboard
Copied
Hi Mark,
I updated the code.
Copy link to clipboard
Copied
I reworked the script a little and posted it here on my site.
Copy link to clipboard
Copied
Thanks Kasyan, that's a great addition.
Copy link to clipboard
Copied
So I downloaded your script and tried to run it on Indesign 2023, this script was working sometimes, but sometimes was not working, do you have any idea why? and there's no error message popup...please help. Thank you very much!
Copy link to clipboard
Copied
If I may pitch in - I think you should do re-processing of the already processed TextFrame - in case link in the text has been changed?
Unless you switch from using hidden Label - Insert/ExtractLabel - to the visible one so user can remove it manually and re-run script?