Skip to main content
Participant
October 9, 2022
Answered

Photoshop Scripting Artboard Size Values

  • October 9, 2022
  • 5 replies
  • 1308 views

Is it possible to get the width and height values of an artboard's size with Photoshop scripting? And is it possible to change the size of an artboard with scripting?

This topic has been closed for replies.
Correct answer Stephen Marsh

ScriptingListener changing artboard size via Properties panel resizes from the fixed upper left... Resizing using the artboard tool (v) is "interactive"...

 

Combining the code from @Chuck Uebele with some further code:

 

#target photoshop 

var doc = activeDocument;
var aLayer = doc.activeLayer;
var ab = artboard_rectangle(aLayer);
var aW = ab[2]-ab[0];
var aH = ab[3] - ab[1];

//////////

// Add 100px to current artboard width & height
var aWresize = aW + 100;
var aHresize = aH + 100;

//////////

resizeArtboard(0, 0, aHresize, aWresize, "", 255, 255, 255, 1);

//////////

function artboard_rectangle(layer)
    {
    try {        
        var r    = new ActionReference();    
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("artboard"));
        if (layer) r.putIdentifier(stringIDToTypeID("layer"), layer.id);
        else       r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        var d = executeActionGet(r).getObjectValue(stringIDToTypeID("artboard")).getObjectValue(stringIDToTypeID("artboardRect"));
        var bounds = new Array();
        bounds[0] = d.getUnitDoubleValue(stringIDToTypeID("left"));
        bounds[1] = d.getUnitDoubleValue(stringIDToTypeID("top")); 
        bounds[2] = d.getUnitDoubleValue(stringIDToTypeID("right"));
        bounds[3] = d.getUnitDoubleValue(stringIDToTypeID("bottom"));                                

        return bounds;
        }
    catch(e) { alert(e); }
}

//////////

function resizeArtboard(top, left, bottom, right, artboardPresetName, red, grain, blue, artboardBackgroundType) {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	var descriptor3 = new ActionDescriptor();
	var descriptor4 = new ActionDescriptor();
	var list = new ActionList();
	var reference = new ActionReference();
	reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	descriptor.putReference( s2t( "null" ), reference );
	descriptor3.putDouble( s2t( "top" ), top );
	descriptor3.putDouble( s2t( "left" ), left );
	descriptor3.putDouble( s2t( "bottom" ), bottom );
	descriptor3.putDouble( s2t( "right" ), right );
	descriptor2.putObject( s2t( "artboardRect" ), s2t( "classFloatRect" ), descriptor3 );
	descriptor2.putList( s2t( "guideIDs" ), list );
	descriptor2.putString( s2t( "artboardPresetName" ), artboardPresetName );
	descriptor4.putDouble( s2t( "red" ), red );
	descriptor4.putDouble( s2t( "grain" ), grain );
	descriptor4.putDouble( s2t( "blue" ), blue );
	descriptor2.putObject( s2t( "color" ), s2t( "RGBColor" ), descriptor4 );
	descriptor2.putInteger( s2t( "artboardBackgroundType" ), artboardBackgroundType );
	descriptor.putObject( s2t( "artboard" ), s2t( "artboard" ), descriptor2 );
	executeAction( s2t( "editArtboardEvent" ), descriptor, DialogModes.NO );
}

 

5 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
October 10, 2022

ScriptingListener changing artboard size via Properties panel resizes from the fixed upper left... Resizing using the artboard tool (v) is "interactive"...

 

Combining the code from @Chuck Uebele with some further code:

 

#target photoshop 

var doc = activeDocument;
var aLayer = doc.activeLayer;
var ab = artboard_rectangle(aLayer);
var aW = ab[2]-ab[0];
var aH = ab[3] - ab[1];

//////////

// Add 100px to current artboard width & height
var aWresize = aW + 100;
var aHresize = aH + 100;

//////////

resizeArtboard(0, 0, aHresize, aWresize, "", 255, 255, 255, 1);

//////////

function artboard_rectangle(layer)
    {
    try {        
        var r    = new ActionReference();    
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("artboard"));
        if (layer) r.putIdentifier(stringIDToTypeID("layer"), layer.id);
        else       r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        var d = executeActionGet(r).getObjectValue(stringIDToTypeID("artboard")).getObjectValue(stringIDToTypeID("artboardRect"));
        var bounds = new Array();
        bounds[0] = d.getUnitDoubleValue(stringIDToTypeID("left"));
        bounds[1] = d.getUnitDoubleValue(stringIDToTypeID("top")); 
        bounds[2] = d.getUnitDoubleValue(stringIDToTypeID("right"));
        bounds[3] = d.getUnitDoubleValue(stringIDToTypeID("bottom"));                                

        return bounds;
        }
    catch(e) { alert(e); }
}

//////////

function resizeArtboard(top, left, bottom, right, artboardPresetName, red, grain, blue, artboardBackgroundType) {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	var descriptor3 = new ActionDescriptor();
	var descriptor4 = new ActionDescriptor();
	var list = new ActionList();
	var reference = new ActionReference();
	reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	descriptor.putReference( s2t( "null" ), reference );
	descriptor3.putDouble( s2t( "top" ), top );
	descriptor3.putDouble( s2t( "left" ), left );
	descriptor3.putDouble( s2t( "bottom" ), bottom );
	descriptor3.putDouble( s2t( "right" ), right );
	descriptor2.putObject( s2t( "artboardRect" ), s2t( "classFloatRect" ), descriptor3 );
	descriptor2.putList( s2t( "guideIDs" ), list );
	descriptor2.putString( s2t( "artboardPresetName" ), artboardPresetName );
	descriptor4.putDouble( s2t( "red" ), red );
	descriptor4.putDouble( s2t( "grain" ), grain );
	descriptor4.putDouble( s2t( "blue" ), blue );
	descriptor2.putObject( s2t( "color" ), s2t( "RGBColor" ), descriptor4 );
	descriptor2.putInteger( s2t( "artboardBackgroundType" ), artboardBackgroundType );
	descriptor.putObject( s2t( "artboard" ), s2t( "artboard" ), descriptor2 );
	executeAction( s2t( "editArtboardEvent" ), descriptor, DialogModes.NO );
}

 

Stephen Marsh
Community Expert
Community Expert
October 10, 2022

@JustinCarroll - Do you have an anchor point that you wish to change the artboard around? Middle centre, upper left etc?

Participant
October 10, 2022

No. Wondering if I could change the Artboard size was curiousity. Thanks!

Stephen Marsh
Community Expert
Community Expert
October 11, 2022

@JustinCarroll - Both of your questions have been answered in the affirmative by @Chuck Uebele and myself with working code examples.

Chuck Uebele
Community Expert
Community Expert
October 10, 2022

Just tried to change the size by recording the code with ScriptListener. I got the code, but changing the values does odd things to the size. I'm not sure what's going on.

Stephen Marsh
Community Expert
Community Expert
October 10, 2022

Yeah, I hate trying to script artboards! 

Chuck Uebele
Community Expert
Community Expert
October 10, 2022

Here is code to get the artboard's dimentions. You have to have the artboard selected.

#target photoshop 
var doc = activeDocument;
var aLayer = doc.activeLayer
    ab = artboard_rectangle(aLayer);
    aW = ab[2]-ab[0];
    aH = ab[3]-ab[1];
alert('artboard width = ' + aW +'--'+ 'artboard height = ' + aH)

function artboard_rectangle(layer)
    {
    try {        
        var r    = new ActionReference();    
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("artboard"));
        if (layer) r.putIdentifier(stringIDToTypeID("layer"), layer.id);
        else       r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        var d = executeActionGet(r).getObjectValue(stringIDToTypeID("artboard")).getObjectValue(stringIDToTypeID("artboardRect"));
        var bounds = new Array();
        bounds[0] = d.getUnitDoubleValue(stringIDToTypeID("left"));
        bounds[1] = d.getUnitDoubleValue(stringIDToTypeID("top")); 
        bounds[2] = d.getUnitDoubleValue(stringIDToTypeID("right"));
        bounds[3] = d.getUnitDoubleValue(stringIDToTypeID("bottom"));                                

        return bounds;
        }
    catch(e) { alert(e); }
    }
Participant
October 10, 2022

This works! Thanks for grabbing this.

Stephen Marsh
Community Expert
Community Expert
October 9, 2022

As this is outside of the standard DOM, I believe that you would need to install and use the ScriptingListener plug-in:

 

https://helpx.adobe.com/au/photoshop/kb/downloadable-plugins-and-content.html#ScriptingListenerplugin

 

If you search around the forum I'm sure that you will find code to help in your project.