3

My AppleScript app needs to get color of pixel in coordinates 123, 456 in screen.

8

I found a way to do this using only built-in components:

do shell script "screencapture -R123,456,1,1 -t bmp $TMPDIR/test.bmp && \
                 xxd -p -l 3 -s 54 $TMPDIR/test.bmp | \
                 sed 's/\\(..\\)\\(..\\)\\(..\\)/\\3\\2\\1/'"

Output is the color in hex, for example, ffffff for white.

The screencapture command takes a 1x1 screenshot of the coordinates, and outputs it in the bmp format to a temporary location.

The xxd command reads 3 bytes from that file at offset 54. 54 is the length of the header in the generated bmp file, and the next 3 bytes are the color of the pixel.

The sed command swaps the first two characters of the output with the last two. This is because the bmp stores the colors in reverse of the normal order - BBGGRR. This command swaps the order to the normal RRGGBB.

  • 1
    Damn you for trumping my answer. Nicely done. – jnovack Jan 23 '18 at 21:08
1

Possible, but only if you chain together multiple events:

  1. Take a screenshot do shell script "screencapture -l" & windowid & " ~/test.png"
  2. Crop around the specific pixel using ImageMagick

Output:

convert ~/test.png -crop 1x1+123+456 txt:-
# ImageMagick pixel enumeration: 1,1,255,rgba
0,0: (  0, 74,117,251)  #004A75FB  rgba(0,74,117,0.984314)

The idea is to crop around a specific pixel, then output it to the special txt: format, with an output file name of - which is of course standard output.

You can leave out the the -crop 1x1+X+Y business to just get a listing of all pixels. Also note that you image must have an alpha channel to see the alpha channel, otherwise it just won't get included.

  • 1
    Every time I think that's the craziest question that no way will someone know an answer, do I happen upon an awesome answer like this that just nails it. Internet FTW! – bmike Apr 4 '13 at 21:56
  • Thanks. I will try this, but maybe Cocoa could do this too? – user46761 Apr 5 '13 at 5:23
  • Doubtful that you can do it natively. Although, if it does work for you, I would be honored to get a lil' green checkbox for the answer :) – jnovack Apr 5 '13 at 12:50

You must log in to answer this question.

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