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?
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
Instead of using the activeLayer/s you can identify Layers by their indices or identifiers.
Copy link to clipboard
Copied
Instead of using the activeLayer/s you can identify Layers by their indices or identifiers.
Copy link to clipboard
Copied
c.pfaffenbichler​ Thank you very much for the pointer.
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
};
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++) { |
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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 😉
Copy link to clipboard
Copied
Do you know what this $.writeln actually is? I see it more often, most times when righting to log files orso. Feels like PHP code orso.
Copy link to clipboard
Copied
Use them to write text to JavaScript Console in Extendscript Toolkit.
Copy link to clipboard
Copied
I tried the script with/out the change and it works same. Could you share example of .psd (with artboards) the error occurs on when using c.pfaffenbichler code, but not with yours?
Copy link to clipboard
Copied
You do know many if not most scripts are broken here, i think you do know. But we need to hunt for the locations where need to add [i] back in. The updating of this forum mangled all the scripts up to that point.
Copy link to clipboard
Copied
I asked a question after I edited posts with crippled codes. Later I found out that Naoki-Hada post about error was not to earlier script but to the script he did not post yet in that time 😉
Copy link to clipboard
Copied
I just found new Array is equivalent to Array() I use 🙂
btw there's one pair of square brackets too many: [[theName, theID]]
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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);
}