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

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

Explorer ,
Feb 24, 2023 Feb 24, 2023

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

TOPICS
EPUB , Publish online , Scripting

Views

2.2K

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 , 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
...

Votes

Translate

Translate
Community Expert ,
Feb 24, 2023 Feb 24, 2023

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... 

 

▒► ID-Tasker / ID-Tasker Server - work smart not hard ◄▒

Votes

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

Copy link to clipboard

Copied

Sorry, here it is:

Votes

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

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

2023-02-24_16-48-23.png

After

2023-02-24_16-49-51.png

Votes

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

Copy link to clipboard

Copied

Thanks for that Kasyan.

Got this error now.

Votes

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

Copy link to clipboard

Copied

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

Votes

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

Copy link to clipboard

Copied

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.

Votes

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

Copy link to clipboard

Copied

▒► ID-Tasker / ID-Tasker Server - work smart not hard ◄▒

Votes

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

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. 

Votes

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

Copy link to clipboard

Copied

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

Votes

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

Copy link to clipboard

Copied

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

Votes

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

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

 

Votes

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

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!

Votes

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

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;

Votes

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

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

Votes

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

Copy link to clipboard

Copied

Hi Mark,

I updated the code.

Votes

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

Copy link to clipboard

Copied

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.

Votes

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

Copy link to clipboard

Copied

Thanks Kasyan, that's a great addition.

Votes

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

Copy link to clipboard

Copied

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!

Votes

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

Copy link to clipboard

Copied

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? 

 

▒► ID-Tasker / ID-Tasker Server - work smart not hard ◄▒

Votes

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