Script to add hyperlink in InDesign to text

Hi all,

I’m pulling my hair out because I cannot figure out what I feel like should be a simple script. Some of it may be because I don’t fully understand the InDesign scripting library very well.

I’ve created a script that takes data from fields in an Excel document and flows it into a text box in InDesign. That part wasn’t the issue. What I want to do, though, is take a URL (for example, a company’s LinkedIn profile) and use that as the hyperlink for text in the InDesign textbox that reads something like “Visit us on LinkedIn”

For the moment, I’m just trying to get it to add a URL to a text selection in InDesign, but it seems to do absolutely nothing. The text doesn’t get hyperlinked, the destination itself doesn’t appear in the hyperlinks panel, etc. I’m a bit confused because it appears to me that InDesign wants you to add hyperlinks to a document in a sort of index and then apply those to text/objects but I just can’t seem to get those two to marry up. If anyone could help I would be greatly appreciative.

Here’s the code I thought was working, but it’s not… and other code samples I’ve found online don’t seem to do anything either

tell application "Adobe InDesign 2024"
	tell document -1
		set i to insertion point 1 of item 1 of selection
		make hyperlink URL destination with properties {destination text:i, URL:"https://macscripter.net"}
	end tell
end tell

I will apologize now and readily admit I am not an expert nor a programmer. Just a graphic designer with a bit of scripting knowledge. I’ve previously used AppleScript to automate generating yearbook-style portrait blocks from files in a directory, for example… and in my mind this should be working, but there’s something about the way hyperlinks work in InDesign that I’m just not grasping.

please be gentle.

Ok, so I found a example here and managed to make it work (?!) but if anyone can suggest improvement I would be very happy.

tell application "Adobe InDesign 2024"
	activate
	tell active document
		
		
		set myHyperLink to "http://macscripter.net"
		set myHyperLinkName to "HL1"
		
		make hyperlink with properties {name:myHyperLinkName, source:make hyperlink text source with properties {source text:selection}, destination:make hyperlink URL destination with properties {destination URL:myHyperLink}}
		
	end tell
end tell