Giorgio
Giorgio

Reputation: 1613

Check pixel color and trigger a bash script

How can we check the color of a determined pixel(x,y) and, if it is equal to a certain value, run a bash script.

In particular I'd like to write a script that:

  1. Wait for a determined key being pressed (in particular 'j' or 'f' or 'space' or 'y').
  2. Compare the color of 4 pixels to a color. If we found that one or more (if there are more we can pick the first or a random one) we can go to step 2.
  3. Run a bash script.
  4. Go to step 1.

I know only how to complete step 3, the first 2 steps are out of my coding skill. Can you help me?

Upvotes: 4

Views: 2903

Answers (2)

CRGreen
CRGreen

Reputation: 3444

As for checking for keystrokes, I use this command line tool:

http://www.klieme.ch/pub/checkModifierKeys.zip

, which is discussed on macscripter.net (sorry, can't post this as link). This command line command is utilized in a similar fashion to what I describe below ("do shell script"). Another command line tool is used to get the pixel position of the mouse:

www.bluem.net/en/mac/cliclick/

(again, sorry for inability to post this as link).

For getting pixel color ...

See my blog post about using python for this (my method is based on similar solutions): http://thechrisgreen.blogspot.com/2013/04/python-script-for-getting-pixel-color.html

I have an AppleScript script that does "do shell script" with this python script, sets a variable "rgb" to the (string) result, then does

run script rgb

which gets you a list like

{255,255,255}

Seeing this rather late in the game ... this is for future surfers I guess ...

Upvotes: 1

Lri
Lri

Reputation: 27613

I don't know about the first step, but you could take a screenshot and use ImageMagick to print the colors of pixels.

tmp=/tmp/$(uuidgen).png
trap "rm $tmp" exit
screencapture $tmp
/usr/local/bin/convert $tmp[1x1+1000+500] $tmp[1x1+1100+500]\
$tmp[1x1+1000+510] $tmp[1x1+1100+510] txt: | grep '#FFFFFF' || exit

Upvotes: 3

Related Questions