0

This is a working script which do the same job as it would if you hit tab File>Export>Quick Export As (PNG/JPG).
There are 4 artboards and 7 layers in document. I need export 4 arboards.
Result: all 4 artboards are exported as PNG into folder. Code export works like all in one. No loop is needed (for var i=0...,i++)

Problem is when I add close document at the end of script activeDocument.close(...).
Result: NO artboards are exported, document is closed.
There is not export like 1 artboard from 4 artboards.
I can export all (4) artboards withous close document
or
close document and NO artboards are exported.
How to make this code export and then close?
Thank you!
// export activeDocument
quick_export_png(activeDocument.path.fsName); 
// export activeLayer
quick_export_png(activeDocument.path.fsName, true); 

function quick_export_png(path, layer)
{
   try
   {
       if (layer == undefined) layer = false;    
       var d = new ActionDescriptor();
       var r = new ActionReference();
       r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
       d.putReference(stringIDToTypeID("null"), r);
       d.putString(stringIDToTypeID("fileType"), "png");
       d.putInteger(stringIDToTypeID("quality"), 32);      
       d.putInteger(stringIDToTypeID("metadata"), 0);
       //d.putString(stringIDToTypeID("destFolder"), "Z:/Luke/Batch Final"); 
       d.putString(stringIDToTypeID("destFolder"), path);  
       d.putBoolean(stringIDToTypeID("sRGB"), true);
       d.putBoolean(stringIDToTypeID("openWindow"), false);
       executeAction(stringIDToTypeID(layer?"exportSelectionAsFileTypePressed":"exportDocumentAsFileTypePressed"), d, DialogModes.NO);
   }
   catch (e) { throw(e); }
}   
activeDocument.close(SaveOptions.SAVECHANGES); //this cancels previous export!
8
  • Guessing.. you need to loop through layers, then close when the loop is done. Otherwise you get 1 export, then the document closes. Your script basically states -- Do X, then close. You need it to state -- Do X for each layer, then close.
    – Scott
    Oct 22, 2022 at 20:35
  • Does it close the document immediately, or does it still take the time it would normally take to export the artboards and then the document closes? If it closes immediately, it’s probably running the functions asynchronously, so it ends up closing the document before it’s managed to finish exporting even the first artboard. Oct 23, 2022 at 12:58
  • @Scott thank you for idea. There are 4 artboards and 7 layers in document. I need export 4 arboards. Result: all 4 artboards are exported as PNG into folder. No loop is needed (for var i=0...,i++). Code export works like export all artboards (4) in one. Problem is when I add close document at the end of script activeDocument.close(...). Result: NO artboards are exported, document is closed. There is not export like 1 artboard from 4 artboards. I can export all (4) artboards without close document or close document and NO artboards are exported.
    – Jenna
    Oct 23, 2022 at 15:28
  • @Janus Bahs Jacquet thank you. The script works fast (you can try it). I do not notice difference if i run script with doc.close() or without it. Asynchronus functionS ? How to stop asynchronous run or how to close doc correctly? There is only one function. After function and out of function is doc.close().
    – Jenna
    Oct 23, 2022 at 15:35
  • you cant control assychronious actions. the reason it feels fast for you is that the computer works in the background. but obviously if you kill it then it does not work in the background thus you get nothing out.
    – joojaa
    Oct 23, 2022 at 19:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.