Search code examples
javascriptadobephotoshopextendscriptphotoshop-script

Photoshop Script Select all visible Artboards


I've been trying to find a way to select all visible artboards, so i can later Quick Export them all as PNGs. I'd also love if i've could also include the exporting part inside the script, but that's not the main problem now.

I have this code that finds all the visibile artboards and stores them inside visibleArtboards, but I can't seem to find a way to make a selection of them.

var artboards = activeDoc.layers;
var visibleArtboards = [];
for (var i=0; i<artboards.length; i++){
    if(artboards[i].visible){
        visibleArtboards.push(artboards[i]);
    }
}

activedoc.activeLayer can make a selection, but only of 1 single artboard.

If anybody can help me with the missing piece, I'd appreciate it a lot


Solution

  • You can get the layer ID of the artboards and add that to a selection. Using your code, visibleArtboards now is the ID of the artboard layers.

    // call the source document
    var activeDoc = app.activeDocument;
    
    var artboards = activeDoc.layers;
    
    var visibleArtboards = [];
    for (var i=0; i<artboards.length; i++)
    {
        if(artboards[i].visible)
        {
    
          // select ID as we go along
          var layerID = artboards[i].id;
          visibleArtboards.push(layerID);
    
        }
    }
    
    // Loop over the layers again and select all the art boards
    for (var i=0; i<visibleArtboards.length; i++)
    {
       // add to current selection of layers
       select_by_ID(visibleArtboards[i], true);
    }
    
    
    
    
    function select_by_ID(id, add)
    {
        if (add == undefined) add = false;
        var desc1 = new ActionDescriptor();
        var ref1 = new ActionReference();
        ref1.putIdentifier(charIDToTypeID('Lyr '), id);
        desc1.putReference(charIDToTypeID('null'), ref1);
        if (add) desc1.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection"));
        executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
    } // end of selectByID()
    

    It will, however, add the background (if included). I'm sure you can get around that.