[Q] Is there way to check if the LayerSet is ArtBoard without selecting?

Contributor ,
Jul 07, 2017 Jul 07, 2017

Copy link to clipboard

Copied

Hi all,

 

Is there way to check if the LayerSet is ArtBoard without selecting?

I mean not make active the LayerSet.

 

My understanding following code returns true if selected LayerSet is Artboard.

Is this only way to get info?

 

isArtboard()

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

var isArtboardLayer = executeActionGet(ref).getBoolean(stringIDToTypeID("artboardEnabled"));

 

 

$.writeln(isArtboardLayer);

 

When I try to get the info in following code, above code does not work

 

traverseLayer()

traverseLayer(app.activeDocument.layerSets, 0);

 

 

 

function traverseLayer(layers, level){

    try{

        for (var i = 0; i < layers.length; i++) {

            var l = layers;

            $.writeln(level + ": (" + l.typename + ")", l.name);

            if( l.typename == 'LayerSet') {

               

                // TODO: Check if it is ArtBoard

               

                traverseLayer(l.layers, level+1); // recursive

            }

        }

    }catch(e){

        $.writeln("EXCEPTION: ", e);

    }

}

 

I saw some code is selectForwardLayer() . Retrieve Artboard reference of active nested layer

But I don't want to change selected layer by some reasons.

 

Is there any way to check if it is ArtBoard LayerSet without selecting?

 

Thank you,

Naoki

TOPICS
Actions and scripting

Views

911

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
Adobe Community Professional ,
Jul 09, 2017 Jul 09, 2017

Copy link to clipboard

Copied

Instead of using the activeLayer/s you can identify Layers by their indices or identifiers. 

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
Contributor ,
Jul 10, 2017 Jul 10, 2017

Copy link to clipboard

Copied

c.pfaffenbichler​ Thank you very much for the pointer.

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
Adobe Community Professional ,
Jul 09, 2017 Jul 09, 2017

Copy link to clipboard

Copied

// get list of artboards;

// 2017, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

var aaa = getArtboards ();

alert (aaa.join("\n"))

};

////////////////////////////////////

function getArtboards () {

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var applicationDesc = executeActionGet(ref);

var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));

// anumber is intended to keep track of layerset depth;

var aNumber = 0;

var theArray = new Array;

var theGroups = new Array;

var theContents = new Array;

////// work through layers //////

for (var m = 0; m < theNumber+1; m++) {

//for (var m = theNumber; m >= 0; m--) {

try {

var ref = new ActionReference();

ref.putIndex( charIDToTypeID( "Lyr " ), m);

var layerDesc = executeActionGet(ref);

var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));

var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));

var theName = layerDesc.getString(stringIDToTypeID('name'));

var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));

var theBounds = layerDesc.getObjectValue(stringIDToTypeID('bounds'));

var theX = theBounds.getInteger(stringIDToTypeID('left'));

var theY = theBounds.getInteger(stringIDToTypeID('top'));

var theX2 = theBounds.getInteger(stringIDToTypeID('right'));

var theY2 = theBounds.getInteger(stringIDToTypeID('bottom'));

var artBoard = layerDesc.getBoolean(stringIDToTypeID("artboardEnabled"));

$.writeln(artBoard)

var thisArray = [[theName, theID]];

////////////////////////////////////

if (artBoard == true) {theArray.push(thisArray)};

////////////////////////////////////

}

catch (e) {};

};

// the results;

return theArray

};

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
Contributor ,
Jul 10, 2017 Jul 10, 2017

Copy link to clipboard

Copied

c.pfaffenbichler​ Thank you very much for the code.

Line #22 need to be modified to avoid error, but it works great.

Line #21

    for (var m = 1; m <= theNumber; m++) { // to be 1 based

//    for (var m = 0; m < theNumber+1; m++) {

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
Contributor ,
Jul 10, 2017 Jul 10, 2017

Copy link to clipboard

Copied

Following is updated working code.

Thank you very much.

working code snippet

#target photoshop

var abList = listArtboard();

traverseLayer(app.activeDocument.layers, 0);

function traverseLayer(layers, level){

    try{

        for (var i = 0; i < layers.length; i++) {

            var l = layers;

            $.writeln(level + ": (" + l.typename + ")", l.name);

            if( l.typename == 'LayerSet') {

                if(isArtboard(l.id)){

                    $.writeln("Artboard!");

                }

                traverseLayer(l.layers, level+1); // recursive

            }

        }

    }catch(e){

        $.writeln("EXCEPTION: ", e);

    }

}

function isArtboard(id){

    var result =false;

    try{

        for(var i=0; i < abList.length; i++){

            if(abList[0] == id){ // found

                result = true;

                break;

            }

        }

    }catch(e){

        $.writeln("EXCEPTION: ", e);

    }

    //$.writeln(result);

    return result;

}

function listArtboard(index){

    $.hiresTimer; // reset

    var result = [];

    try{

        var ar = new ActionReference();

        ar.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

        var appDesc = executeActionGet(ar);

        var numOfLayers = appDesc.getInteger(stringIDToTypeID("numberOfLayers"));

        for(var i=1; i <= numOfLayers; i++){

            var ar = new ActionReference();

            ar.putIndex( charIDToTypeID( "Lyr " ), i); // 1-base

            var dsc = executeActionGet(ar);

            var id = dsc.getInteger(stringIDToTypeID('layerID'));

            var name = dsc.getString(stringIDToTypeID('name'));

            var isAb = dsc.getBoolean(stringIDToTypeID("artboardEnabled"));

            if(isAb){

                result.push([id, name]);

            }

        }

    }catch(e){

        $.writeln("EXCEPTION: ", e);

    }

    $.writeln("listArtboard done: time = ", $.hiresTimer, " \xB5s"); // micro second

    $.writeln("result = ", result);

   

    return result;

}

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 Beginner ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

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
Enthusiast ,
Jul 06, 2021 Jul 06, 2021

Copy link to clipboard

Copied

LATEST

is this a riddle, why not add description what your linking.

I add here a translated version of @songgebs46585277  addition. Though the method used can be done better. saving a file then reopening it and then cropping makes the script slow. Better is making a duplicate of the current document, then crop to the active artboard and export this. This is faster.

 

An example of this is export layer comps file which comps with photoshop. I use this same approach in a script i got for export layer comps and artboards in a script of mine. Normally its either one or the other, you cant export layer comps from a file which has artboards. 

 

You function will not do anything since the last function is never called??

// Author: WangSongSong
// Source: https://github.com/songgeb/ExtendScript-Helper/blob/master/artboard.jsx

// Since the current (April 19, 2018) official api does not support the drawing board attribute query, 
// write a method to determine whether the layer is a drawing board.
function isArtboard (layer) {
    var itemIndex = layer.itemIndex;
    try {
        if (app.activeDocument.backgroundLayer) {
            itemIndex --;
        }
    } catch (e) {
    }

    var ref = new ActionReference();
    ref.putIndex(stringIDToTypeID("layer"), itemIndex);
    var desp = executeActionGet(ref);
    try {
        return desp.getBoolean(stringIDToTypeID("artboardEnabled"));
    } catch (e) {
        return false;
    }
}

// Get the size of the artboard and the coordinates of the origin from the upper left corner of the entire canvas
function getArtboardBounds (artboardLayer) {
    var originalRuler = app.preferences.rulerUnits;
    
    app.preferences.rulerUnits = Units.PIXELS;
    var itemIndex = artboardLayer.itemIndex;
    try {
        if (app.activeDocument.backgroundLayer) {
            itemIndex --;
        }
    } catch (e) {
    }
    var ref = new ActionReference();
    ref.putIndex(stringIDToTypeID("layer"), itemIndex);
    var desp = executeActionGet(ref);
    var theBounds = desp.getObjectValue(stringIDToTypeID('bounds'));
    var theX = theBounds.getInteger(stringIDToTypeID('left'));
    var theY = theBounds.getInteger(stringIDToTypeID('top'));
    var theX2 = theBounds.getInteger(stringIDToTypeID('right'));
    var theY2 = theBounds.getInteger(stringIDToTypeID('bottom'));
    
    app.preferences.rulerUnits = originalRuler;
    return [UnitValue(theX, "px"), UnitValue(theY, "px"), UnitValue(theX2, "px"), UnitValue(theY2, "px")];
}

// Export an artboard separately, similar to the function of "File -> Export -> Artboard to File" in ps
function exportArtboardAsPNG(artboard, exportDirPath) {
    // Create a new document and copy the artboard layer group to guoqu 
    var currentDoc = app.activeDocument;
    var newDoc = app.documents.add (currentDoc.width, currentDoc.height, currentDoc.resolution, null, NewDocumentMode.RGB, DocumentFill.TRANSPARENT);
    app.activeDocument = currentDoc;
    var newArtboard = artboard.duplicate (newDoc);
    app.activeDocument = newDoc;
    // Record the size of the artboard
    var bounds = getArtboardBounds(newArtboard);
    var tmpFilePath = exportDirPath + "/tmp.png";
    // Export a temporary picture that only contains the artboard, but the size is still the original size
    saveDocumentAsPNG(tmpFilePath);
    newDoc.close(SaveOptions.DONOTSAVECHANGES);
    // ps opens the temporary picture, crops the artboard area according to the size of the artboard, and exports the cropped part
    var tmpFile = new File(tmpFilePath);
    newDoc = app.open (tmpFile);
    newDoc.crop(bounds);
    saveDocumentAsPNG(exportDirPath+"/result.png");
    tmpFile.remove();
    newDoc.close(SaveOptions.DONOTSAVECHANGES);
}

 

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