I think many of us work every day with a lots of PDFs. Thus, I guess it may be of interest to almost everyone. I’ll number my suggestions for further reference:
ToC editing. I like the new, and long awaited feature of ToC editing. Thanks to devs for that. But currently the link is leading to the page. I think it is possible to make it lead to the selected text (heading or object name). Sometimes we have more than one heading per page.
Copy Selection Link. I don’t remember exactly, but in DT3 one could get it via AppleScript. Is it possible to return back this feature (wouldn’t like to use GUI scripting). I can’t find it in dictionary.
PDF scripting. Apart from Selection Link, I’d like other AppleScript commands to use with PDFs, like:
Add link to the selected text (this would help automating backlinks)
Edit Sorry, that was before breakfast. A bit more detail on it: Apple’s PDFKit (which is buggy!) provides a class PDFAnnotation that can add links to text and highlight text. You can use it to add nearly every kind of graphic that can be added with Preview.
So this part is easy. The difficult one would be to find the currently selected text in the PDF. For this, it appears that you have to use UIKit or something similar. Which you can’t easily do from an arbitrary script.
I wouldn’t hold my breath waiting for the functions you mention to arrive in DT’s scripting dictionary. I’m not even sure that they would be a good idea, as they require too many conditions to be met, which is not really a good thing ™ in scripting: the currently open record must be a PDF, and the user must have selected something in it.
Thank you @cgrunenberg now I recall where I saw it… )
One little question: why do we need &search= part right after &length=. Isn’t it ?page=...&start=...&length=... clause define the selection unambiguously? Or it’s just in case we need the text of the selection right in url?
You work with PDF, find interesting thought, select text
Go to OmniOutliner find the place where to insert it, and run the script
What it does:
In PDF it creates the annotation with yellow marker for the text selected, and make a link to this text, leading you to the corresponding topic in OmniOutliner
In OmniOutliner, in selected place, it inserts a citation with link to the selection in PDF, like “Cited text” [Name of the PDF, p.page number]. Text in square brackets is a link. You can place your thoughts under this topic.
So you can go back and forth between PDF and OmniOutliner. You can browse annotations of PDF, see links in Links inspector, and see all OmniOutliner docs, where you have your thoughts about this PDF, in Mentions inspector. Nice.
-- Script to build a backlinks between PDF and OmniOutliner
-- Select text in PDF, go to OmniOutliner, select the place where you want the citation
-- Created by Silverstone on 14.07.2025
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "PDFKit"
use scripting additions
-- Data for PDF and selection
tell application id "DNtp"
set theTW to the front think window
set theRecord to content record of theTW
set PDFlink to reference URL of theTW
set PDFname to name of theRecord
set pdfPath to POSIX path of (get path of theRecord)
end tell
-- Dismantling the PDF link
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "="}
set theList to text items 1 thru -1 of PDFlink
set {PageIndex, startChar, lengthChar, SelectedText} to {(text 1 thru ((offset of "&" in (item 2 of theList)) - 1) of (item 2 of theList)) as integer, (text 1 thru ((offset of "&" in (item 3 of theList)) - 1) of (item 3 of theList)) as integer, (text 1 thru ((offset of "&" in (item 4 of theList)) - 1) of (item 4 of theList)) as integer, ((current application's NSString's stringWithString:(item 5 of theList))'s stringByRemovingPercentEncoding()) as string}
set PDFlink to text items 1 thru -2 of PDFlink as string
set AppleScript's text item delimiters to "&"
set PDFlink to text items 1 thru -2 of PDFlink as string
set AppleScript's text item delimiters to TID as string
-- Constructing a topic for OmniOutliner
set preText to "«" & SelectedText & "»" -- Citation text
set linkText to " [" & PDFname & ", стр." & ((PageIndex + 1) as string) & "]" -- Link text
set linkURL to PDFlink -- Link URL
-- Formatting data for OmniOutliner
set fullText to preText & linkText
set linkStart to (length of preText) + 2
set linkLength to (length of linkText)
tell application "OmniOutliner"
tell selected row of front document
tell rich text of cell 2
set its text to fullText
-- Formatting RTF
set value of attribute "font-fill" of style of characters 1 thru ((length of preText) as integer) to {25225, 25225, 25225, 65536} -- Color of citation
set value of attribute "font-italic" of style of characters 1 thru ((length of preText) as integer) to true -- Italic font for citation
set value of attribute "link" of style of characters linkStart thru (linkStart + linkLength - 2) to linkURL -- Making a part of topic to be a link
end tell
-- Getting a link to this topic
set urlString to "omnioutliner:///open?row=" & its id
end tell
end tell
-- Loading PDF
set pdfURL to current application's NSURL's fileURLWithPath:pdfPath
set pdfDoc to current application's PDFDocument's alloc()'s initWithURL:pdfURL
-- Setting the selection
set thePage to pdfDoc's pageAtIndex:PageIndex
set theRange to current application's NSMakeRange(startChar, lengthChar)
set lineSelections to (thePage's selectionForRange:theRange)'s selectionsByLine()
-- Working with selection lines
repeat with subSel in (lineSelections as list)
-- Yellow marker
set highlightAnn to (current application's PDFAnnotation's alloc()'s initWithBounds:(subSel's boundsForPage:thePage) forType:"Highlight" withProperties:(missing value))
(highlightAnn's setColor:(current application's NSColor's yellowColor()))
(thePage's addAnnotation:highlightAnn)
-- Link
set linkAnn to (current application's PDFAnnotationLink's alloc()'s initWithBounds:(subSel's boundsForPage:thePage))
(linkAnn's setURL:(current application's NSURL's URLWithString:urlString))
(thePage's addAnnotation:linkAnn)
end repeat
-- Saving a PDF (overwriting)
set saveSuccess to pdfDoc's writeToURL:pdfURL
You may adapt it to any other app, you like for mindbuilding, through pasteboard if you want RTF, or plane text, if markdown