getColorAtPosition(x, y)

Hello,

Does anyone know how I can findout (with Java-Script in BTT) what the color ("1F1F1F")
is at a given x,y screen position ?

I want to store that color in a user BTT variable,
so that I define triggers based on an advanded condition (which can use that user BTT variable)

I am using Ableton Live and the purpose would be to find out what the 'state' the app / screen is in.

for example:
Whether the screen 'shows' the Ableton Play button in green or in white,
or if the 'detailed info' is displayed, etc, etc.

Without knowing that 'state', I trigger now actions which are sometimes not 'valid' in that context ( as for example nothing is playing, i.e. button=green )

Thx,
Christian

Running:
BTT 3.998 (2170) / Mac M1 Max OS Ventura 13.1 /
BTT plugin Stream Deck option
Stream Deck Mobile (on iPad Air) latest version

Not with JavaScript but with AppleScript and also only with the help of the clipboard (screenshot):

use framework "AppKit"
use framework "Foundation"
use framework "QuartzCore"
use scripting additions

set xCoordinate to 100
set yCoordinate to 100

set {redColor, greenColor, blueColor} to getRGBData(xCoordinate, yCoordinate)
set hexValue to (do shell script "printf " & quoted form of "#%02X%02X%02X" & " " & redColor & " " & greenColor & " " & blueColor)

tell application "BetterTouchTool"
	set_string_variable "screenColorAtPoint" to hexValue
end tell

on getRGBData(xCoordinate, yCoordinate)
	do shell script "screencapture -R " & xCoordinate & "," & yCoordinate & ",1,1 -c"
	set imageData to (current application's NSPasteboard's generalPasteboard()'s dataForType:(current application's NSPasteboardTypeTIFF))
	set theImage to (current application's CIImage's imageWithData:imageData)
	set imageRep to (current application's NSBitmapImageRep's alloc()'s initWithCIImage:theImage)
	set theColors to (imageRep's colorAtX:0 y:0)
	set theColors to theColors's colorUsingColorSpace:(current application's NSColorSpace's genericRGBColorSpace())
	set redColor to ((theColors's redComponent()) * 255) as integer
	set greenColor to ((theColors's greenComponent()) * 255) as integer
	set blueColor to ((theColors's blueComponent()) * 255) as integer
	return {redColor, greenColor, blueColor}
end getRGBData

Maybe Andreas has a better idea ...

Dirk,

fantastic, works...

thanks a lot !
Christian

Here is a version without clipboard (via temp file):

use framework "AppKit"
use framework "Foundation"
use framework "QuartzCore"
use scripting additions

set xCoordinate to 100
set yCoordinate to 100

set {redColor, greenColor, blueColor} to getRGBData(xCoordinate, yCoordinate)
set hexValue to (do shell script "printf " & quoted form of "#%02X%02X%02X" & " " & redColor & " " & greenColor & " " & blueColor)

tell application "BetterTouchTool"
	set_string_variable "screenColorAtPoint" to hexValue
end tell

on getRGBData(xCoordinate, yCoordinate)
	set imagePath to POSIX path of ((path to temporary items folder as text) & "image.png")
	do shell script "screencapture -R " & xCoordinate & "," & yCoordinate & ",1,1 " & imagePath
	set imageRep to current application's NSBitmapImageRep's imageRepWithContentsOfFile:imagePath
	set theColors to (imageRep's colorAtX:0 y:0)
	set theColors to theColors's colorUsingColorSpace:(current application's NSColorSpace's genericRGBColorSpace())
	set redColor to ((theColors's redComponent()) * 255) as integer
	set greenColor to ((theColors's greenComponent()) * 255) as integer
	set blueColor to ((theColors's blueComponent()) * 255) as integer
	do shell script "rm " & imagePath
	return {redColor, greenColor, blueColor}
end getRGBData

You can also always get the color under the cursor via this (requires one of the recent BTT alphas)

tell application "BetterTouchTool"
	get_string_variable "color_under_cursor"
end tell

(can be combined with the move mouse to position and restore saved mouse position actions)

Andreas,

great, that might be easier....

I am on BetterTouchTool 3.9981 ...ist zurzeit die neueste verfügbare Version....
but somehow the get_string_variable "color_under_cursor" part
does not yet work ?


tell application "BetterTouchTool"
	set xy to  get_string_variable "color_under_cursor"
	set_string_variable "screenColorAtPoint" to xy
end tell

Thx,
Christian

Ahh bug in the code for retrieving these variables. I'll fix it now.

Top !

Hello Andreas,

I noticed that there is a variable created after the action "Save Mouse position" is run
like → saved_mouse_position: {717.26171875, 350.6171875}

If I change that variable to a differnt value like
saved_mouse_position: {100, 100}
why is "Restore Mouse Position" not 'moving' the cursor to that (new) position ?

Thx,
Christian

Running:

BTT 3.998 (2170) / Mac M1 Max OS Ventura 13.1 /
BTT plugin Stream Deck option
Stream Deck Mobile (on iPad Air) latest version

which function are you using to change the variable?

Hallo,

I use this apple script to (re)set the saved_mouse_position
the (last two rows...)

the var values are:

BTTWindowSwitcherActive: 0
org_mouse_xy: {1244.609375, 167.96484375}
BTTActiveAppBundleIdentifier: com.hegenberg.BetterTouchTool
ID: MacBookPro
screenColorAtPoint: #D2D2D2
OutputVolume: -1
BuiltInDisplayBrightness: -1
BTTLastTriggeredAction: 155
BTT_OPENED_URL: https://folivora.ai
BTTLastTriggeredUUID: 445D74EE-2655-4ECD-939B-ABA9219EC7AA
BTTLastTriggerTime: 695055229.49256
saved_mouse_position: {1244.609375, 167.96484375}
mouse_xy: {540, 80}
tell application "BetterTouchTool"
	
	set xy to get_string_variable "color_under_cursor"
	set_string_variable "screenColorAtPoint" to xy
	
	set mouse_xy to get_string_variable "saved_mouse_position"
	set_string_variable "mouse_xy" to mouse_xy
	
	
	
	set org_mouse_xy to get_string_variable "org_mouse_xy"
	set_string_variable "saved_mouse_position" to org_mouse_xy
	
end tell

then right after that Apple script I call the "restore mouse position" action...
but the cursors stays at {540, 80}

Thx,
Christian

Thx Andreas,
the new version 3.9989 / 2189 fixed that...