Skip navigation
plumsfriend
Currently Being Moderated

Scripting Relative Canvas Size

Jun 29, 2012 10:44 PM

Tags: #size #scripting #relative #canvas

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()

 
Replies
  • Currently Being Moderated
    Jun 30, 2012 1:09 AM   in reply to plumsfriend

    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;

    };

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points