Skip to main content
Naoki-Hada
Known Participant
July 7, 2017
解決済み

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

  • July 7, 2017
  • 返信数 2.
  • 3128 ビュー

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

このトピックへの返信は締め切られました。
解決に役立った回答 c.pfaffenbichler

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

返信数 2

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
Community Expert
July 9, 2017

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

Naoki-Hada
Naoki-Hada作成者
Known Participant
July 10, 2017

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

c.pfaffenbichler
Community Expert
Community Expert
July 9, 2017
// 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
};