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.

JJMack
Community Expert
Community Expert
March 18, 2011

dgolberg wrote:

  I stress canvas size as image size converts all layers regardless of type.

You may stress that!!!  However canvas size will only change the size of one layer a background layer.  A PSD file may or may not not have a Photoshop Background layer. You can not create an action that will be able to convert the background layer to a normal layer for all PSD files do not have a background layer. However you can do that with a script for it can test to see if the bottom layer is a Background layer and convert it to a normal layer.  If you download my crafting action package one of the utility scripts I wrote for use within actions that are included does exactly that.

http://www.mouseprints.net/old/dpr/JJMacksCraftingActions.zip

Contains:

Action Actions Palette Tips.txt

Action Creation Guidelines.txt

Action Dealing with Image Size.txt

Action Enhanced via Scripted Photoshop Functions.txt

CraftedActions.atn Sample Action set includes an example Watermarking action

Sample Actions.txt Photoshop CraftedActions set saved as a text file. This file has some additional comments I inserted describing how the actions work.

12 Scripts for actions

Also to do what you want to do you need to script it for you want to get the canvas size (actions can not do that as you know) of one document and change the canvas size of a second document. Actually I don't think you know what you want to do or you can not describe what you want to do well.  For you think  that Canvas Size and Image size are the same function which is not the case.

I do not think you realize that all layers in a layered document do not always have the same size that layers can have different aspect ratios and sizes and can have a  size different then the canvas be larger or smaller then that size.

JJMack