Remove / export all highlighting from Preview

I got more than 200 highlighting on a single word "WWWW"

How do I remove them all ?

I do not understand how they all appeared, since I usually do not highlight so many words

Also would like to remove all highlighting, or export them. Is it possible ?

Posted on Mar 9, 2024 3:06 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 9, 2024 7:32 AM

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.

6 replies
Question marked as Top-ranking reply

Mar 9, 2024 7:32 AM in response to VikingOSX

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.

Mar 9, 2024 3:45 AM in response to Bomiboll

Open the PDF in Preview. Press cmd+i to open the Inspector and on its toolbar, click the right-most tool. This will expose all of the assigned annotations (including Highlight) in a list. Press cmd+A to select all, and then backspace to remove all annotations. Then click on the PDF to dismiss any text selections that linger.


If you only want to remove Highlight annotations in the entire PDF, I have an AppleScript that can do that without needing Preview.


Although the PDF should then autosave, you can manually click Save if you prefer.

Mar 9, 2024 4:29 AM in response to Bomiboll

Bomiboll wrote:

Cmd A has no effect.
Could you send that Script?


I tried it and I see that, rather surprisingly, Command-A is not working for me, either.


However, selecting one, and then Shift-clicking on another lets me select a whole range of highlights, then clicking Delete lets me delete a bunch of them at once.

It seems to require that the annotations selected are of the same type - e.g. many highlights in sequence, ok, but if they are interspersed with text annotations I can't select them all at once.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Remove / export all highlighting from Preview

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.