Create hyperlinks in Indesign with Applescript

I’m trying to create a applescript that creates hyperlinks on linked images in InDesign.

This is what i have come up with so far.

set activeLayer to “Layer 1”

tell application “Adobe InDesign CC 2017”
activate
tell active document
set grphicBoxs to all graphics of layer activeLayer
set myLinks to {}
repeat with i in grphicBoxs
set iLink to item link of i
set end of myLinks to iLink
end repeat

repeat with theLinkRef in myLinks
set theLinkName to ((name of theLinkRef) as string)
display dialog "theLinkName: " & theLinkName

set myHyperLink to “www.myURL”
set myHyperLinkName to “myURL name”

try
set hyperLinkPreSet to make hyperlink URL destination with properties {name:myHyperLinkName, destination URL:myHyperLink}
on error
set hyperLinkPreSet to hyperlink URL destination myHyperLinkName
end try

–try
set hyperLinkObject to make hyperlink page item source with properties {name:myHyperLinkName, source page item:rectangle theLinkName, hidden:false}
–on error
–set TheHS to hyperlink page item source myHyperLinkName
–end try

display dialog “hyperLinkObject:” & hyperLinkObject

make new hyperlink with properties {destination:myHyperLink, source:hyperLinkObject, visible:false}

end repeat

end tell
end tell

When activated this script creates an selectable hyperlink (hyperLinkPreSet) but it is not applied to the object.
The Error message i get says

Invalid value for parameter ‘source’ of method ‘make’. Expected page item, but received nothing.

Is there anyone here who has successfully created hyperlinks on linked objects who can help me?

Hi. Your source is malformed, and your destination formatting reuses names, which is impermissible.

set counter to 1
tell application "Adobe InDesign CS3"'s document 1 to repeat with isLinkedGraphic in (get all graphics's item link's name)
	make hyperlink with properties {name:isLinkedGraphic's contents & counter, source:make hyperlink page item source with properties {source page item:rectangle counter}, destination:make hyperlink URL destination with properties {destination URL:"http://macscripter.net"}}
	set counter to counter + 1
end repeat

Edited to prevent name collisions if the same image is used more than once.

Thanks Marc, i had not thought about the name formatting.

This is implementation of your suggested fix and it works just fine.


set activeLayer to "Layer 1"
set counter to 1

tell application "Adobe InDesign CC 2017"
	activate
	tell active document
		set grphicBoxs to all graphics of layer activeLayer
		set myLinks to {}
		repeat with i in grphicBoxs
			set iLink to item link of i
			set end of myLinks to iLink
		end repeat
		
		repeat with theLinkRef in myLinks
			set theLinkName to ((name of theLinkRef) as string)
			set theLinkNameNoext to text 1 thru ((offset of "." in theLinkName) - 1) of theLinkName
			
			set myHyperLink to "http://macscripter.net"
			set myHyperLinkName to theLinkNameNoext & " " & counter
			
			make hyperlink with properties {name:myHyperLinkName, source:make hyperlink page item source with properties {source page item:rectangle counter}, destination:make hyperlink URL destination with properties {destination URL:myHyperLink}}
			
			set counter to counter + 1
		end repeat
		
	end tell
end tell