Change the default background colour for text fields added to PDFs in Preview?
Solution 1:
Is there any way to make Preview also remember the background colour setting when adding text fields to PDFs?
There is no Preferences… setting in Preview to remember the last used color shown in its Color Well for the Fill Color when using the Tools > Annotate > Text menu item, or its ^⌘T keyboard shortcut , or clicking Text on its Markup Toolbar.
If there is an unpublished setting to change the default behavior, i.e. the Fill Color is automatically reset to none upon the aforementioned actions, I am unaware of it.
Workaround Solution 1
As Preview does remember the last used color shown in its Color Well for the Fill Color on its Markup Toolbar, until using the Tools > Annotate > Text menu item, or its ^⌘T keyboard shortcut, or clicking Text on its Markup Toolbar, this allows for the use of a Service/Quick Action in Automator, using a Run AppleScript action with UI Scripting code, to then be assigned a keyboard shortcut that when pressed will automate the process of setting the last used fill color to a new text field.
- 1The colors used in this workaround are from the standard pallet as shown when clicking on Fill Color on the Markup Toolbar, and shown in the image below.
The example AppleScript code, shown below, was tested under macOS High Sierra thru macOS Big Sur, with Language & Region set to English (US) in System Preferences, and worked for me without issue, sans adding Preview in: System Preferences > Security & Privacy > Privacy > Accessibility
Using Automator I created a Service/Quick Action setting Workflow receives [no input] in [Preview], with a Run AppleScript action, replacing the default code with the example AppleScript code, shown below.
Saved the Automator workflow as Preview - Add Text with last Fill Color, then assigned a keyboard shortcut of ⇧⌘T in System Preferences > Keyboard > Shortcuts > Services for the Automator Service/Quick Action.
Example AppleScript code:
tell application "Preview"
activate
if not (exists front window) or ¬
not (visible of front window) ¬
then return
end tell
tell application "System Events"
tell application process "Preview"
if (exists menu item ¬
"Show Markup Toolbar" of ¬
menu "View" of first menu bar) then ¬
click menu item "Show Markup Toolbar" of ¬
menu "View" of first menu bar
tell front window
tell (toolbars whose ¬
description is "edit toolbar")
set lastFillColor to the ¬
value of every color well whose ¬
help is "Fill Color"
click (every button ¬
whose description is "Text")
tell (every color well whose ¬
help is "Fill Color")
click
click (every button of ¬
UI element 2 of ¬
UI element 1 whose ¬
description is ¬
lastFillColor)
if result is missing value then
click (every button of ¬
UI element 3 of ¬
UI element 1 whose ¬
description is ¬
lastFillColor)
end if
end tell
end tell
end tell
end tell
end tell
Notes
If you'd prefer to use the default keyboard shortcut (⌃⌘T) for the Automator Service/Quick Action, then you can change the default keyboard shortcut used by Preview by adding it to: System Preferences > Keyboard > Shortcuts > App Shortcuts
Click the [+] button and set:
- Application: [Preview]
- Menu Title: Text
- Keyboard Shortcut: ⇧⌘T
Then assigned a keyboard shortcut of ⌃⌘T in System Preferences > Keyboard > Shortcuts > Services for the, e.g. Preview - Add Text with last Fill Color Service/Quick Action.
The example AppleScript code shown, as is, worked under macOS High Sierra thru macOS Big Sur without needing to be modified, nonetheless, future releases of macOS may require modifications beyond any localizations done for other settings with Language & Region when set to other than English (US) in System Preferences.
For this Automator Service/Quick Action, as coded, to work properly, in regards to Accessibility settings and Automation settings, one should be prompted to allow certain events and you may see messages such as:
- The action “Run AppleScript” encountered an error: “System Events got an error: com.automator.runner.xpc is not allowed assistive access.”
- “Preview“ would to control this computer using accessibility features. Grant access to this application in Security & Privacy > Privacy preferences, located in System Preferences.
- “Preview“ wants access to control “System Events“. Allowing control will provide access to documents and data in “System Events“, and to perform actions within that app.
- Or similar type messages.
This means under:
-
System Preferences > Security & Privacy > Privacy > Accessibility
- Preview needs to be added and checked.
-
System Preferences > Security & Privacy > Privacy > Automation
- Preview should show with System Events checked.
As applicable to the version of macOS this is run under.
All testing was done on an Intel based Mac and I cannot speak to whether or not any of this, as is, will work on an M1 based Mac.
How It Works
In the first tell
block, Preview checks that a window exists and is not minimized, otherwise the remaining code would fail, and stops the script if the conditions warrants.
In the second tell
block, System Events does the following:
- Checks whether or not the Markup Toolbar is showing and if not, shows it.
- Reads the current value of the selected color of the Color Well for the Fill Color, assigning it to the
lastFillColor
variable. - Creates the Text annotation.
- Sets the fill color of the Text annotation to the value stored in the
lastFillColor
variable.
Understanding the code for clicking the Color Palette
Unfortunately UI Scripting is necessary to perform the task required to set the fill color by clicking on the color palette just as if being done manually, however, automating this certainly beats dealing with continued manual repetition.
In the image of the Color Palette below, you will see how its broken-down into three UI elements
. UI element2
and UI element 3
are two distinct sections of the color palette.
As I tend to use the colors from the larger selection (UI element 2
), I've coded it to click this section first, and if the target is not there, I click the smaller section (UI element 3
).
I'm mentioning this so if you typically choose a color from the smaller section (UI element 3
), you'll want to switch the numbers of the two UI elements
, switching 2
for 3
and 3
for 2
in the code shown below.
tell (every color well whose ¬
help is "Fill Color")
click
click (every button of ¬
UI element 2 of ¬
UI element 1 whose ¬
description is ¬
lastFillColor)
if result is missing value then
click (every button of ¬
UI element 3 of ¬
UI element 1 whose ¬
description is ¬
lastFillColor)
end if
end tell
The example AppleScript code is just that and sans the included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.