Sunday, April 21, 2019

#1 2019-02-19 09:50:04 am

FredrikM
Member
From:: Förslöv, Sweden
Registered: 2019-02-19
Posts: 21

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?

Offline

 

#2 2019-02-19 05:56:05 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 864

Re: Create hyperlinks in Indesign with Applescript

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

Applescript:

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.

Last edited by Marc Anthony (2019-02-19 06:40:43 pm)

Offline

 

#3 2019-02-20 06:58:50 am

FredrikM
Member
From:: Förslöv, Sweden
Registered: 2019-02-19
Posts: 21

Re: Create hyperlinks in Indesign with Applescript

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

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

Applescript:


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



Filed under: Indesign, hyperlink

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)