PDFkit rolloverCaption

Hello,
I’ve continue to play with PDFkit and I’ve run into another issue I don’t understand.

I can use the following code to get different information from the annotations in a PDF.

myAnnot's fieldName() as string
myAnnot's |bounds|()

and I can use the following code to set those same properties

myAnnot's setFieldName:"test-name" 
myAnnot's setBounds:{{30, 75}, {120, 45}}

But I can’t seem to get/set the rolloverCaption, the following do not work and produce errors and I’m unclear how to do it correctly.

myAnnot's rolloverCaption()
myAnnot's setRolloverCaption:"My caption"

Any help would be greatly appreciated!

@Fredrik71 Dang it! I sort of suspected that was my issue but I didn’t realize I needed to add the alloc()'s init() after it. I had tried…

theAnnotation's current application's PDFAppearanceCharacteristics's setRolloverCaption:"caption-text"

and different variations on that, but never thought to initialize it. Thanks for your help @Fredrik71, I 'll give this another try and hopefully I can get it to work now.

One thing i’m still not understanding is I see you’re creating a variable called “appearance” and applying the rollover caption to that, but where/how does that ever relate to the annotation that is being created? I’m not seeing any lines that tie the “theAnnotation” and “appearance” together.

I’m starting to think I have the wrong item. I tried the code you suggested @Fredrik71, and it works without error but it doesn’t seem to do what I was hoping for.

If you open a PDF in Acrobat that has a form field item, there is an option labeled “Tooltip”, by the name and description I thought the rolloverCaption was going to be the same thing. However, after running the code you supplied, I’m not seeing my value in that section of the newly created widget.

Any thoughts on what I need to do to add the “Tooltip” to the annotation?

I think I’m not understanding something here…

set appearance to current application's PDFAppearanceCharacteristics's alloc()'s init()
appearance's setRolloverCaption:"caption-text"

I don’t see where in this sample code that the rollover caption is being applied to theAnnotation. It doesn’t seem to me like we are relating the instance of PDFAppearanceCharacteristics’s to theAnnotation. If I’m not associating it to my annotation, am I not just creating a random appearance object?

Ah, ok. That makes more sense. I misunderstood that. I thought your code was showing how to get the ApperanceCharacteristics of the annotation.

So, I need to figure out what the similar code is to this…

set theArray to current application's NSArray's arrayWithArray:theString

for my situation with the annotations.

I have purchased Shane’s book on ASObjC, and am trying to read that and understand as I go.

@Shai1

If your PDF was made by any Adobe app or PDFKit, your annotation has no rolloverCaption property.
Try this:

use framework "Foundation"
use framework "PDFKit"
use scripting additions

set theURL to current application's NSURL's fileURLWithPath:"/Users/you/Desktop/aaa.pdf"
set thedoc to current application's PDFDocument's alloc()'s initWithURL:theURL
set theAnnotation to ((thedoc's pageAtIndex:0)'s annotations())'s objectAtIndex:0 -- change the indexes if needed

--return theAnnotation's annotationKeyValues() as record

theAnnotation's setValue:"Some rollover string" forAnnotationKey:"/TU"
theAnnotation's valueForAnnotationKey:"/TU"
thedoc's writeToFile:"/Users/you/Desktop/bbb.pdf"

If it does not work, uncomment the commented line and post the resulting record here.

@ionah. Thanks for the post. I had been working on this but couldn’t get a functional script. The setValue:forAnnotationKey: method is where I was stuck.

I tested your script on a sample PDF (see bottom of script for link) and it seems to work fine. I wish we had a sample PDF with a tooltip (as the OP wants) but I couldn’t find one. Anyways, great work :slight_smile:

use framework "Foundation"
use framework "PDFKit"
use scripting additions

-- set annotation
set theURL to current application's NSURL's fileURLWithPath:"/Users/you/Desktop/aaa.pdf"
set thedoc to current application's PDFDocument's alloc()'s initWithURL:theURL
set theAnnotation to ((thedoc's pageAtIndex:0)'s annotations())'s objectAtIndex:0 -- change the indexes if needed
-- return theAnnotation's annotationKeyValues() --> /TU:"First name"
theAnnotation's setValue:"Some rollover string" forAnnotationKey:"/TU"
theAnnotation's valueForAnnotationKey:"/TU"
thedoc's writeToFile:"/Users/you/Desktop/bbb.pdf"

-- check annotation
delay 1
set theURL to current application's NSURL's fileURLWithPath:"/Users/you/Desktop/bbb.pdf"
set thedoc to current application's PDFDocument's alloc()'s initWithURL:theURL
set theAnnotation to ((thedoc's pageAtIndex:0)'s annotations())'s objectAtIndex:0 -- change the indexes if needed
return theAnnotation's annotationKeyValues() --> /TU:"Some rollover string"


-- Test PDF
-- http://foersom.com/net/HowTo/data/OoPdfFormExample.pdf
1 Like

@ionah This did exactly what I was looking for.

(theAnnotation's setValue:"Some rollover string" forAnnotationKey:"/TU")

I really appreciate you showing me this example. I learn so much more with getting “hands on” and tinkering with code as I try and understand what’s happening.