Assign shortcut to higligting colours in Preview

Is there a way to assign some shortcut to the colours in the higlight section of Preview, e.g CMD+1 -> highlight the selected text in Yellow CMD+2 -> highlight the selected text in Green ...

enter image description here


Solution 1:

No, not directly, as they do not exist in the normal menu system of the application.

However, one could use .e.g., some AppleScript code in a Run AppleScript action of an Automator Service/Quick Action, assigned a keyboard shortcut, to cause the menu button to open as in the screen shot in your question. It would then be a matter of pressing the key of the first letter of the color, or other menu entry, and then the enter key.

So adding a keyboard shortcut to the Automator Service/Quick Action, e.g., ⌃⌘C, then to change to e.g., Blue, one would press, e.g.,: ⌃⌘C   B  enter

The example AppleScript code, shown below, was tested in Script Editor, and as an Automator Service/Quick Action, under macOS Big Sur with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

As this uses UI Scripting it can be kludgy and prone to failure for a number of different reasons. Changes to the hierarchical UI element structure, timing issues in the value of a delay command, etc. However, when it works, it works! 🤪

  1. In Automator create a new Quick Action with settings as shown in the image below.

  2. Add a Run AppleScript action replacing the default code with the example AppleScript code show below.

  3. Save it as, e.g.,: Change Highlight Color

  4. Then in System Preferences > Keyboard > Shortcuts > Services assign it a keyboard shortcut of, e.g.,: ⌃⌘C

  5. Next trigged the Service/Quick Action manually from the Services menu in Preview so as to trigger the Security & Privacy related dialog boxes and once that is dealt with the keyboard shortcut can be used freely.

Example AppleScript code:

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
            try
                click menu button 1 of radio group 1
            end try
        end try
    end tell
    tell group 2 of ¬
        toolbar 1 of ¬
        group 1 of ¬
        window 1 of ¬
        application process "Preview"
        try
            click menu button 1 of group 1
        on error
            try
                click menu button 1 of radio group 1
            end try
        end try
    end tell
end tell

Notes:

I have edited out the original answer, and an update, to replace all of it with a KISS version of the script. (One can review the edit history if one wants.)

  • As coded, this works on a normal window, Full Screen View window and single/multiple display/monitor scenarios.

Note that if the assigned keyboard shortcut is pressed and either a window does not exist or the Toolbar is set to be hidden the script silently errors out, otherwise it attempts to make the menu show. By coding it this way and adding the additional try statements, it negates the need to ascertain whether the window is a normal window, Full Screen View window and using single/multiple display/monitor.

As this script uses UI Scripting and the hierarchical UI element structure changes between Preview being first opened, and the target menu having been first clicked and is different in a normal window vs. a Full Screen View window, this is why it's written using try statements. It allows for one of the four possible hierarchical UI element structures to be acted upon and done so without causing an error that would stop the process otherwise.

Assuming the necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed and the Toolbar is available, not set to be hidden, this script should work as is until the hierarchical UI element structure changes in updates/upgrades to macOS. If one needs to troubleshoot the script not working, then run the script in Script Editor where one can examine it with the Log pane showing when run. Note that the, e.g., delay 0.5 command will need to be added between the first and second lines of code when testing in Script Editor.

The keyboard shortcut, e.g., ⌃⌘C is just an example because it was not already assigned elsewhere in Preview and can be set to whatever one likes that is not already assigned. (I actually have mine set to ⇧⌘H from a previous answer on a different site and didn't realize it when posting this answer originally.)

Note that once the menu shows, one can also use the up/down arrow keys to navigate the menu and or press the esc key to dismiss it without making a change.

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.