Skip to main content
Inspiring
August 10, 2023
Question

Finding the dimensions of a smart object, even when the smart object contains negative space...

  • August 10, 2023
  • 2 replies
  • 459 views

Hi all,

 

I'm trying to find the true dimensions of a smart object layer in Photoshop... The following piece of applescript works as long as there is no negative space in the smart object. If there is negative space, the following is no longer accurate. You can click on the layer and it shows what the below applescript is seeing, however, clicking on a corner of the layer can sometimes reveal more than what is being shown originally.

 

tell application "Adobe Photoshop 2023"
	
	tell current document
		
		set smartObjectDimensions to bounds of current layer
		set smartObjectWidth to (item 3 of smartObjectDimensions) - (item 1 of smartObjectDimensions)
		set smartObjectHeight to (item 4 of smartObjectDimensions) - (item 2 of smartObjectDimensions)
		
	end tell
	
end tell

 

Any help would be much appreciated!

 

Cheers,

Sam

This topic has been closed for replies.

2 replies

c.pfaffenbichler
Community Expert
Community Expert
August 20, 2023
// 2023, use it at your own risk;
if (app.documents.length > 0) {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var layerDesc = executeActionGet(ref);
var soDesc = layerDesc.getObjectValue(stringIDToTypeID('smartObject'));
var soMoreDesc = layerDesc.getObjectValue(stringIDToTypeID('smartObjectMore'));
var transform = soMoreDesc.getList(stringIDToTypeID("transform"));
var xx = new Array;
for (var m = 0; m < transform.count; m++) {
xx.push(transform.getDouble(m))
};
alert ("\n"+xx.join("\n"));
};
Stephen Marsh
Community Expert
Community Expert
August 10, 2023

I don't use AS, is "set" correct and not "get"? Anyway, it's easy in JS and AS can run JS:

 

https://gist.github.com/MarshySwamp/ef345ef3dec60a843465347ee6fcae2f

 

The wrinkle is always layer effects such as drop shadows or glow, one has to dupe, rasterize, get bounds and calculate the width and height.

 

P.S. Can you provide a sample PSD file to illustrate?

Inspiring
August 11, 2023

Hi @Stephen Marsh , thanks for your response! I'm always up for using Javascript in my Applescripts, but both options still return the same result, as it ignores the empty space around it. So, to hopefully explain my example, I have a smart object here, which when highlighted, only shows the space with pixels in. These are the dimensions which are stripped from the layer when using my Applescript, as well as the Javascript you recommended.

Yet, when I click on the corner to enlarge it, the true size of the smart object appears, which includes all negative space too. I know how to extract the smart object size without opening it, but I need to know the size of the smart object as it is on this image, as scaling etc may have happened. If I was to triggger the script on the layer in the below state, it hangs, as it assumes Photoshop is busy doing something.

 

 

Regarding your first comment, I always use 'set' to retrive any variables... I don't ever use 'get'. If I'm testing anything I do use 'return' after I've used 'set' which stops the script at that point, returning the variable in question.

Stephen Marsh
Community Expert
Community Expert
August 11, 2023

I'm sure that this has been covered previously, but where?