Export URL hyperlinks

Viewing 7 reply threads
  • Author
    Posts
    • #117601

      I have a booklet that mainly will be viewed as a pdf. I also need to print it out, and want to produce a separate page of URLs that are in it. It’s designed with clickable URL links, of which the text is different from the URLs themselves. i.e. “Serving Fish in the Jailhouse Tonight” is in the text (which has a character style assigned to it), the embedded URL is https://www.lyrics.com/lyric/34545012, and I want both output to a text file that I can print out and insert into the booklet in case anyone wants to look them up.
      Are there any scripts that can do this, or does anyone know a workaround?
      Thanks!

    • #117608
      Kal Starkis
      Member

      Hi Kathlyn

      I don’t know if there’s an existing script, but here’s a little script I’ve thrown together to get you started. Even if you’ve never scripted before, have a read over the comments in my code and see if you can follow along with what it’s doing…

      // Get all our document hyperlinks
      var myDocument = app.documents[0]
      var myDocumentHyperlinks = myDocument.hyperlinks.everyItem().getElements()
      // Create an empty object to store all the URLs (along with linked text)
      var indexOfURLs = {}
      // Iterate over all the document hyperlinks and add each one to the object
      for (var i = 0; i < myDocumentHyperlinks.length; i++) {
          var hyperlink = myDocumentHyperlinks[i]
          // Get linked text (title) and URL
          var title = hyperlink.source.sourceText.contents
          var url = hyperlink.destination.destinationURL
          // Add title and URL to the list
          indexOfURLs[title] = url
      }
      // Print out all the URLs
      for (var key in indexOfURLs) {
        $.writeln(key + ": " + indexOfURLs[key]);
      }

      The last part prints out the URLs (with titles) to the console (which opens in Adobe’s ExtendScript Toolkit app). You could copy and paste them from there if you want. Or you could modify the script to do something else with them. That’s really up to you. :-)

    • #1237863
      Khaled Aly
      Member

      Hi this doesn’t work.

    • #1237873
      Kal Starkis
      Member

      Hi Khaled. It’s been a good while since I threw that script together, but looking back at it now, I can see that it only works if all the hyperlinks in the document have URL Destinations. If you have other types of destinations (like Text Destinations for example), the script will fail at the line that tries to get the destinationURL. So let’s fix it…

      I think the easiest way is to just catch the error when it happens. Since we’re only interested in finding actual URLs, anything that isn’t a URL can safely be ignored. So, here’s an updated version of the script that does just that…

      // Get all our document hyperlinks
      var myDocument = app.documents[0];
      var myDocumentHyperlinks = myDocument.hyperlinks.everyItem().getElements();
      // Create an empty object to store all the URLs (along with linked text)
      var indexOfURLs = {};
      // Iterate over all the document hyperlinks and add each one to the object
      for (var i = 0; i < myDocumentHyperlinks.length; i++) {
          var hyperlink = myDocumentHyperlinks[i];
          // Get linked text (title) and URL
          var title = hyperlink.source.sourceText.contents;
          var url;
          try {
              url = hyperlink.destination.destinationURL;
          } catch (err) {
              // The destination is not a URL. Ignore it.
              continue;
          }
          // Add title and URL to the list
          indexOfURLs[title] = url;
      }
      // Print out all the URLs
      for (var key in indexOfURLs) {
        $.writeln(key + ": " + indexOfURLs[key]);
      }

      Please let us know how you go. :-) (I find that it’s not uncommon to give up precious time on forums like this one to try and help others only to never hear back from people, as was the case with the OP on this thread. We’re all human and a little ‘thanks for your help’ goes a long way to keeping any community alive and helpful.)

    • #1237893
      Khaled Aly
      Member

      Hi

      First of all thank you very much for responding and taking the time to update the script! I really really really appreciate it. In fact if you have time I would love to hear your thoughts on a small project I have with InDesign and how I’m hoping to automate it.

      Unfortunately though I keep getting this error:

      Object does not support the property or method sourceText

      Any ideas what could be causing this?

      Thanks
      Khaled

    • #1237903
      Kal Starkis
      Member

      Ah… so the problem isn’t with the destination but the source. The fix should be much the same. Just as a hyperlink destination can be of various types in InDesign (a page, text or a URL), so a hyperlink source can be of different types too (a page item, text or cross reference). Page items don’t have a sourceText property, so if you have these in your document the script will no-doubt fail with the kind of error you’re seeing. (A consequence of banging out code without thorough testing!)

      It sounds like you’re about to roll up your sleeves and get stuck into a scripting project of your own, so why don’t you have a go at fixing my script? Just apply the same logic I used before to catch the error, and you should be good to go. Let me know if you can’t get it to work.

      I don’t have a lot of spare time ATM, but I’m sure I can find time to share my thoughts on your project. You can always shoot me a message via the feedback form on my website (inkwire.app). If you have more technical questions, probably best just to ask questions here, at the Adobe forums or on Stack Overflow.

    • #1237923
      Khaled Aly
      Member

      Ok I’ll give it a go and get back to you if I can’t figure it out! Thanks

    • #14337613
      Federico Faccio
      Participant

      Hello Khaled Aly and Kal Starkis,

      First of all, thank you very much for your exchange around this topic, it’s been super useful for me and what I am trying to do. From what I understand, the second version of Kal Starkis’ script is exactly what I would need, however I cannot rely on ExtendScript Toolkit so I would need the result of this script to be exported into some format like .txt for example.

      The reason for this is that I work in a team where most of my colleagues barely use the Adobe suite, meaning they have access to it but aren’t quite proficient. Among other things, we create ebooks, so for specific tasks (like localizing them into other languages) we need to adapt the linked URLs for the (new) target audience. I am looking for a way to make this easy for them: instead of manually going page by page trying to find all hyperlinks or even via the hyperlink windows in InDesign (sometimes it’s a few hundred URLs), I would like to give them a quick solution to run a script like this on a file and get a list of the used URLs. This list could then be used to find the corresponding links for the localized versions (for example, if an ebook links to a help site like [https://helpx.adobe.com/story/help/add-edit-scripts-.html], we would then know that this link needs to be swapped with this [https://helpx.adobe.com/la/story/help/add-edit-scripts-.html] for the Spanish-language version.

      Now I’ve given it a few tries, but I am very unfamiliar with actual editing of Javascript code myself, so I am completely lost. I understand I should be adding these instructions about what to do with the result of the script in the bit that says “for (var key in indexOfURLs) { $.writeln(key + “: ” + indexOfURLs[key]);” but I really don’t know how.

      Could someone give me a hand with this? Also, is there a way to then automatically import the URLs back into the InDesign file? For example, if all source URLs are in Column A of a spreadsheet and the Spanish version links are in column B and German in C, can such a spreadsheet be used to automatically swap all links in the InDesign file in order to avoid mistakes by manual work?

      Thank you very much for all your help already!

Viewing 7 reply threads
  • You must be logged in to reply to this topic.
>