Skip to main content
Participant
February 14, 2012
Answered

Crop & Save layers

  • February 14, 2012
  • 1 reply
  • 933 views

I am always writing small one-time only tools to process graphics, that are used for one project and then made redundant.

I would like to know if I can achive the following in Photoshop scripting to save me time:

I want a script to go through all layers of a PSD file, crop away all transparency borders of every layer, and save out layers as PNG.

I would also like to keep note of the size of the left and top transparent border that was cropped out, so that I can write out a text file, with x and y co-ords, so that I can reassemble all the layers in their exact position later on in my project.

This would really save me a lot of time, and would be great if I can achieve this in PS scripting.

I worked out file saving, but getting the x & y co-ords of the crop[ped layer still eludes me.

Any help or link to a similar script would be greatly appreciated.

Much thanks.

This topic has been closed for replies.
Correct answer Paul Riggott

Now I need to know how to loop through all layers and set the layer active & it's done!


You can set the ruler units then restore them back to what they  were...

var startRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

//code here

app.preferences.rulerUnits =  startRulerUnits;

For selecting all layers, see ..

http://www.scriptsrus.talktalk.net/Layer%20Saver.htm

Maybe you could modifiy it to add your own features?

1 reply

Paul Riggott
Inspiring
February 14, 2012

Before you crop the layer you can use the layer bounds to get the x, y position...

Make sure ruler units are set to pixels.

var LB = activeDocument.activeLayer.bounds;

var x = LB[0].value;

var y = LB[1].value;

These values can then be written to file.

mopkrayzAuthor
Participant
February 14, 2012

This is one of the most important pieces of script I was looking for! Thanks Paul.

Is it possible to set the ruler units to pixels within the script itself just to be 100% safe?

mopkrayzAuthor
Participant
February 14, 2012

Now I need to know how to loop through all layers and set the layer active & it's done!