In Preview you can add text fields to PDFs using the text tool on the toolbar. Every time I add such a text field the background colour is reverted to transparent. All other text properties (i.e., size, font, foreground colour etc) are remembered from the last text field when creating a new text field.
Is there any way to make Preview also remember the background colour setting when adding text fields to PDFs?
Best Answer
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.
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:
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:
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:
This means under:
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:lastFillColor
variable.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
andUI 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 twoUI elements
, switching2
for3
and3
for2
in the code shown below.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.