Tuesday, June 27, 2017

#1 2016-01-04 01:36:42 pm

kerflooey
Member
Registered: 2011-07-07
Posts: 121

Adding a hyperlink to a text string in existing PDF?

I have a PDF with a unique text string in it. I would like to scriptomatically make this arbitrary but unique text string into a hyperlink, and then resave the PDF  with this string's hyperlink active and clickable. The text string's format does not necessarily need to change to show that it's a hyperlink.

Does anyone know of an Applescriptable app which can do this *other than* an Adobe product? Thanks for any help!


Filed under: PDF, hyperlink, non-adobe

Offline

 

#2 2016-01-04 07:25:33 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 5023

Re: Adding a hyperlink to a text string in existing PDF?

Assuming you're running 10.9 or later, you could always do it without any app:

Applescript:

use scripting additions
use framework "Foundation"
use framework "Quartz"

on makeLinksInPDF:posixPath forString:searchString linkURL:linkURLString
   -- make URL from path
   set theURL to current application's class "NSURL"'s fileURLWithPath:posixPath
   -- make PDF
   set thePDF to current application's PDFDocument's alloc()'s initWithURL:theURL
   -- get list of matches as PDFSelections
   set theSels to (thePDF's findString:searchString withOptions:0) -- find matches as PDFSelections
   repeat with aSel in theSels
       set thePage to (aSel's pages()'s objectAtIndex:0) -- get the page it's on
       set theBounds to (aSel's boundsForPage:thePage) -- get its bounds
       set theLink to (current application's PDFAnnotationLink's alloc()'s initWithBounds:theBounds) -- make link with those bounds
       set theAction to (current application's PDFActionURL's alloc()'s initWithURL:(current application's class "NSURL"'s URLWithString:linkURLString)) -- action to perform
       (theLink's setMouseUpAction:theAction) -- set link's action
       -- set link's appearance
       (theLink's setColor:(current application's NSColor's blueColor()))
       set linkBorder to current application's PDFBorder's alloc()'s init()
       (linkBorder's setLineWidth:1.0)
       (linkBorder's setStyle:0)
       (theLink's setBorder:(linkBorder))
       (theLink's setShouldDisplay:true)
       -- add it to the page
       (thePage's addAnnotation:theLink)
   end repeat
   -- save the modified PDF
   set oldName to theURL's lastPathComponent()'s stringByDeletingPathExtension()
   set newURL to (theURL's URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:(oldName's stringByAppendingString:"-new"))'s URLByAppendingPathExtension:"pdf"
   thePDF's writeToURL:newURL
end makeLinksInPDF:forString:linkURL:

my makeLinksInPDF:"/Users/shane/Desktop/Some.pdf" forString:"whatever" linkURL:"http://www.macosxautomation.com/applescript/apps/"

Last edited by Shane Stanley (2016-01-05 05:24:14 am)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2016-01-05 03:56:38 am

CMYS
Member
From: Belgium
Registered: 2007-07-02
Posts: 79

Re: Adding a hyperlink to a text string in existing PDF?

Hi Shane!
I've tried your code even if I still don't get anything at that "new" (to me) scripting language.
I've put a PDF link of mine in lieu of yours, put a string of mine as well and also put another link.

The script stops at:
set theAction to (current application's PDFActionURL's alloc()'s initWithURL:(current application's nsurl's URLWithString:linkURLString)) -- action to perform

The error returned is:
nsurl doesn't understand the message URLWithString_

URLWithString_("http://www.editionsjesuites.com")
the AppleEvent was not handled by any handler (errAEEventNotHandled:-1708)

I've probably missed sth, haven't I?

Offline

 

#4 2016-01-05 05:21:20 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 5023

Re: Adding a hyperlink to a text string in existing PDF?

I suspect the problem is that you have a scripting addition loaded that unfortunately defines the term NSURL. So change this:

Applescript:

       set theAction to (current application's PDFActionURL's alloc()'s initWithURL:(current application's NSURL's URLWithString:linkURLString)) -- action to perform

to:

Applescript:

       set theAction to (current application's PDFActionURL's alloc()'s initWithURL:(current application's class "NSURL"'s URLWithString:linkURLString)) -- action to perform

I'll edit the original.

Last edited by Shane Stanley (2016-01-05 05:23:46 am)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#5 2016-01-05 01:28:09 pm

kerflooey
Member
Registered: 2011-07-07
Posts: 121

Re: Adding a hyperlink to a text string in existing PDF?

This will be wonderful if I can get it to work, Shane! I'm getting an error though:

     Can’t get framework \"Foundation\" of «script». Access not allowed." number -1723 from framework "Foundation"

I'm on 10.9.5 and just downloaded (updated) XCode. Do you know what I'm doing wrong? Thanks!

Offline

 

#6 2016-01-05 06:07:38 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 5023

Re: Adding a hyperlink to a text string in existing PDF?

Under 10.9 you need to save it as a script librariy. There's a full tutorial here:

macscripter.net/viewtopic.php?id=41638

Basically, you save it as .scptd file, click the Bundle Contents button in Script Editor's toolbar and click the checkbox AppleScript/Objective-C library, save again, then move to ~/Library/Script Libraries (you have to create this folder). Then you call it like this:

Applescript:

use theLib: script "<name of script lib>"
use scripting additions

tell theLib to makeLinksInPDF:"/Users/shane/Desktop/Some.pdf" forString:"whatever" linkURL:"http://www.macosxautomation.com/applescript/apps/"


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#7 2016-01-05 08:06:57 pm

kerflooey
Member
Registered: 2011-07-07
Posts: 121

Re: Adding a hyperlink to a text string in existing PDF?

Unbelievable! It works perfectly -- thank you so much!!

Offline

 

#8 2016-01-05 10:51:13 pm

kerflooey
Member
Registered: 2011-07-07
Posts: 121

Re: Adding a hyperlink to a text string in existing PDF?

Now a new but related question: Is it possible to make the searchString an Applescript list of words and match them with a list of linkURLstrings? Honestly I'm not well versed in this new ASObj-C stuff yet to see how it could be done most easily. I tried simply using lists for searchString and linkURLstring in the existing code but it errors. Thanks for any insight or pointers.

Offline

 

#9 2016-01-06 01:17:51 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 5023

Re: Adding a hyperlink to a text string in existing PDF?

kerflooey wrote:

Now a new but related question: Is it possible to make the searchString an Applescript list of words and match them with a list of linkURLstrings? Honestly I'm not well versed in this new ASObj-C stuff yet to see how it could be done most easily. I tried simply using lists for searchString and linkURLstring in the existing code but it errors. Thanks for any insight or pointers.

It's a simple AppleScript exercise. You would need to pass the two lists, then loop through them doing a search for each one. Something like:

Applescript:

use scripting additions
use framework "Foundation"
use framework "Quartz"

on makeLinksInPDF:posixPath forStrings:listOfSearchStrings linkURLs:listOfLinkURLStrings
   -- make URL from path
   set theURL to current application's class "NSURL"'s fileURLWithPath:posixPath
   -- make PDF
   set thePDF to current application's PDFDocument's alloc()'s initWithURL:theURL
   repeat with i from 1 to count of listOfSearchStrings
       set searchString to item i of listOfSearchStrings
       set linkURLString to item i of listOfLinkURLStrings
       -- get list of matches as PDFSelections
       set theSels to (thePDF's findString:searchString withOptions:0) -- find matches as PDFSelections
       repeat with aSel in theSels
           set thePage to (aSel's pages()'s objectAtIndex:0) -- get the page it's on
           set theBounds to (aSel's boundsForPage:thePage) -- get its bounds
           set theLink to (current application's PDFAnnotationLink's alloc()'s initWithBounds:theBounds) -- make link with those bounds
           set theAction to (current application's PDFActionURL's alloc()'s initWithURL:(current application's class "NSURL"'s URLWithString:linkURLString)) -- action to perform
           (theLink's setMouseUpAction:theAction) -- set link's action
           -- set link's appearance
           (theLink's setColor:(current application's NSColor's blueColor()))
           set linkBorder to current application's PDFBorder's alloc()'s init()
           (linkBorder's setLineWidth:1.0)
           (linkBorder's setStyle:0)
           (theLink's setBorder:(linkBorder))
           (theLink's setShouldDisplay:true)
           -- add it to the page
           (thePage's addAnnotation:theLink)
       end repeat
   end repeat
   -- save the modified PDF
   set oldName to theURL's lastPathComponent()'s stringByDeletingPathExtension()
   set newURL to (theURL's URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:(oldName's stringByAppendingString:"-new"))'s URLByAppendingPathExtension:"pdf"
   thePDF's writeToURL:newURL
end makeLinksInPDF:forStrings:linkURLs:

Edited for fix in next message

Last edited by Shane Stanley (2016-01-06 05:29:08 pm)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#10 2016-01-06 11:46:48 am

kerflooey
Member
Registered: 2011-07-07
Posts: 121

Re: Adding a hyperlink to a text string in existing PDF?

It works, thanks Shane, FANTASTIC!

Notes:

+ I had to change this line:

   set theURL to class "NSURL"'s fileURLWithPath:posixPath

to this:

         set theURL to current application's class "NSURL"'s fileURLWithPath:posixPath


+ Also, spaces in fileURLWithPath:posixPath don't seem to be a problem.

Offline

 

#11 2016-01-06 05:30:29 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 5023

Re: Adding a hyperlink to a text string in existing PDF?

kerflooey wrote:

+ I had to change this line:

set theURL to class "NSURL"'s fileURLWithPath:posixPath

Thanks; I've edited it above.

+ Also, spaces in fileURLWithPath:posixPath don't seem to be a problem.

Right. Quoting of paths is needed for shell scripting, but not elsewhere.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#12 2016-01-07 03:53:39 am

CMYS
Member
From: Belgium
Registered: 2007-07-02
Posts: 79

Re: Adding a hyperlink to a text string in existing PDF?

I must say this is pure magic to me!
I'm clearly not at ease with this language dealing with foreign syntax, and it's frustrating to me!
How to know what framework to use, what dictionary it uses, when to use pure AS language and when to use ASOC?
What applications are scriptable that way?
It seems to be much more efficient but yet requires new learnings. hmm

Offline

 

#13 2016-01-07 04:44:48 am

TecNik
Member
From: Applescript-Leeds-England-UK
Registered: 2003-04-02
Posts: 558
Website

Re: Adding a hyperlink to a text string in existing PDF?

I tried the same exercise, a year or two ago now, but using Acrobat Javascript instead.
It was nice to get this example working.
Thanks Shane! smile

It's prompted me to dig a little deeper regarding dictionaries etc.

Last edited by TecNik (2016-01-07 04:58:51 am)

Offline

 

#14 2016-01-07 04:52:17 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 5023

Re: Adding a hyperlink to a text string in existing PDF?

CMYS wrote:

I'm clearly not at ease with this language dealing with foreign syntax, and it's frustrating to me!

Well the syntax is still that of AppleScript. But it's using it to call Objective-C.

How to know what framework to use, what dictionary it uses, when to use pure AS language and when to use ASOC?

All good questions, with no simple answers, except maybe the last. There's no need to use ASObjC unless either there's no other way, or it's more efficient.

What applications are scriptable that way?

None. In many cases you use it to bypass applications.

It seems to be much more efficient but yet requires new learnings.

Yes, but that's not necessarily a bad thing.

Some things, like this example, take a fair bit of effort or knowledge. But in a lot of cases it's reasonably straight-forward, once you understand the use of classes and methods. You write it with the documentation open, just as the best way to write scripts is with the relevant dictionaries open.

Excuse the shameless plug, but I reckon my book gives a pretty good introduction.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#15 2016-01-07 05:16:21 am

CMYS
Member
From: Belgium
Registered: 2007-07-02
Posts: 79

Re: Adding a hyperlink to a text string in existing PDF?

Shane Stanley wrote:

Excuse the shameless plug, but I reckon my book gives a pretty good introduction.

I definitely trust you!
The point is that I write scripts to simplify my workflow or enhance some features in some applications. I'm basicaly a designer. Scripting is a plus to me and the question is like always: is it worth learning this to do that?
In this particular case of modifying a PDF with no particular application open, I'm dreaming! Even if I know -and make use of it- AS already allows us modifying text files or other stuff the same applicationless way.

Offline

 

#16 2016-01-07 06:01:28 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 5023

Re: Adding a hyperlink to a text string in existing PDF?

CMYS wrote:

Scripting is a plus to me and the question is like always: is it worth learning this to do that?

I suspect that's a question most of us face (and not just about scripting). But picking up snippets here and there -- which is how a lot of us got into scripting -- is not too time-consuming.

AS already allows us modifying text files or other stuff the same applicationless way.

Yes, but with limitations. ASObjC opens up the use of regular expressions, for example, and the ability to manipulate styled text, or process XML files. If you don't need them, fine -- but it's good to know that solutions are available.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#17 2016-01-07 07:08:06 am

CMYS
Member
From: Belgium
Registered: 2007-07-02
Posts: 79

Re: Adding a hyperlink to a text string in existing PDF?

Shane Stanley wrote:

Yes, but with limitations. ASObjC opens up the use of regular expressions, for example, and the ability to manipulate styled text, or process XML files. If you don't need them, fine -- but it's good to know that solutions are available.

I'd surely need all of that sooner or later actually. There is also the question of "free" time which is getting shorter and shorter at my end. I'll keep that in mind anyway and certainly be happy to dig into it. Thanks for all!

Offline

 

#18 2017-06-07 04:11:57 pm

kerflooey
Member
Registered: 2011-07-07
Posts: 121

Re: Adding a hyperlink to a text string in existing PDF?

Hi Shane:

Your script is still working great. Another related question if you don't mind: how could the target URLs be changed so that the link opens another PDF or JPG file on a local disk? Is it even possible? I tried various versions of "file:///" to no avail.

Also wondering if a link could jump to a page in the same PDF (so that if a PDF's Table of Contents were not actually links to its chapters it could be "retro-fitted" to jump to them)?

Thanks for any insight or advice on this!

Offline

 

#19 2017-06-07 04:59:20 pm

t.spoon
Member
From: BFE, Massachusetts
Registered: 2013-01-12
Posts: 185

Re: Adding a hyperlink to a text string in existing PDF?

This is certainly possible, as Acrobat Pro allows you to insert hyperlinks to open local files into PDF's.

You add these links through a GUI, so I'm not 100% sure when I view this sort of link that it's showing me the raw syntax you would use to insert a link like this.

But in case it is, it just shows the HFS path proceeded by "File:"

File:Macintosh HD:Users:Username:Downloads:etc.pdf

I suspect the "file:///" syntax is for Windows.

- Tom.


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#20 2017-06-07 06:22:30 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 5023

Re: Adding a hyperlink to a text string in existing PDF?

kerflooey wrote:

how could the target URLs be changed so that the link opens another PDF or JPG file on a local disk?

You need to create a file NSURL. So this line:

Applescript:

set theAction to (current application's PDFActionURL's alloc()'s initWithURL:(current application's class "NSURL"'s URLWithString:linkURLString)) -- action to perform

would become:

Applescript:

set theAction to (current application's PDFActionURL's alloc()'s initWithURL:(current application's class "NSURL"'s fileURLWithPath:linkURLString)) -- action to perform

That assumes that linkURLString is a POSIX path.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#21 2017-06-08 02:20:54 pm

kerflooey
Member
Registered: 2011-07-07
Posts: 121

Re: Adding a hyperlink to a text string in existing PDF?

Hi Shane, that worked great, but only for PDF files (doesn't work for JPGs). I only have a wild guess that this line:

Applescript:

set theLink to (current application's PDFAnnotationLink's alloc()'s initWithBounds:theBounds)

is maybe specifying a built-in function that restricts it to PDF files only? Or is it something else? All help appreciated as always!

Thanks, k

Offline

 

#22 2017-06-08 06:52:48 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 5023

Re: Adding a hyperlink to a text string in existing PDF?

Sorry, I don't know why it's not woking for jpegs.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#23 2017-06-12 11:14:01 am

kerflooey
Member
Registered: 2011-07-07
Posts: 121

Re: Adding a hyperlink to a text string in existing PDF?

OK, thanks. I'll do some more testing with other files and see if I can figure anything out!

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.078 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)