Skip to main content
Known Participant
August 2, 2022
Answered

Script for Export the artboard based on the Layer color

  • August 2, 2022
  • 2 replies
  • 734 views

Hi,

My task is to export the artboard as jpeg based on the Layer color through JavaScript. The task is only export the artboard which is blue in color (hp_airpods_tile and hp_desktop_tile) and other artboards which are in "Red" and "Gray" should not export.


But I am unable to find the layer based on its color. Can anyone help me to resolve this issue?
Please see the below code, which I used for this task.

var doc = activeDocument;
var allLayers = doc.layers;

for (var a = 0; a <allLayers.length; a++) {
doc.activeLayer = allLayers[a];
if (isArtBoard()) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), a);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if not layer group collect values;
 
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart"*/ && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var visible = layerDesc.getBoolean(stringIDToTypeID("visible"));
var theColor = layerDesc.getEnumerationValue(stringIDToTypeID("color"));
if (typeIDToStringID(theColor) != "Red" || typeIDToStringID(theColor) != "Gray") {
theLayers.push([theName, theID])
}
else {
theOthers.push([theName, theID])
}
};
}
catch (e) {};
}
}

//Artboard Validation Function
//============================
function isArtBoard() {
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
return executeActionGet(ref).getBoolean(stringIDToTypeID("artboardEnabled"));
}

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

Hi @c.pfaffenbichler ,
Sorry about that, sure, in future I will mentioned as much as I can.
I here with attached sample file for testing..

Thanks


And to save jpgs try this function instead of SavePNG. 

////// function to jpg //////
function saveJPG (myDocument, docPath, basename, theSuffix) {
// weboptions;
    var webOptions = new ExportOptionsSaveForWeb();
    webOptions.format = SaveDocumentType.JPEG;
    webOptions.includeProfile = false;
    webOptions.interlaced = 0;
    webOptions.optimized = true;
    webOptions.quality = 80;
    myDocument.exportDocument(new File(docPath+"/"+basename+theSuffix+".jpg"), ExportType.SAVEFORWEB, webOptions);
    };

 

2 replies

Stephen Marsh
Community Expert
Community Expert
August 2, 2022

It's not clear to me, would both blue artboards be saved as a single JPG image combined - or two separate images?

AsuvathAuthor
Known Participant
August 8, 2022

Hi @Stephen Marsh ,
It should be export two separate images (JPEG).
Thanks

c.pfaffenbichler
Community Expert
Community Expert
August 8, 2022

So adapt the Script I posted accordingly. 

c.pfaffenbichler
Community Expert
Community Expert
August 2, 2022
// create pngs for artboards with green color;
// 2022, ue it at your own risk;
doStuff();
function doStuff () {
if (documents.length == 0) {return};
var myDocument = app.activeDocument;
var docName = myDocument.name;  
try {var basename = docName.match(/(.*)\.[^\.]+$/)[1]} 
catch (e) {var basename = docName};
try {var docPath = myDocument.path}
catch (e) {var docPath = "~/Desktop"};
var theArtBoards = collectArtBoards ();
// process artboards;
for (var m = 0; m < theArtBoards.length; m++) {
    var thisOne = theArtBoards[m];
    if (thisOne[3] == "grain") {
        selectLayerByID(thisOne[2], false);
        hideOthers ();
        savePNG (myDocument, docPath, basename, "_"+thisOne[0]);
        hideOthers ();
    }
};
};
////// collect layers with certain name //////
function collectArtBoards () {
// get number of layers;
    var ref = new ActionReference();
    ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
    ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
    var applicationDesc = executeActionGet(ref);
    var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
    var theLayers = new Array;
    for (var m = 0; m <= theNumber; m++) {
    try {
    var ref = new ActionReference();
    ref.putIndex( charIDToTypeID( "Lyr " ), m);
    var layerDesc = executeActionGet(ref);
    var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
    if (layerSet == "layerSectionStart") {
    var artBoardRect = layerDesc.getObjectValue(stringIDToTypeID("artboard")).getObjectValue(stringIDToTypeID("artboardRect"));
    var v01 = artBoardRect.getDouble(artBoardRect.getKey(0));
    var v02 = artBoardRect.getDouble(artBoardRect.getKey(1));
    var v03 = artBoardRect.getDouble(artBoardRect.getKey(2));
    var v04 = artBoardRect.getDouble(artBoardRect.getKey(3));
    var theName = layerDesc.getString(stringIDToTypeID('name'));
    var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
    var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
    var theColor = layerDesc.getEnumerationValue(stringIDToTypeID("color"));
// collect if the rect values are not identical:
    if (v01 != v03 && v01 != v04) {theLayers.push([theName, theIndex, theID, typeIDToStringID(theColor)])}
    };
    }
    catch (e) {};
    };
    return theLayers
    };
////// based on code by mike hale and paul riggott //////
function selectLayerByID(index,add){ 
    add = undefined ? add = false:add 
    var ref = new ActionReference();
        ref.putIdentifier(charIDToTypeID("Lyr "), index);
        var desc = new ActionDescriptor();
        desc.putReference(charIDToTypeID("null"), ref );
           if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
          desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
       try{
        executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
    }catch(e){
    alert(e.message); 
    }
    };
////// function to png //////
function savePNG (myDocument, docPath, basename, theSuffix) {
    // weboptions;
    var webOptions = new ExportOptionsSaveForWeb();
    webOptions.format = SaveDocumentType.PNG;
    webOptions.PNG8 = false;	
    webOptions.transparency = true;
    webOptions.interlaced = 0;
    webOptions.includeProfile = false;
    webOptions.optimized = true;
    myDocument.exportDocument(new File(docPath+"/"+basename+theSuffix+".png"), ExportType.SAVEFORWEB, webOptions);
    };
 ////// toggle visibility of others //////
 function hideOthers () {
    var desc10 = new ActionDescriptor();
    var list4 = new ActionList();
    var ref7 = new ActionReference();
    ref7.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
    list4.putReference( ref7 );
    desc10.putList( charIDToTypeID( "null" ), list4 );
    desc10.putBoolean( charIDToTypeID( "TglO" ), true );
    executeAction( charIDToTypeID( "Shw " ), desc10, DialogModes.NO );
    };