Skip to main content
Naoki-Hada
Known Participant
July 7, 2017
Answered

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

  • July 7, 2017
  • 2 replies
  • 3128 views

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?

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(app.activeDocument.layerSets, 0);

function traverseLayer(layers, level) {
	try{
		for(var i = 0; i < layers.length; i++) {
			var l = layers[i];
			$.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

This topic has been closed for replies.
Correct answer c.pfaffenbichler

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

2 replies

Participating Frequently
April 19, 2018
schroef
Inspiring
July 7, 2021

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

 

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
July 9, 2017

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

Naoki-Hada
Known Participant
July 10, 2017

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

Kukurykus
Legend
August 28, 2021

Following is updated working code.

Thank you very much.

#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[i];
			$.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;
}

If you use background layer you may use c.pfaffenbichler for loop in your code, but if you don't, you still can use his for loop, but then in first instance you mast check if background layer exists to take into account its variable in later part of script. btw. thanks to your code I found $.writeln() content does not have to be used with pluses, but may be with comas 😉