Search code examples
macosbashapplescriptcomparepixel

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?


Solution

  • 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