Photoshop/Preview, grab RGB values from image

Is there a way to have AppleScript open an image in Photoshop or Preview and grab the RGB values (anywhere in the image) and return in back to the script?

You can do this with Photoshop using color samplers ( up to 4 ) it is slow thou if you need a large array of values.

I saw earlier posts from you here, it seems it doesn’t work for CS6 though - do you have some up to date code?

Something like this may do you. but my applescript is getting weak.

tell application "Adobe Photoshop CS5"
	activate
	tell the current document
		set x to make new color sampler with properties ¬
			{class:color sampler, position:{20, 20}}
		set y to color sampler color of x
		display dialog ((red of y as text) & space & (green of y as text) & space & (blue of y as text))
	end tell
end tell

Given a open RGB file working in pixel measures.

seems “make” doesn’t work anymore.

This script:

tell application "Adobe Photoshop CS6"
	activate
	tell the current document
		set x to make new color sampler with properties ¬
			{class:color sampler, position:{20, 20}}
		set y to color sampler color of x
		display dialog ((red of y as text) & space & (green of y as text) & space & (blue of y as text))
	end tell
end tell

returns this error:
error "Adobe Photoshop CS6 got an error: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.

  • The command “Make” is not currently available." number 8800

Hum you know what it did this with me in CS5. If I launch PS and run once it works run again then error. I will go see if its the same using Adobe’s ESTK or ActionManger. I report back. BTW bridge may also be able to do this whats you workflow.?

I had a quick look and it appears to be OK. In ESTK you can store this as a string and call it for app do javascript

sampleDoc();

function sampleDoc() {

var doc = app.activeDocument;

doc.colorSamplers.removeAll();

var sam = doc.colorSamplers.add( [150,150] );

return sam.color.rgb.red+','+sam.color.rgb.green+','+sam.color.rgb.blue;

};

Hi Mark67,

I’m not familiar with ESTK.

I want to accomplish this:
From Filemaker I will perform an AppleScript that will make a screenshot of a small selection Messages.app, then I want to open this screenshot file in Photoshop and have the AppleScript report back to FileMaker if it was red or blue.

Do you think it could be done?

ESTK is the debugger for Adobe’s JavaScript. You can call this inside of a couple of apps so here is the above in an AppleScript wrap. The variable col has your values to parse.

set JS to "function sampleDoc() {
var doc = app.activeDocument;
doc.colorSamplers.removeAll();
var sam = doc.colorSamplers.add( [50,50] );
return sam.color.rgb.red+','+sam.color.rgb.green+','+sam.color.rgb.blue;
}; sampleDoc();"

tell application "Adobe Photoshop CS5"
	-- open your document here.
	set col to do javascript JS
	display dialog col giving up after 2
	-- Close it here.
end tell

OK, tried it with an image opened in Photoshop, and it gave me this error code:

Adobe Photoshop CS6 got an error: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.

  • Error 8800: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.
  • The command “Make” is not currently available.
    Line: 4
    → var sam = doc.colorSamplers.add( [50,50] );

The only thing I changed in Your script above is that i’m using CS6 - could the error have something to do with that?

Im having no issues with it. can you check what measures the app is using.? I always use pixels in PS. Just wondering if you are trying to sample out side the pic bounds.? what are the dims.?

Color samplers was added in CS5 I think may be CS4 but not limited the the extended app.

Hi Mark67,

when I change Units, Rulers to Pixels in the prefs of Photoshop CS6 the script works.
Is it possible to adjust the JavaScript so that it doesn’t matter how the prefs in Photoshop are set to (Units, Rules)?

In the script where it says “50,50” what does that mean? I guess it is pixel values, but exactly from what?

BTW, do you happen to know how to make a screenshot of a specific selection of the screen (specified by pixels)?

Hello Photoshop has the same object model in AppleScript & JavaScript. Yes you can have the script change the units then put them back when done. I do this with all scripting in Photoshop.

The [50,50] in the script is the x,y location of where to place the sampler in the image. You should see this on screen as a little crosshair.? Just adjust these values to suit your needs.

I don’t know how to capture a selection of screen using an array input. You can make this interactive but I doubt thats what you want. Why you no just chop the image down in photoshop. Effectively the same thing.?

Thanks to you I now have a working script for what I need, many thanks!

The script involves cropping the screen shot in Photoshop just like you suggest.

Could you share some code on how to change the units to pixels?

This is doing inside of the the JavaScript string. Same principle store original in variable, set, then restore from variable.

“sampleDoc();
function sampleDoc() {
var doc, sam, unts;
units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
doc = app.activeDocument;
doc.colorSamplers.removeAll();
sam = doc.colorSamplers.add( [ 20, 20 ] );
app.preferences.rulerUnits = units;
return sam.color.rgb.red+‘,’+sam.color.rgb.green+‘,’+sam.color.rgb.blue;
};”

OK.

I’m a little unsure of how to add new JavaScript code.

Could you please add it the my current one?:

set JS to "function sampleDoc() {
 var doc = app.activeDocument;
 doc.colorSamplers.removeAll();
 var sam = doc.colorSamplers.add( [1,1] );
 return Math.round(sam.color.rgb.red);
 }; sampleDoc();"

It is nothing more than a string of text as far as AppleScript is concerned. I write all my JavaScript using single quotes as much as possible. That way it’s less escaping when/if I put it inside AppleScript string ( double quotes ).

set JS to "sampleDoc();
function sampleDoc() {
var doc, sam, unts;
units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
doc = app.activeDocument;
doc.colorSamplers.removeAll();
sam = doc.colorSamplers.add( [ 20, 20 ] );
app.preferences.rulerUnits = units;
return sam.color.rgb.red+','+sam.color.rgb.green+','+sam.color.rgb.blue;
};"

tell application "Adobe Photoshop CS5"
	-- open your document here.
	set col to do javascript JS
	display dialog col giving up after 2
	-- Close it here.
end tell

Hi,

I can see you changed the JavaScript (other than adding the Units-code), is it more efficient now or what?

Is there a way to output more than variable from the JavaScript so that I can have the three R, G and B values separate? Or is it better to extract the three values from the comma-separated text?

It’s only ever going to result in a string ( or so I think ). I could pass a JS Array object back but I don’t think AppleScript is going to interpret a list. Just split using delimiters.