Skip to main content
Todd_Morgan
Legend
September 14, 2023
Open for Voting

SCALE ARTBOARD AND CONTENT

  • September 14, 2023
  • 24 replies
  • 3068 views

Please get this into the next update.... or is there a script out there available??

24 replies

Participating Frequently
October 3, 2023

thanks for that, i tried using it and set the free transform to scle 30% and it did scale all the layers but it didnt scale the artboard, it actully made it larger than before, since the backgroung layer it much larger than the artboard.

Todd_Morgan
Legend
September 20, 2023

Thanks... will have a look.

Stephen Marsh
Community Expert
Community Expert
September 20, 2023

@Todd_Morgan 

 

Do you have any feedback on the script?

Stephen Marsh
Community Expert
Community Expert
September 16, 2023

There are multiple ways to script this, however, this is what first came to me. I have not performed exhaustive testing, use at your own risk.

 

Refer to the notes and changelog at the head of the script for more info.

 

/*
Resize Artboard and Content.jsx
v1.1 - 17th September 2023, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-ideas/scale-artboard-and-content/idi-p/14084466

Notes: 
* The artboard and content will both be resized
* The script doesn't account for layer content extending outside of the artboard bounds
* Visible layer styles such as strokes will be hidden before resizing in order to avoid unexpected sizing results!
* Layer styles are not transformed with the layer content
* The free transform control handles may not be visible when initially resizing the layer content, you can zoom in/out to correct this display glitch
      
Changelog:
v1.0 - 16th September 2023 - Inital release: Artboard was resized to fit content, ignoring transparent artboard pixels
v1.1 - 17th September 2023 - Update: Artboard and content resized honouring transparent artboard pixels
*/

#target photoshop

function main() {
    // Check if the active layer is an artboard (not layer group/set or frame)
    if (isArtboard() === true) {
        // Set the original artboard variable
        var origArtboard = activeDocument.activeLayer;
        // Get the original artboard name
        var origArtboardName = activeDocument.activeLayer.name;
        // Add a temp layer for the original artboard bounds
        artboardBoundsTempLayer();
        // Reselect the artboard
        if (activeDocument.activeLayer.parent !== activeDocument) activeDocument.activeLayer = activeDocument.activeLayer.parent;
        // Select the artboard contents
        selectActiveSetContents();
        // Hide the layer styles
        layerStyleVisibility(false);
        // Resize the artboard content
        alert("Free transform to set the new content and artboard size...");
        app.runMenuItem(stringIDToTypeID("freeTransform"));
        // Add a new artboard using the original artboard content
        app.runMenuItem(stringIDToTypeID("artboardFromLayersEvent"));
        // Delete the artbord bounds temp layer
        removeArtboardBoundsTempLayer();
        // Delete the original (now empty) artboard
        origArtboard.remove();
        // Reselect the artboard
        if (activeDocument.activeLayer.parent !== activeDocument) activeDocument.activeLayer = activeDocument.activeLayer.parent;
        // Rename the new artboard to the original artboard name
        activeDocument.activeLayer.name = origArtboardName;
        // Select the artboard contents
        selectActiveSetContents();
        // Show the layer styles
        layerStyleVisibility(true);
        // Reselect the artboard
        if (activeDocument.activeLayer.parent !== activeDocument) activeDocument.activeLayer = activeDocument.activeLayer.parent;
    } else {
        alert("Select an artboard to resize the content and artboard...");
    }
}

activeDocument.suspendHistory("Resize Artboard and Content.jsx", "main()");


///// FUNCTIONS /////

function isArtboard() {
    // modified from a script by greless with hints from jazz-y!
    // returns true or false
    try {
        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putEnumerated(stringIDToTypeID('layer'), stringIDToTypeID('ordinal'), stringIDToTypeID('targetEnum'));
        var options = executeActionGet(r);
        return options.hasKey(stringIDToTypeID('artboard')); // test for the required key
    } catch (e) {
        //alert(e);
    }
}

function selectActiveSetContents() {
    /* https://gist.github.com/joonaspaakko/1add5c6a905216740c9c922a657636e1 */
    var doc = app.activeDocument;
    var sourceGroup = app.activeDocument.activeLayer;

    // Make a temp group with a temp layer inside it:
    // [Parent group]
    // - [TEMP GROUP]
    // - - [TEMP LAYER]

    var tempGroup = sourceGroup.layerSets.add();
    tempGroup.name = 'TEMP GROUP';
    var tempLayer = tempGroup.artLayers.add();
    tempLayer.name = 'TEMP LAYER';
    // Loop through all direct child element and move the last layer below 'TEMP LAYER'
    var children_length = sourceGroup.layers.length - 1;
    for (var i = 0; i < children_length; i++) {
        var lastLayer = sourceGroup.layers[sourceGroup.layers.length - 1];
        lastLayer.move(tempGroup.layers[0], ElementPlacement.PLACEAFTER);
    }
    // Get rid of TEMP LAYER
    tempLayer.remove();
    // Get rid of TEMP GROUP by ungrouping it, which also happens to select its children.
    doc.activeLayer = tempGroup;
    // Ungroup tempGroup
    var idungroupLayersEvent = stringIDToTypeID("ungroupLayersEvent");
    var desc702 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    var ref427 = new ActionReference();
    var idLyr = charIDToTypeID("Lyr ");
    var idOrdn = charIDToTypeID("Ordn");
    var idTrgt = charIDToTypeID("Trgt");
    ref427.putEnumerated(idLyr, idOrdn, idTrgt);
    desc702.putReference(idnull, ref427);
    executeAction(idungroupLayersEvent, desc702, DialogModes.NO);
}

function layerStyleVisibility(layerFXVisible) {
	function s2t(s) {
        return app.stringIDToTypeID(s);
    }
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putProperty( s2t( "property" ), s2t( "layerFXVisible" ));
	reference.putEnumerated( s2t( "document" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	descriptor.putReference( s2t( "null" ), reference );
	descriptor2.putBoolean( s2t( "layerFXVisible" ), layerFXVisible );
	descriptor.putObject( s2t( "to" ), s2t( "layerFXVisible" ), descriptor2 );
	executeAction( s2t( "set" ), descriptor, DialogModes.NO );
}

function artboardBoundsTempLayer() {
    addTempLayer("_Temp_AB-Bounds", 0);
    function addTempLayer(theName, opacity, layerID) {
        function s2t(s) {
            return app.stringIDToTypeID(s);
        }
        var descriptor = new ActionDescriptor();
        var descriptor2 = new ActionDescriptor();
        var reference = new ActionReference();
        reference.putClass(s2t("layer"));
        descriptor.putReference(s2t("null"), reference);
        descriptor2.putString(s2t("name"), theName);
        descriptor2.putUnitDouble(s2t("opacity"), s2t("percentUnit"), opacity);
        descriptor.putObject(s2t("using"), s2t("layer"), descriptor2);
        executeAction(s2t("make"), descriptor, DialogModes.NO);
    }
    fillArtLayer(100);
    function fillArtLayer(opacity) {
        function s2t(s) {
            return app.stringIDToTypeID(s);
        }
        var descriptor = new ActionDescriptor();
        descriptor.putEnumerated(s2t("using"), s2t("fillContents"), s2t("foregroundColor"));
        descriptor.putUnitDouble(s2t("opacity"), s2t("percentUnit"), opacity);
        descriptor.putEnumerated(s2t("mode"), s2t("blendMode"), s2t("normal"));
        executeAction(s2t("fill"), descriptor, DialogModes.NO);
    }
}

function removeArtboardBoundsTempLayer() {
	function s2t(s) {
        return app.stringIDToTypeID(s);
    }
	var descriptor = new ActionDescriptor();
	var list = new ActionList();
	var reference = new ActionReference();
	reference.putName( s2t( "layer" ), "_Temp_AB-Bounds" );
	descriptor.putReference( s2t( "null" ), reference );
	descriptor.putBoolean( s2t( "makeVisible" ), false );
	executeAction(s2t( "select" ), descriptor, DialogModes.NO);
	activeDocument.activeLayer.remove();
}

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html