Skip to main content
Inspiring
February 7, 2020
Question

Resize Canvas Script

  • February 7, 2020
  • 3 replies
  • 5149 views

I an using a script to increase the canvas size by 211 pixels on the right

The function is called from a button click.

 

Button Click

$("#btn_Canvas_Size").click(function () {
csInterface.evalScript('Canvas_Bigger()');
});

 

Function Called from Button

function Canvas_Bigger() {
var srcDoc = app.activeDocument;

// get original width and height
var w = srcDoc.width.value;
var h = srcDoc.height.value;

// Increase canvas size + 211 pixels to the right
// with the anchor in the top left
srcDoc.resizeCanvas(w +211, h, AnchorPosition.TOPLEFT);
}

 

Question:

I am trying to figure out how to prevent the function from firing on multiple clicks of the button.

I only want the canvas to be resized the once.

 

Any suggestions please

Ian

 

This topic has been closed for replies.

3 replies

pixxxelschubser
Community Expert
Community Expert
February 9, 2020

Hi @ian-barber,

maybe a bit awkward, but how about with something like that (depends on your layer structure and workflow)?

Canvas_Bigger ();
function Canvas_Bigger() {
var srcDoc = app.activeDocument;
var nme = srcDoc.layers[srcDoc.layers.length-1].name;
if (nme != "Big Canvas") {

    // get original width and height
    var w = srcDoc.width.value;
    var h = srcDoc.height.value;

    // Increase canvas size + 211 pixels to the right
    // with the anchor in the top left
    srcDoc.resizeCanvas(w /*+211*/, h, AnchorPosition.TOPLEFT);
    srcDoc.layers[srcDoc.layers.length-1].name = "Big Canvas";
    //alert ("Canvas was changed");
    } else {
    //alert ("Script was already used");
    }
}

 

Rename (for example) your background layer in the first run and check for the name.

Legend
February 8, 2020
Paste this code in function code

 

this.enabled = false;

 

 

EDIT:
Did not notice.
The interface is made not by a script but by an extension.
I can’t say anything, because basically I do not use extensions and they are all off.
 
JJMack
Community Expert
Community Expert
February 7, 2020

If an Action or script is run or played with a click of a button a or shortcut. If Photoshop see you used the button or shortcut it will run or play the automation. You would need to be control to when these event happen to throw away these event should they occur. But as soon as you loose control or give up control If photoshop get control and see  an event the automation  will be started.  You must to control you fingers better.

JJMack