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.
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.
// 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++) {
...
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
...
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 );
};
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?
Copy link to clipboard
Copied
Hi @Stephen_A_Marsh ,
It should be export two separate images (JPEG).
Thanks
Copy link to clipboard
Copied
So adapt the Script I posted accordingly.
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.
Please note that, my expectaion is the layer color not equal to "Gray" or "Red".
thanks
Asuvath
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)])}
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
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:
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
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«)?
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);
};
Copy link to clipboard
Copied
It is working fine for me now. Thanks for your support.
Thanks
Copy link to clipboard
Copied
Good to read that!