1

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

2

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.

8
  • Thank you for the detailed response. I tried your code, but unfortunately I don't see a difference (i.e., I get the same result as just running choose color: the modal appears, but the eyedropper tool doesn't run automatically). I'm using Big Sur, if that makes a difference. – Théophile Mar 4 at 16:42
  • @Théophile, I just copied and pasted the example AppleScript code into Script Editor under macOS Big Sur and it did indeed bring up the Colors window and clicked the color picker button. So it does work in that respect from Script Editor. If you are copy and pasting the example AppleScript code in any other application then application process "Script Editor" is of course not going to work as it did not call it. – user3439894 Mar 4 at 17:31
  • Ah, I see. That makes sense. Indeed, it works as you described from the Script Editor. Is there a way to change the last line to use the "current process" (I'm not sure what the right terminology is) or to search through all processes? The tool I use lets me assign keyboard shortcuts to commands, so I can hit ^+Shift+P at any point to bring up the colour modal. So in short, is there a solution that doesn't involve opening the Script Editor? – Théophile Mar 4 at 17:48
  • @Théophile, There probably is a way, and I actually played around with it a bit, triggering it from FastScripts, but didn't get anywhere. I hope to look into it a bit more later but wanted you to know what I had found out so you could do some investigating on your own too. – user3439894 Mar 4 at 17:57
  • Thank you, I appreciate your help. – Théophile Mar 4 at 18:06
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .