Crop an image from a selection - Photoshop CS3

Hello
I’ve got to write an applescript to make photoshop crop an image from a selection, I did this, but then I can’t find the command that would ennable Photoshop to crop the image :

tell docRef
delete (every path item whose kind ≠clipping)
set detourage to (every path item whose kind = clipping)
select detourage
create selection detourage feather amount 0 with antialiasing
deselect detourage
invert selection
fill selection with contents {class:RGB color, red:255, green:255, blue:255}
invert selection
expand selection by 30

Have you any ideas to help me ?
Thanks for your help

Model: Mac Pro Dual-Core
AppleScript: 1.10.7
Browser: Firefox 3.0.5
Operating System: Mac OS X (10.4)

I don’t have CS3 but getting bounds of selection is broken in CS2. You can either try it in CS3 or make your image a layer, inverse selection, clear then trim based on transparent or use script listener as you can do this in Photoshops GUI. This is it from the latter.

tell application "Adobe Photoshop CS2"
	activate
	set Doc_Ref to the current document
	tell Doc_Ref
		do javascript "Crop(); function Crop() {function cTID(s) { return app.charIDToTypeID(s); }; function sTID(s) { return app.stringIDToTypeID(s); }; var desc001 = new ActionDescriptor(); executeAction( cTID('Crop'), desc001, DialogModes.NO );};" show debugger on runtime error
	end tell
end tell

Hi clado
Something like the below should do the trick.

tell application "Adobe Photoshop CS3"
	activate
	crop document 1 bounds {0, 0, 50, 50} --square
end tell

crop document 1 bounds {0, 0, 50, 50} --square

This means that my image will be 50x50 pixels and that not what I want.
I want photoshop to resize it at the size of my selection as in the menu “image” but it doesn’t seem possible.
Do you know if it’s possible to tell Applescript to run a photoshop script (that would be able to crop my image from my selection) ?

Thanks again for your time

Looks like that menu selection is not directly scriptable. You could do it with system events, but I would do it this way. It’s two steps instead of one (once you have your selection that is - which it looks like you do).

tell application "Adobe Photoshop CS3"
	tell current document
		set theCropBounds to bounds of selection
		crop bounds theCropBounds
	end tell
end tell

Model: iMac Intel 10.5.5
Browser: Firefox 3.0.2
Operating System: Mac OS X (10.5)

Yes Matt-Boy, you’re great :lol:

I had found something else :
trim current document basing trim on top left pixel with top trim, right trim, left trim and bottom trim
But it didn’t allow me to have white around my image.

Thank you very much