Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script to automatically assign existing text hyperlinks to the text frame they reside within instead

Explorer ,
Feb 24, 2023 Feb 24, 2023

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

TOPICS
EPUB , Publish online , Scripting
3.6K
Translate
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

Valorous Hero , Feb 24, 2023 Feb 24, 2023

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
...
Translate
Community Expert ,
Feb 24, 2023 Feb 24, 2023

If you give us the error you are receiving - then maybe someone will be able to help you... we are not mind readers... 

 

Translate
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
Explorer ,
Feb 24, 2023 Feb 24, 2023

Sorry, here it is:

Translate
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
Valorous Hero ,
Feb 24, 2023 Feb 24, 2023

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

2023-02-24_16-48-23.pngexpand image

After

2023-02-24_16-49-51.pngexpand image

Translate
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
Explorer ,
Feb 24, 2023 Feb 24, 2023

Thanks for that Kasyan.

Got this error now.

Translate
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
Valorous Hero ,
Feb 24, 2023 Feb 24, 2023

This is not a start up script! Move it from the "Startup Scripts" to the "Scripts Panel" folder.

Translate
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
Explorer ,
Feb 24, 2023 Feb 24, 2023

Sorry about that, I'm a scripting novice, had no idea it made a difference.

I've moved into the "Scripts Panel" and get this error now.

Translate
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 ,
Feb 24, 2023 Feb 24, 2023

Translate
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 ,
Feb 24, 2023 Feb 24, 2023

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. 

Translate
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
Valorous Hero ,
Feb 24, 2023 Feb 24, 2023

I tested it against a very simple file shown on the screenshot. Did you try m1b's reccomendation?

Translate
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
Explorer ,
Feb 27, 2023 Feb 27, 2023

Hi Guys,
I put in m1b's extra line and it brought up the attached error.
The good news is though, it actually worked, all the text hyperlinks are now on their frames instead! Should I be worried about this error?

Cheers

Translate
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
Valorous Hero ,
Feb 27, 2023 Feb 27, 2023

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;
}

 

Translate
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
Explorer ,
Feb 27, 2023 Feb 27, 2023

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!

Translate
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 ,
Feb 27, 2023 Feb 27, 2023

Sorry, this is what I should have written for the check:

if (
    !hyperlink.source.hasOwnProperty('sourceText')
    || hyperlink.source.sourceText == undefined
)
    continue;
Translate
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 ,
Feb 27, 2023 Feb 27, 2023

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

Translate
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
Valorous Hero ,
Feb 27, 2023 Feb 27, 2023

Hi Mark,

I updated the code.

Translate
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
Valorous Hero ,
Feb 28, 2023 Feb 28, 2023

I reworked the script a little and posted it here on my site.

  1. Now it marks — via insertLabel() — ‘processed’ text frames so if a frame has 2+ hyperlinks it won’t throw an error.
  2. Enclosed adding a new source into a try-catch block to avoid “The object you have chosen is already in use by another hyperlink.” errors.
Translate
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 ,
Feb 28, 2023 Feb 28, 2023

Thanks Kasyan, that's a great addition.

Translate
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 Beginner ,
Oct 03, 2023 Oct 03, 2023

Hi @Kasyan Servetsky 

 

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!

Translate
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 ,
Oct 04, 2023 Oct 04, 2023
LATEST

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? 

 

Translate
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