Skip to main content
Inspiring
March 18, 2011
Answered

Get/Apply Canvas Size

  • March 18, 2011
  • 2 replies
  • 7651 views

I'm doing some clean-up scripting for some photoshop processes used where I work and have thought of a way to eliminate having to modify the actions but am unsure the script used to perform the actions.  What I need is a script that will get the canvas size of one document (I'll call it original.psd for easy reference) and apply it to another document without damaging or resizing the layers present (I'll call this one target.psd).

I have most of the script already, so I just need the lines that will

a) get the canvas size of original.psd and

b) apply that canvas size to target.psd

Thanks in advance for any help!

DGolberg

This topic has been closed for replies.
Correct answer Michael_L_Hale

A script need to reference a document before it can work with it. You did not tell the order the images were opened or their names. The code below will get the canvas size of the first opened document and apply it to the second opened.

var w = app.documents[0].width;// the width of the first open document
var h = app.documents[0].height;
app.activeDocument = app.documents[1];// make sure the second open document is active
app.activeDocument.resizeCanvas (w, h);

If you want the document order switched just swap the 0 and 1.

2 replies

Michael_L_HaleCorrect answer
Inspiring
March 19, 2011

A script need to reference a document before it can work with it. You did not tell the order the images were opened or their names. The code below will get the canvas size of the first opened document and apply it to the second opened.

var w = app.documents[0].width;// the width of the first open document
var h = app.documents[0].height;
app.activeDocument = app.documents[1];// make sure the second open document is active
app.activeDocument.resizeCanvas (w, h);

If you want the document order switched just swap the 0 and 1.

dgolbergAuthor
Inspiring
March 19, 2011

Thanks for all the help guys!  I actually solved it myself yesterday, but was unable to get into the forums to post what I came up with.  Michael hit it almost spot on though!  The one document is always named original.psd, the other is a random image file such as .jpg that is opened with a batch process.  Anyway, here's the code I am using, and I included quotes in case anyone else might have use for it.

//store's the batch opened file name for later use.

var myDoc = app.activeDocument;

//stores the source file name
var theLayerSetDoc = app.documents.getByName("original.psd");

//sets the focus on the source file

app.activeDocument = theLayerSetDoc;

//changes the ruler units to pixels

preferences.rulerUnits = Units.PIXELS;

//these 2 lines grab the current document's width and height in pixels and stores them for use later

imageWidth = activeDocument.width.as('px');
imageHeight = activeDocument.height.as('px');

//changes the active document back to the batch opened file (also works with manually opened files, just focus on this document before running the script)

app.activeDocument = myDoc;

//ensures the ruler is set to pixels in this document as well, then resizes the canvas based on the previously stored width and height form the original.psd.
preferences.rulerUnits = Units.PIXELS;
activeDocument.resizeCanvas(imageWidth,imageHeight,AnchorPosition.MIDDLECENTER);

//remaining unrelated script follows here

Michael basically had the code that I was looking for though.  Thanks again for the help though!

DGolberg

Edit: Forgot to mention; the actions leading up to the script handle converting the image file's background to the layer "Layer 1".  I didn't see the need to do this in script, so decided to leave it as an action.

JJMack
Community Expert
Community Expert
March 18, 2011

If the target psd file has a Photoshop Background Layer and you change the canvas size that background layer will be changes new pixels will be added if the new canvas dimensions size is larger then or old ones will be cropped it the new canvas is smaller. If the PDS has a background layer it is a special Photoshop layer its the bottom layer that does not support transparency so it will always be at least partially locked and be canvas size .  Other layer do not have this special property and they can be any size they can have dimensions lager then smaller then and the same as the canvas size dimensions. Only unmasked Layer areas over the canvas area will be rendered or blended into the composite view.

JJMack
dgolbergAuthor
Inspiring
March 18, 2011

Not entirely sure what you're saying, but it sounds like you're talking about the locked background layer you generally see when opening non-PSD files.  This I already know about and the actions I have take care of converting this to a layer.  What I need is the code to get the canvas size of one document and apply it to the one where the background has been converted to a layer.  I stress canvas size as image size converts all layers regardless of type.

Muppet_Mark-QAl63s
Inspiring
March 18, 2011

I too am at a bit of a loss as to what exactly you are tying to do? From what its sounds like you don't need the help of script… You can just use resize canvas to a given value about given position in a recorded action… can't you? (It has been a while since I last used actions)…