Here is the AppleScript that I have tested on macOS 14.4. It prompts for a single PDF document and prompts for a single string that in the PDF has a highlight annotation. It will remove that specific annotation.
(*
PDF_remove_HL.applescript
Prompts user for one PDF document and then a prompt for the single text
string that has a highlight annotation on it. That PDF highlight is then
removed and the PDF updates without that highlight.
Suggestion: Have a backup of the PDF before running the script.
Tested: macOS Sonoma 14.4
Author: VikingOSX, 2024-03-09, Apple Support Communities, No warranties of any kind.
*)
use framework "Foundation"
use framework "PDFKit"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions
property ca : current application
set pdfPath to POSIX path of (choose file of type "PDF" default location (path to desktop)) as text
repeat while true
set findText to text returned of (display dialog "Enter Text for Highlight removal:" default answer "") as text
if not (findText is "") = true then exit repeat
end repeat
set pdf to ca's PDFDocument's alloc()'s initWithURL:(ca's NSURL's fileURLWithPath:pdfPath)
-- PDF pages are zero-based
repeat with apage from 1 to pdf's pageCount()
set pdfPage to (pdf's pageAtIndex:(apage - 1))
repeat with anno in pdfPage's annotations()
if ((anno's type()) as text) contains "Highlight" then
set arect to anno's |bounds|()
set atext to ((pdfPage's selectionForRect:arect)'s |string|()) as text
if (atext = findText) = true then
(pdfPage's removeAnnotation:anno)
end if
end if
end repeat
end repeat
pdf's writeToFile:pdfPath
return
Although I have another script that prints out the names of all Highlight annotated words, it is less reliable with Preview than Adobe Acrobat Reader as Apple and Adobe implement Highlight differently. If this worked properly, one could generate a list of highlighted text strings and with revision, read that into this current script as a list and then remove the individual list annotation names in one fell swoop.