Jump to content

Mac Preview get coordinates and size of selected area


Recommended Posts



I use applescript and javascript in order to get the coordinates of a selection area in Photoshop and save them to a text file. This works perfectly, but now I am attempting to set this up on a computer, which does not have photoshop installed. Is there a way to do that? I was thinking of Preview and I managed to the necessary file in preview via applescript, but I cannot find anything on how to get the x and y coordinates of the various points of the selected area.


The terminal command screencapture maybe does not help either, because I need the coordinates and sizes (left, top, width, height) in relation to the pic's size. I post below the code to get this information out of Photoshop.


Any ideas?

tell application "Finder"		set sourcefolder to (get selection) as alias		set ocFolder to folder "original-captures" of sourcefolder as text			tell application "Adobe Photoshop CS6"				set theDocument to current document				set myVar to do javascript "function getLayerSizeToCopy(){    var layer = activeDocument.activeLayer; //Grab the currently selected layer    var height = layer.bounds[3]-layer.bounds[1]; //Grab the height    var width = layer.bounds[2]-layer.bounds[0]; //Grab the width        height = height.toString().replace(' px', '');    width = width.toString().replace(' px', '');	try {		var s=app.activeDocument.selection.bounds				s[0] = s[0].toString().replace(' px', '');		s[1] = s[1].toString().replace(' px', '');		s[2] = s[2].toString().replace(' px', '');		s[3] = s[3].toString().replace(' px', '');						var sleft = Math.round(s[0] / (width / 1000))		var stop = Math.round(s[1] / (height / 1000))		var swidth = Math.round((s[2] - s[0]) / (width / 1000))		var sheight = Math.round((s[3] - s[1]) / (height / 1000))						var result = 'Selection "'+sleft+' '+stop+' '+swidth+' '+sheight+'"';		return result		//alert('left = '+sleft+'; top = '+stop+'; width = '+swidth+'; height = '+sheight)						}		catch(e) 	{ 		alert('No selection') 	}}getLayerSizeToCopy();"		tell theDocument			close saving no		end tell						set outputFile to ((sourcefolder as text) & "camsettings.txt") as alias				try			set fileReference to open for access file outputFile with write permission			write myVar to fileReference			close access fileReference		on error			try				close access file outputFile			end try		end try	end tell		end tell
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...