Skip to main content
Known Participant
September 28, 2023
Question

Artboards To Files – skip dialog window

  • September 28, 2023
  • 3 replies
  • 291 views

Hello, I am creating a script which, in one of the steps, runs the standard Photoshop script "Artboards To Files". Do you know how to modify this "Artboards To Files" script so that it doesn't open its dialog window? Or after opening this dialog window, did he confirm it immediately?

 

 

This topic is closed to new replies. Start a new post to keep the conversation going.

3 replies

KHKHAuthor
Known Participant
September 29, 2023

Ok, I managed to do it with this script 🙂

 

#target photoshop
// Export each top layer group (artboards) as a PNG
var destStatics = new Folder(activeDocument.path + '/export/');
var doc = activeDocument;
if (!destStatics.exists) {
    destStatics.create();
}

(function getLayers(el) {
    // find layer groups
    for (var a = 0; a < el.layerSets.length; a++) {
        var lname = el.layerSets[a].name;
        var exportName = '';
        if (lname != 'Null' && lname != 'NULL' && lname != 'null') {
            var ext = lname.substr(-4);
            if (ext === '.png') {
                exportName = lname;
            } else if (ext === '.jpg') {
                exportName = lname.replace('.jpg', '.png');
            } else {
                exportName = lname + '.png';
            }
            saveLayer(el.layers.getByName(lname), exportName, destStatics, true);
        }
    }
})(doc);

function saveLayer(layer, lname, path, shouldMerge) {
    if (layer.visible && !layer.allLocked) {
        activeDocument.activeLayer = layer;
        dupLayers();
        if (shouldMerge === undefined || shouldMerge === true) {
            activeDocument.mergeVisibleLayers();
        }

        // Set the active layer to the one below the current layer
        var belowLayer = activeDocument.activeLayer;
        
        // Calculate the center of this layer
        var belowLayerBounds = belowLayer.bounds;
        var centerX = (belowLayerBounds[0].value + belowLayerBounds[2].value) / 2;
        var centerY = (belowLayerBounds[1].value + belowLayerBounds[3].value) / 2;

        // Create a "rectangle" with dimensions of 2790x2460px and align it to the center of the layer below
        var rectWidth = 2790;
        var rectHeight = 2460;
        var startX = centerX - (rectWidth / 2);
        var startY = centerY - (rectHeight / 2);
        var endX = startX + rectWidth;
        var endY = startY + rectHeight;
        var rectangleShape = [[startX, startY], [endX, startY], [endX, endY], [startX, endY]];
        var rectangleLayer = activeDocument.artLayers.add();
        rectangleLayer.name = "rectangleTemp";
        activeDocument.selection.select(rectangleShape);
        activeDocument.selection.fill(app.foregroundColor);
        activeDocument.selection.deselect();

        // Run the Trim function
        activeDocument.trim(TrimType.TRANSPARENT, true, true, true, true);

        // Delete the "rectangleTemp" layer
        rectangleLayer.remove();

        var saveFile = File(path + "/" + lname);
        SavePNG(saveFile);
        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    }
}

function dupLayers() {
    var desc143 = new ActionDescriptor();
    var ref73 = new ActionReference();
    ref73.putClass(charIDToTypeID('Dcmn'));
    desc143.putReference(charIDToTypeID('null'), ref73);
    desc143.putString(charIDToTypeID('Nm  '), activeDocument.activeLayer.name);
    var ref74 = new ActionReference();
    ref74.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
    desc143.putReference(charIDToTypeID('Usng'), ref74);
    executeAction(charIDToTypeID('Mk  '), desc143, DialogModes.NO);
};

function SavePNG(saveFile) {
    var pngOpts = new PNGSaveOptions();
    pngOpts.compression = 9; // Range from 0 (no compression) to 9
    pngOpts.interlaced = false;
    activeDocument.saveAs(saveFile, pngOpts, true, Extension.LOWERCASE);
}
c.pfaffenbichler
Community Expert
Community Expert
September 29, 2023

Please specify the actually intended process (the settings, the target file format, the target location, …). 

Stephen Marsh
Community Expert
Community Expert
September 28, 2023

The Adobe default scripts are long and complex. The dialog GUI is there to set options, which you would need to remove and hard code into the script.

 

It's probably going to be easier to adapt or create different code to save artboards to files.