• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Photoshop Scripting Artboard Size Values

New Here ,
Oct 09, 2022 Oct 09, 2022

Copy link to clipboard

Copied

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?

TOPICS
Actions and scripting , macOS

Views

94

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Oct 09, 2022 Oct 09, 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"));
    
...

Likes

Translate

Translate
Community Expert , Oct 10, 2022 Oct 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;

///////
...

Likes

Translate

Translate
Community Expert ,
Oct 09, 2022 Oct 09, 2022

Copy link to clipboard

Copied

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#ScriptingListenerplugi...

 

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2022 Oct 09, 2022

Copy link to clipboard

Copied

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); }
    }

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 10, 2022 Oct 10, 2022

Copy link to clipboard

Copied

This works! Thanks for grabbing this.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2022 Oct 09, 2022

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2022 Oct 09, 2022

Copy link to clipboard

Copied

Yeah, I hate trying to script artboards! 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2022 Oct 09, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 10, 2022 Oct 10, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2022 Oct 10, 2022

Copy link to clipboard

Copied

LATEST

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2022 Oct 10, 2022

Copy link to clipboard

Copied

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 );
}

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines