• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script for Export the artboard based on the Layer color

Participant ,
Aug 01, 2022 Aug 01, 2022

Copy link to clipboard

Copied

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.


Screenshot 2022-08-01 at 7.30.14 PM.png

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
TOPICS
Actions and scripting , macOS

Views

508

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

correct answers 2 Correct answers

Community Expert , Aug 02, 2022 Aug 02, 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++) {
   
...

Votes

Translate

Translate
Community Expert , Aug 08, 2022 Aug 08, 2022

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, webO
...

Votes

Translate

Translate
Adobe
Community Expert ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

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

Votes

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 Expert ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

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

Votes

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
Participant ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

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

Votes

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 Expert ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

So adapt the Script I posted accordingly. 

Votes

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
Participant ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

Hi @c.pfaffenbichler ,

Sorry, actually I am testing your code, so I am not responding for your replies.

The code running without any error, but it will not produce any output (PNG) in docpath.

Also I am not clear about the below code (highlighted in red), can you please clarify.

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

Please note that, my expectaion is the layer color not equal to "Gray" or "Red".

thanks
Asuvath

Votes

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 Expert ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

In the code 

 

if (thisOne[3] == "grain") {

 

refers to green ArtBoards – there seems to be a typo in the original code and »grain« signifies »green«. 

 

To see what the various parts of the Array are see the line 

 

    if (v01 != v03 && v01 != v04) {theLayers.push([theName, theIndex, theID, typeIDToStringID(theColor)])}

 

Votes

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
Participant ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

Hi @c.pfaffenbichler ,
Thanks for the explanation, but this code is not produced the output (PNG). Can you please check?
Also, our expectation is not equal to "Red" or "Gray".

Thanks
Asuvath

Votes

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 Expert ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied


Also, our expectation is not equal to "Red" or "Gray".

Well, then you should have said so in your original post.

(edit: Admittedly you mentioned you don’t want to export the red and gray ones but you firstly stated you want to export the blue one.) 

The code is in plain, readable form, so feel free to amend it however necessary. 

 


Thanks for the explanation, but this code is not produced the output (PNG). Can you please check?

Works fine here, please provide the file for testing. 

 

Edit: exportGreenArtboardsScr.gif

Votes

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
Participant ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

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

Votes

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 Expert ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

Sorry, I meant the actual image, not a recording. 

Did you change the if-clause to check for »blue« (or not »red« and not »gray«)? 

Votes

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 Expert ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

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

 

Votes

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
Participant ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

Hi @c.pfaffenbichler,

It is working fine for me now. Thanks for your support.
Thanks

Votes

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 Expert ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

LATEST

Good to read that! 

Votes

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