Help: Change the Color of the Selected Object in Preview

Hey guys!

Needed a bit of advice. What’s the best way to change the color of the selected object in Preview, per this video: http://d.pr/mHfG/TH4It1Sy

I know how to get the markup toolbar to open, but changing the colors is a challenge. Peter’s suggestion was “relative to window, pause, click” or Applescript but suggested that I ask here.

Not sure how I would do this with AppleScript, although that’d be nice and direct if possible.

Any ideas?

Thank you,

Eugene

This AppleScript works here:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Keyboard Maestro Engine"
  set btnNum to (getvariable "zzzColorButtonNumber") as integer
end tell

tell application "System Events"
  tell application process "Preview"
    tell first window
      tell (first toolbar whose description is "toolbar")
        if value of (first checkbox whose description is "Markup") is 0 then
          tell (first checkbox whose description is "Markup")
            perform action "AXPress"
          end tell
        end if
      end tell
      tell (first toolbar whose description is "edit toolbar")
        tell (first color well whose description is "Border color")
          perform action "AXPress"
          tell first pop over
            tell (first UI element whose description is "grid")
              tell button btnNum
                perform action "AXPress"
              end tell
            end tell
          end tell
        end tell
      end tell
    end tell
  end tell
end tell

‌The script takes the color button number from a KM variable.

Here the complete macro:

_[test] Select Border Color in Preview.kmmacros (3.0 KB)

Instructions:

  1. Set the desired color button number in the green action of the macro.
  • The color buttons are the color fields in Preview’s color well. They are numbered from left to right, top to bottom.
  1. Make sure your document is open in Preview.
  2. Launch the macro.
2 Likes

Wow - this is truly amazing! Thank you so much Tom. Exactly what I was looking for and much appreciated. :+1::pray:

Great!

If you think a post has solved your problem you can check the “Problem solved” checkbox at the bottom of the post that solved your problem.

Hi guys,

On one of my Macs (although not the other one), I now get an error when running this AppleScript. The error is as follows. Any ideas?

osascript: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" cannot be used with the current OS because it has no OSAXHandlers entry in its Info.plist.
/var/folders/tl/4gngpls16hs08fhcm21991jr0000gn/T/Keyboard-Maestro-Script-8B7F3714-BC0F-4D79-AEAB-1FAA4126A24E:359:375: execution error: System Events got an error: Can’t get toolbar 1 of window 1 of application process "Preview" whose description = "toolbar". Invalid index. (-1719)

Thanks again!

You have a scripting addition installed that is not compatible with your OS.

This problem has been extensively covered in another thread, starting with this post. For the Adobe Unit Types.osax see especially this post:

1 Like

Hi Tom - thank you for the response! I updated the Adobe Unit Types file per that previous post, and now get this error. Any ideas?

/var/folders/tl/4gngpls16hs08fhcm21991jr0000gn/T/Keyboard-Maestro-Script-B0E3DC75-6731-41DA-AD44-07C341154E65:359:375: execution error: System Events got an error: Can’t get toolbar 1 of window 1 of application process "Preview" whose description = "toolbar". Invalid index. (-1719)

It works here, so I fear I can’t help much.

What OS version is running on the Mac where the script doesn’t work?

Thank you for the response! Looks like restarting Preview fixed it. Greatly dislike the bugs within MacOS these days, but greatly appreciate your help!