Within a PDF I can highlight some text, right click on it. The “Add Link…” option then allows me to link that text to another record.
Can I do the same within AppleScript ?
Thanks
Frank.
Within a PDF I can highlight some text, right click on it. The “Add Link…” option then allows me to link that text to another record.
Can I do the same within AppleScript ?
Thanks
Frank.
For notes format I use Formatted Note
so I’m working with basic HTML code, easily scripted
>Within a PDF
Wow, the menu option actually added a link (as an annotation)
Sorry, I don’t know about scripting for a pdf
I think there is an ambiguity in this question, which perhaps other people will face as well: are you asking
If it’s #2, it would be useful to know what kind of document contains the AppleScript code you would like to link to. Is it, for example, a Markdown document containing a code block with AppleScript inside the code block?
No there is no AppleScript command to add a link to selected PDF text.
OK thanks.
Editing of PDF documents isn’t scriptable so far.
When you get a pdf from an open tab it returns an object of class “any”. Did you work out what structure this contains? I’d like to manipulate it with PDFkit before saving the record.
It’s binary data identical to the one of PDF files.
Is it base64 encoded or similar ?
As the following bombs out …
tell application id "DNtp"
set thisTab to open tab for URL "https://kernel.org"
repeat while loading of thisTab = true
delay 1
end repeat
set thePDFdata to PDF of thisTab
set thisPDF to (current application's PDFDocument's alloc()'s initWithData:thePDFdata)
end tell
It works if I generate a URL and point to a specific file on disk, but it doesn’t seem to appreciate the class “any”.
No, just raw binary data (internally NSData before passing it to AppleScript).
I hit a bit of a wall on this one and didn’t get back to it.
The problem might be the selected text. It seems to be associated with a PDFView
object (currentSelection
). But I doubt that you can create and or access a PDFView
with AppleScript/JXA-ObjC. If that were possible, it would be easy to get the currentSelection
and create a link PDFAnnotation
object.
OTOH, if you could access the PDFView
with a script, you could perhaps break a lot of things in DT.
There is an interesting script here …
macscripter
That does what I’m looking for, i.e. updating PDFs. But I’ve not managed to join up the dots yet. It works perfectly against a local PDF outside of DevonThink.
Eight years old. It may or may not work still. And it is not working with the currently selected string, but with a string that you provide to it. The code then searches for that string. Which is not at all the same as selecting something interactively.
My objective is to;
This way I’m updating the PDF with annotations before DevonThink even knows I’m going to create a record from it. So anything that DT normally does with a PDF after step 5, e.g. indexing can continue as normal. Better that, than identifying the PDF file in the DT database and manipulating it directly which might upset DT.
I’ve completed 95% of the script so far. Outstanding tasks are;
This is all in a script. With no user selection/interaction.
So the thread is not at all about adding a link annotation to the current selection. Wild goose chase…
“As a key”? What do you mean? And from what do you create anotherPDF
– out of thin air?
Are you sure? Isn’t
already creating a PDF? Or how do you “capture thePDF”? And what does “as a variable” mean?
Adding annotations to PDFs inside DT with a script doesn’t upset it. At all. I’ve tried that.
And: DT creates a PDF record. Otherwise, there would be no PDF data to return (regardless of its usability in a script).
No worries there. PDFKit might screw up, but DT doesn’t mind you adding annotations in a script.
Well:
That was the text of your original post.
I never intended to write my entire script in a forum. I needed pointers on what was possible with the DT API before I started to reinvent the wheel. I got the answer initial answer I needed.
But I continued my investigations and have received incredible support from various AppleScript, ObjC and this forum that has helped me get to where I am. However, I never expected to have to justify the logic of my full algorithm, I’m sorry if you don’t like it.
The regex issue is well in hand and I’m refining it on my daily data feeds.
But as @MrSkooby has pointed out, there is an issue I’ve overlooked as I haven’t addressed this issue yet. So if you have (or anyone else has) any suggestions on casting/coercing the raw PDF data NSData/any into a PDFDocument I would be very interested in your suggestion/ideas/advice, even if its just to confirm its not possible.
Thanks
I doubt that’s possible. And I’d use the path to the document instead. And I already explained that adding annotations to a PDF with a script doesn’t screw it up for DT. For more information:
I didn’t ask for justification but for explanation (what does “as a key” mean, what’s the content of “anotherPDF” etc.). If I don’t understand what you’re trying to do (or doing), how do you expect me to give advice?
In Objective-C, there’s a -[PDFDocument initWithData:]
method which creates a new PDFDocument instance from the given NSData. PDFDocument
also offers the methods -writeToFile
and -writeToURL
which write the PDFDocument instance to a file. These methods should be also accessible/usable via AppleScriptObjC.
As to “creating a PDF from DT’s data
property”:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "PDFKit"
use scripting additions
tell application id "DNtp"
set rec to get record with uuid "x-devonthink-item://E2D5F820-37B7-402C-A642-869BAD579F5E"
set pdfdata to data of rec
set nsData2 to my NSDataFromASData(pdfdata)
set pdfDoc to (current application's PDFDocument's alloc()'s initWithData:nsData2)
log pdfDoc's pageCount()
end tell
on NSDataFromASData(asData)
return (current application's NSArray's arrayWithObject:asData)'s firstObject()'s |data|()
end NSDataFromASData
That worked, at least for the single PDF document I tried it with.
I didn’t invent that, I just scrapped it from the net. Seems to indeed do what was asked for. If I understood what was asked for correctly.