Skip to main content
June 30, 2012
Answered

Scripting Relative Canvas Size

  • June 30, 2012
  • 2 replies
  • 3235 views

I'm just getting the hang of Photoshop scripting, and have looked everywhere, but I can't find any way of changing a canvas size relative to the current canvas using scripting.

My current script is below..it sets a selection to the active layer and crops/ flattens the image. Now I want to add a ten pixel border relative to the size of the canvas. Does anyone have any idea how this would be accomplished? I really don't want to have to create an action just to run the canvas resize. Any help would be greatly appreciated!!

layerPixels2Selection = function(){

if(app.activeDocument.activeLayer.isBackgroundLayer){

   return;// background doesn't have a transparency mask

   }

var desc = new ActionDescriptor();

var ref = new ActionReference();

ref.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );

desc.putReference( charIDToTypeID( "null" ), ref );

var ref1 = new ActionReference();

ref1.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Trsp" ) );

desc.putReference( charIDToTypeID( "T   " ), ref1 );

executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );

}

//demo

layerPixels2Selection()

app.activeDocument.crop(app.activeDocument.selection.bounds);

app.activeDocument.mergeVisibleLayers()

This topic has been closed for replies.
Correct answer c.pfaffenbichler

You could try this:

// 2012, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

var originalRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

myDocument.resizeCanvas(myDocument.width + 20, myDocument.height + 20, AnchorPosition.MIDDLECENTER)

app.preferences.rulerUnits = originalRulerUnits;

};

2 replies

Participant
September 3, 2018

thank you so much ..!

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
June 30, 2012

You could try this:

// 2012, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

var originalRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

myDocument.resizeCanvas(myDocument.width + 20, myDocument.height + 20, AnchorPosition.MIDDLECENTER)

app.preferences.rulerUnits = originalRulerUnits;

};

June 30, 2012

WOW!! Thanks so much-- it works perfectly!