Question

I'd like to be able to use an eyedropper tool on command. The built-in Mac colour picker is nice, so to that end, I set up a keyboard shortcut using BetterTouchTool to open the colour picker; it does so simply by running the choose color AppleScript command.

This is only a partial solution, because I still need to click the eyedropper icon to get what I want. Apart from the minor nuisance of an extraneous click, the problem is that the click itself changes focus away from the application I'm using. (For example, suppose I'm using the eyedropper to test the :focus state of an element on a website.)

Is there a way to tell my script to run the eyedropper tool after opening the colour picker? In other words, I would like to automatically click the button indicated in this screenshot:

enter image description here

Was it helpful?

Solution

Updated Answer

I download the trial of BetterTouchTool and resolved the issue in the following manner:

  1. Added a keyboard shortcut assigning it: ⇧⌃P
  2. Added the built-in Show Color Picker action to the aforementioned keyboard shortcut.
  3. Added a Run AppleScript (async in background) action, to the aforementioned keyboard shortcut, with the following AppleScript code:
    tell application "System Events" to ¬
        click (checkbox 1 of ¬
            splitter group 1 of ¬
            window "Colors" of ¬
            application process "BetterTouchTool")

Note that this was done under macOS Big Sur and it works without issue after the initial setup and permissions setting as were prompted.

Now when I press ⇧⌃P the Color window opens and the color picker button is clicked, and it is active to select a color where the mouse pointer was when the keyboard shortcut was pressed.


Original Answer

I do not have BetterTouchTool, so using Script Editor I find that if one just runs the choose color command, the Colors window is modal and does not allow any additional code that would click the color picker button to run.

To work around this, the choose color command can be wrapped within an ignoring application responses statement. However, while this allows the color picker button to be clicked by additional code, one does not get back, e.g. {14896, 20151, 65429} from the choose color command when the OK button is cliched after picking the color.

Example AppleScript code:

ignoring application responses
    choose color
end ignoring

delay 0.5

tell application "System Events" to ¬
    click (checkbox 1 of ¬
        splitter group 1 of ¬
        window "Colors" of ¬
        application process "Script Editor")

This was tested under macOS Catalina.

OTHER TIPS

I’m not sure if this will help you, but if all you are looking to do is retrieve the color values at your exact mouse coordinates, this alternate solution may be an option for you.

This following AppleScript code added as an AppleScript action to a new Automator Quick Action, will allow you to retrieve color values at your exact mouse coordinates, without the need to call on System Events or any Color Picker GUI.

enter image description here

I saved my version in Automator as “Eyedropper At Mouse Location.workflow”. I then assigned it the keyboard shortcut ⇧⌃P in System Preferences/Keyboard/Shortcuts/Services. Now, pressing the keyboard shortcut ⇧⌃P while in any application, will retrieve the RGB color values at my exact mouse location.

—————————————————————————————————————-

This code requires the third-party utility, Cliclick.

“Cliclick” is short for “Command-Line Interface Click”. It is a a tiny shell/Terminal application that will emulate mouse clicks or series of mouse clicks (including doubleclicks and control-clicks) at arbitrary screen coordinates. Moreover, it lets you move the mouse, get the current mouse coordinates, press modifier keys etc.

It's free to download but it's donationware, and is easy to install.

On my system I have. cliclick in the following directory: /usr/local/bin/. Because of this location, in my AppleScript code and in Terminal app, I need to use the full path to cliclick to call the command. For example: do shell script "/usr/local/bin/cliclick rc:." In AppleScript is telling cliclick to right-click.

—————————————————————————————————————-

I’m not quite sure what you intend to do with the color values retrieved so I created three different variables… rgbColors (R,G,B values as integers as record), theColors (R,G,B values as text as list), and theColorsAsText (R,G,B values as 1 long string)

property rgbColors : {R:0, G:0, B:0}

set theColors to words of ¬
    ((do shell script "/usr/local/bin/cliclick cp:.") as string) as list

set theColorsAsText to (("Red:" & theColors's item 1) & "  Green:" & ¬
    theColors's item 2) & "  Blue:" & theColors's item 3

(* Not Necessary But This Will Store The Values As A Record Making It ¬
Easier To Call On Or Manipulate, If Need Be, In Other Parts Of The Script*)
set {rgbColors's R, rgbColors's G, rgbColors's B} to ¬
    {theColors's item 1 as integer, theColors's item 2 as integer, ¬
        theColors's item 3 as integer}

activate
display dialog theColorsAsText buttons {"OK"} ¬
    default button "OK" giving up after 4
Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top