I need to implement a simpler version of Digital Color Meter, that means just a color picker of the current state of my screen.
I do not need any preview, or advanced controls, as Digital Color Meter does. All I need instead is to get the color of the selected pixel. in the clipboard.
Could you give me a hint about how could I implement something like this in macOS?
You don't need to, those lovely guys at Apple have done it for you. Just start the Color Picker, let the user choose a colour and it will be returned to your bash script.
So, the code looks like this in the Terminal or a bash script:
osascript -e "set theColor to choose color default color {0,0,0}"
The user must (probably) click the pipette just above the Cancel button, and click a pixel on the screen then click OK. The RGB returned is in the range 0..65535 for Red, Green and Blue.

As regards putting those RGB values on the Clipboard:
osascript <<EOF
set chosen to (choose color default color {65535, 65535, 65535})
set AppleScript's text item delimiters to ","
set the clipboard to chosen as string
EOF
That will give you something like this on the clipboard:
12000,37456,0
If you prefer something like this on the clipboard:
rgb(64633,1408,8371)
You can use:
osascript <<EOF
set chosen to (choose color default color {65535, 65535, 65535})
set col to "rgb(" & item 1 of chosen & "," & item 2 of chosen & "," &item 3 of chosen & ")"
set the clipboard to col
EOF
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With