Applescript to select a highlight color in Preview

Viewed 375

I've recently found Applescript after desperate hours trying to figure out a way to do the trick since I use Preview A LOT. The button to open highlighting colors doesn't have any menu item or keyboard shortcut (only for general highlight that is yellow, my plan was to create shortcuts for the rest of the colors available), it's only located in the toolbar. The code I've encountered for similar problems didn't work (probably due to my serious lack of knowledge in the area...), so if anyone could come up with something I would really appreciate it!

1 Answers

First I'd like to make note that Preview does not contain an AppleScript dictionary file, e.g. Preview.sdef, and as such, sans a minimal set of default commands, is not easily scriptable beyond the default commands and requires UI Scripting with System Events to achieve the goal.

Note that UI Scripting can be kludgy and has its own issues and requires granting privileges in System Preferences > Security & Privacy > Privacy > Accessibility, which one should be prompted for if necessary, however I present two options for your consideration.



Option 1

This first option simply triggers the Highlight menu where you can then press the key of the first character of a color, after which you press the enter to set the chosen color. For the color Purple, since there is also Pink, pressing P will of course highlight Pink, so pressing Pu will select Purple. You can also use the Up/Down arrow keys to navigate the menu.

This would allow you to actuate the menu with one assigned keyboard shortcut and not have to explicitly assign a keyboard shortcut for each individual color.

Example AppleScript code:

tell application "Preview" to activate
delay 0.1
tell application "System Events"
    tell group 2 of ¬
        toolbar 1 of ¬
        window 1 of ¬
        application process "Preview"
        
        try
            click menu button 1 of group 1
        on error
            click menu button 1 of radio group 1
        end try
        
    end tell
end tell

Note: In the example AppleScript code shown above make note that there is an try statement with error handling to click the target menu. This is because the hierarchical UI element structure changes based on whether or not highlighting has already been used in the current document. This also allows for the script to silently fail if the keyboard shortcut to trigger it is pressed on a document that highlighting cannot be preformed on.



Option 2

This next option presents a list to choose from and then sets the highlight color based on the selection.

Example AppleScript code:

set colorList to ¬
    {"Yellow", "Green", "Blue", "Pink", "Purple"}

set chosenHighlightColor to ¬
    (choose from list colorList ¬
        with title "Select Highlight Color") ¬
        as string

if chosenHighlightColor is "false" then return

tell application "Preview" to activate

tell application "System Events"
    tell group 2 of ¬
        toolbar 1 of ¬
        window 1 of ¬
        application process "Preview"
        
        try
            click menu button 1 of group 1
        on error
            click menu button 1 of radio group 1
        end try
        delay 0.05
        try
            click menu item chosenHighlightColor of menu 1
        end try
        
    end tell
end tell

Note: In the example AppleScript code shown above make note that there is an try statement with error handling to click the target menu. This is because the hierarchical UI element structure changes based on whether or not highlighting has already been used in the current document. This also allows for the script to silently fail if the keyboard shortcut to trigger it is pressed on a document that highlighting cannot be preformed on.



Automator Service/Quick Action and Keyboard Shortcut

To use either of these examples as a keyboard shortcut, create an Automator Service/Quick Action with a Run Shell Script action replacing the default code with the example AppleScript code.

Then in System Preferences > Keyboard > Shortcuts > Services assign it a keyboard shortcut. I used ⇧⌘H as it was not already assigned to anything in Preview.


Notes:

  • This was tested and worked for me as is under macOS Catalina and macOS Big Sur with Language & Region setting in System Preferences set to English US.
  • Does not work if the document is in Full Screen view.

Select Highlight Color

enter image description here


Automator Service/Quick Action

enter image description here


Note: The example AppleScript code is just that and sans any 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.

Related