6 Replies Latest reply on Oct 25, 2011 1:37 PM by scriptnic01

    Converting HTML Links to Hyperlinks in InDesign

    scriptnic01

      Hi--I'm new to this forum, so forgive any newbie-ness...

       

      Working on a pretty intense automated page layout which creates a product cut sheet based from content that is on the web (using AppleScript). One feature I'm trying to build in is to convert typical HTML href tags to an actual hyperlink in InDesign.

       

      I'm using GREP to find all the html tags, which works very well.

       

      What I can't do (and it might be a limitation of GREP in ID) is store the 'change to' locations as variables in AppleScript. For example, I use:

       

      --find the href tag

      set find what of find grep preferences to "(?i)(<a href=\")(.+?)(\")(.*)(>)(.*)(</a>)"

      --change to the stuff in the href=" " and the text I want to keep

      set change to of change grep preferences to "$2 $6"

       

      What I NEED is the ability to make $2 and $6 variables.

       

       

      Any advice, tips, hints, tricks, and/or help?

       

      Thanks all!

        • 1. Re: Converting HTML Links to Hyperlinks in InDesign
          John Hawkinson Level 5

          Err, your question doesn't quite make sense -- perhaps if you supplied more of the script it would; it's especially unclear where you are running the grep.

           

          If you use InDesign's built-in findGrep, you must set both the findWhat and the changeTo prior to executing the application's findGrep method.

           

          It sounds like you want access to the grep's capture buffers ($1, $2) inside your script. That's not possible.

           

          But you can take the result of the grep search and perform matching against it in the language of your choice. In JavaScript, this is easy, because there is built-in regular expression support (just like GREP uses. That's the RE in GREP). In Applescript, you don't have direct support for REs. You can either use Applescript's more limited text operations, you can install a scripting addition with regexp support like Satimage, or you can try to shell out to a shell script to use some other tool.

           

          I'll let the Applescript folks tell you which is best, but I would rewrite it in JavaScript . [I suppose you could also call JavaScript from

          AppleScript. That might actually be easier than any of the other choices. ]

           

          But either way, please post a bit more of your script so we can help you without doing a lot of guessing.

          • 2. Re: Converting HTML Links to Hyperlinks in InDesign
            scriptnic01 Level 1

            Well, I'm afraid my starting attempts don't really tell the story. Here's the essence of what I want to do:

             

            convert: <a href="www.script.com/somelink" class="linkClass" target="_blank">Some Link<\a>

            into an actual hyperlink in InDesign, so when the document is exported to PDF, the text "Some Link" can be clicked on within the PDF reader, bringing you to the website "www.script.com/somelink."

             

            Thanks.

            • 3. Re: Converting HTML Links to Hyperlinks in InDesign
              scriptnic01 Level 1

              Been trying to post my solution, but forum is running slow...


              • 4. Re: Converting HTML Links to Hyperlinks in InDesign
                scriptnic01 Level 1

                I've solved this problem, and have been trying to post to the forum, but it

                is running very slow and not allowing me to post.

                • 5. Re: Converting HTML Links to Hyperlinks in InDesign
                  mwelford87

                  scriptnic01

                   

                  I am trying to solve the same issue.  Can you please post your solution or email it to me some how?  I would greatly appreciate it!

                   

                  Thank you!

                  • 6. Re: Converting HTML Links to Hyperlinks in InDesign
                    scriptnic01 Level 1

                    Sorry, it's been a while since I solved this, so its not too fresh. There may be pieces missing. Here's the exerpt of what I did to change real html marked-up text (which contains hyperlinks) into real, clickable InDesign hyperlinks. In a few words, I found the html hyperlinks in the text, and "marked it up" with my own "xml" type language (for later). Then I think I made an array of every link and location, then had InDesign go through and convert to hyperlinks.

                     

                     

                    ---CONVERT HREFS TO HYPERLINKS------------------------------------------------------

                        set find grep preferences to nothing

                        set change grep preferences to nothing

                        --FIND HYPERLINKS

                        set include footnotes of find change grep options to false

                        set include hidden layers of find change grep options to false

                        set include locked layers for find of find change grep options to false

                        set include locked stories for find of find change grep options to false

                        set include master pages of find change grep options to true

                        set find what of find grep preferences to "(?i)(<a href=\")(.+?)(\")(.*?)(>)(.*?)(</a>)"

                        --set find what of find grep preferences to "(?i)(?<=<a href=\").+?(?=\")"

                       

                        set change to of change grep preferences to "<url>http://www.mywebaddress.com$2</url> <linked text>$6</lt>"

                        set applied character style of change grep preferences to nothing

                        tell specSheet

                            change grep

                        end tell

                     

                    set find grep preferences to nothing

                        set change grep preferences to nothing

                        --FORMAT HYPERLINKS

                        set include footnotes of find change grep options to false

                        set include hidden layers of find change grep options to false

                        set include locked layers for find of find change grep options to false

                        set include locked stories for find of find change grep options to false

                        set include master pages of find change grep options to true

                        set find what of find grep preferences to "http://.+?(?=</url> <linked text>)"

                        set change to of change grep preferences to ""

                        set applied character style of change grep preferences to nothing

                     

                    tell specSheet

                            set myFoundItems to find grep

                           

                            set linkURL to get myFoundItems

                           

                        end tell

                       

                        set find what of find grep preferences to "(?<= <linked text>).+?(?=</lt>)"

                        tell specSheet

                            set myFoundTextLinks to find grep

                            set linkNames to get myFoundTextLinks

                        end tell

                       

                        set masterList to {}

                       

                        set numItems to count of linkURL

                        set n to 1

                        repeat numItems times

                            set end of masterList to {item n of linkURL, item n of linkNames}

                            set n to (n + 1)

                        end repeat

                       

                       

                        tell specSheet

                           

                            repeat with currentLink in masterList

                                set hyperLinklocation to item 2 of currentLink

                     

                    try

                                    set hyperLink1 to make hyperlink URL destination with properties {name:item 2 of currentLink as string, destination URL:item 1 of currentLink as string}

                                on error

                                    set hyperLink1 to hyperlink URL destination (item 2 of currentLink as string)

                                end try

                               

                                try

                                    set hyperLinkText to make hyperlink text source with properties {source text:hyperLinklocation, hidden:false, applied character style:character style "hyperlink"} --name:item 2 of currentLink as string,

                                on error

                                    set hyperLinkText to hyperlink text source (item 2 of currentLink as string)

                                end try

                               

                                try

                                    make new hyperlink with properties {destination:hyperLink1, source:hyperLinkText, hidden:false} --name:item 2 of currentLink as string}

                                on error

                                    make hyperlink with properties {destination:hyperLink1, source:hyperLinkText, hidden:false}

                                end try

                               

                            end repeat

                            --set linksDone to "true"

                           

                        end tell

                        set myFoundItems to {}

                        set myFoundTextLinks to {}

                    end tell

                     

                     

                    tell application "Adobe InDesign CS5"

                       

                       

                        --REMOVE MARKUP -URLS FROM COPY

                        set find grep preferences to nothing

                        set change grep preferences to nothing

                        --Set the find options.

                        set include footnotes of find change grep options to false

                        set include hidden layers of find change grep options to false

                        set include locked layers for find of find change grep options to false

                        set include locked stories for find of find change grep options to false

                        set include master pages of find change grep options to true

                        set find what of find grep preferences to "<url>.+?</url> "

                        set change to of change grep preferences to ""

                        tell specSheet

                            change grep

                        end tell

                        --Clear the find/change preferences after the search.

                        set find grep preferences to nothing

                        set change grep preferences to nothing

                       

                        --display dialog "URL text deleted."

                       

                        --LINKED TEXT TAGS REMOVAL AND CHARACTER FORMAT

                        set find grep preferences to nothing

                        set change grep preferences to nothing

                        --Set the find options.

                        set include footnotes of find change grep options to false

                        set include hidden layers of find change grep options to false

                        set include locked layers for find of find change grep options to false

                        set include locked stories for find of find change grep options to false

                        set include master pages of find change grep options to true

                        set find what of find grep preferences to "<linked text>|</lt>"

                        set change to of change grep preferences to ""

                        --set applied character style of change grep preferences to "hyperlink"

                        tell specSheet

                            change grep

                        end tell

                        --Clear the find/change preferences after the search.

                        set find grep preferences to nothing

                        set change grep preferences to nothing

                       

                        --display dialog "Linked text tags removed, style applied."

                     

                    end tell