4 Replies Latest reply on Apr 24, 2015 4:40 PM by AffectDesign

    Merge and export layer groups separately into jpgs via script

    Misnomer23 Level 1

      Hi all, I’m trying to take several layers from a group, merge them, and export them into jpgs, then move on to the next layer group. Saving all the files in a new directory called "images"

      My layers are organized into groups, I’d also like to be able to have the script ignore layers that end with a specific character (or some other tag)

      So far I’ve found several posts that seem to point in the right direction.

      http://ps-scripts.com/bb/viewtopic.php?f=9&t=3348&p=15050&hilit=export+rename&sid=e0ae04e5 a547e2fd84ce7c8df2c2916d#p15050

      This one exports the top visible layer(s) as a jpg with the same filename as the .PSD

      <code>

      function createFolder( folderObj ){ if( !folderObj.parent.exists ) createFolder( folderObj.parent ); if( !folderObj.exists ) folderObj.create(); }; function exportSFW( doc, saveFile, qty ) { var exportOpts = new ExportOptionsSaveForWeb( ); exportOpts.format = SaveDocumentType.JPEG exportOpts.includeProfile = true;//default false exportOpts.quality = qty; if ( saveFile.exists ) saveFile.remove( ); doc.exportDocument( saveFile, ExportType.SAVEFORWEB, exportOpts ); }; // make a new folder object from the doc path and change the structure from to-do to done. Note the folder being replace should not the doc path.parent // S:\to do\folderA\subfolder36 to S:\done\folderA\subfolder36 not S:\to do\folderA\subfolder36 to S:\to do\folderA\subfolder37 var strPath = decodeURI( app.activeDocument.path ); strPath = strPath.replace( 'work', 'ret'); //strPath = strPath+'-retouched'; //strPath = strPath.replace(//.{3}//, '/' );// will remove the first three char folder in the path var saveFolder = new Folder( strPath ) createFolder( saveFolder ); var saveName = decodeURI(app.activeDocument.name); saveName.match(/(.)(.[.]+)/) ? saveName = saveName.match(/(.)(.[.]+)/):saveName = [saveName, saveName]; var saveFile = new File( saveFolder + '/' + saveName[1] + '.jpg' ); // do your save for web step here using saveFile exportSFW( app.activeDocument, saveFile, 80 );

      </code>

      And

      http://ps-scripts.com/bb/viewtopic.php?f=9&t=3909&p=18107&hilit=export+directory&sid=e0ae0 4e5a547e2fd84ce7c8df2c2916d#p18107

      This one creates two folders, one called renders and the other archive, it then merges the layers and exports them into said folders as .exr files with the names of those files pulled from the layer names.

      <code>

      #target photoshop function RGBMain(){ if(!documents.length) return; var doc = activeDocument; var oldPath = activeDocument.path; var renderDir = new Folder(oldPath+"/"+"renders"); var archDir = new Folder(oldPath+"/"+"archive");  if(!renderDir.exists) renderDir.create(); if(!archDir.exists) archDir.create(); var fileList= archDir.getFiles("*.exr").sort(); var LastVersion=0; try{ LastVersion = decodeURI(fileList.pop().name).replace(/\.[^\.]+$/, '').match(/\d+$/); }catch(e){LastVersion=0;} fileList=[]; LastVersion = zeroPad((Number(LastVersion) +1),2); for(var a=0;a<doc.layerSets.length;a++){ activeDocument.activeLayer = activeDocument.layers.getByName(doc.layerSets[a].name); dupLayers(); activeDocument.mergeVisibleLayers(); floatLayer (); if(activeDocument.bitsPerChannel != BitsPerChannelType.THIRTYTWO){ /* //Change profile, run an action etc... if (activeDocument.colorProfileName != "PA_Agfa_D-Lab_Sensatis_09-08-05"){ activeDocument.convertProfile("PA_Agfa_D-Lab_Sensatis_09-08-05", Intent.RELATIVECOLORIMETRIC); } */ activeDocument.bitsPerChannel = BitsPerChannelType.THIRTYTWO; } var archiveFile= File(archDir +"/"+doc.layerSets[a].name +"_v"+LastVersion+".exr"); var saveFile= File(renderDir +"/"+doc.layerSets[a].name +".exr"); saveEXR(archiveFile) saveEXR(saveFile); app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); } } RGBMain();  function alphaMain(){ if(!documents.length) return; 

      var doc = activeDocument; var oldPath = activeDocument.path; var renderDir = new Folder(oldPath+"/"+"renders"); var renderDir = new Folder(oldPath+"/"+"renders"); var archDir = new Folder(oldPath+"/"+"archive");

      if(!renderDir.exists) renderDir.create(); var fileList= renderDir.getFiles("*.exr").sort(); var LastVersion=0; try{ LastVersion = decodeURI(fileList.pop().name).replace(/\.[^\.]+$/, '').match(/\d+$/); }catch(e){LastVersion=0;} fileList=[]; LastVersion = zeroPad((Number(LastVersion) +1),2); for(var a=0;a<doc.layerSets.length;a++){ activeDocument.activeLayer = activeDocument.layers.getByName(doc.layerSets[a].name); dupLayers(); activeDocument.mergeVisibleLayers(); 

      createAlpha ();

      activeDocument.bitsPerChannel = BitsPerChannelType.THIRTYTWO; 

      var archFile= File(archDir +"/"+doc.layerSets[a].name + "_matte_v"+LastVersion+".exr"); var saveFile= File(renderDir +"/"+doc.layerSets[a].name + "_matte" +".exr"); saveEXR(archFile); saveEXR(saveFile); app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); } } alphaMain();

      function floatLayer(){ var layersNum = activeDocument.layers.length; for (var x = 1; x <= 10; x++) { app.activeDocument.activeLayer.duplicate(); if(layersNum >10){break;} activeDocument.mergeVisibleLayers() app.activeDocument.activeLayer.duplicate(); if(layersNum >10){break;} activeDocument.mergeVisibleLayers() app.activeDocument.activeLayer.duplicate(); if(layersNum >10){break;} activeDocument.mergeVisibleLayers() } return; }  function dupLayers() { var desc143 = new ActionDescriptor(); var ref73 = new ActionReference(); ref73.putClass( charIDToTypeID('Dcmn') ); desc143.putReference( charIDToTypeID('null'), ref73 ); desc143.putString( charIDToTypeID('Nm  '), activeDocument.activeLayer.name ); var ref74 = new ActionReference(); ref74.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); desc143.putReference( charIDToTypeID('Usng'), ref74 ); executeAction( charIDToTypeID('Mk  '), desc143, DialogModes.NO ); };  function saveEXR(saveFile){ var idsave = charIDToTypeID( "save" ); var desc6 = new ActionDescriptor(); var idAs = charIDToTypeID( "As  " ); desc6.putString( idAs, "OpenEXR" ); var idIn = charIDToTypeID( "In  " ); desc6.putPath( idIn, saveFile ); executeAction( idsave, desc6, DialogModes.NO ); } function zeroPad(n, s) { n = n.toString(); while (n.length < s)  n = '0' + n; return n; };  function createAlpha (){ var desc = new ActionDescriptor(); var ref = new ActionReference(); ref.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) ); desc.putReference( charIDToTypeID( "null" ), ref ); var ref1 = new ActionReference(); ref1.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Trsp" ) ); desc.putReference( charIDToTypeID( "T   " ), ref1 ); executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );  app.activeDocument.artLayers.add() activeDocument.activeLayer.name = "newmatte"; app.foregroundColor.rgb.hexValue = 'FFFFFF'; app.activeDocument.selection.fill(app.foregroundColor); app.activeDocument.selection.deselect();  var layerRef = app.activeDocument.layers[0] var newLayerRef = app.activeDocument.artLayers.add() app.backgroundColor.rgb.hexValue = '000000'; app.activeDocument.selection.fill(app.backgroundColor); app.activeDocument.selection.deselect();  newLayerRef.move(layerRef,ElementPlacement.PLACEAFTER) activeDocument.mergeVisibleLayers(); 

      };

      </code>

      I don’t really know what I’m doing.

        • 1. Re: Merge and export layer groups separately into jpgs via script
          JJMack Most Valuable Participant

          Misnomer23 wrote:

           

          I don’t really know what I’m doing.

          If you do not have a programming background you should not be look at making custom Photoshop scripts. Like Photoshop actions have limitations Photoshop Scripting DOM does not cover all of Photoshop features. In fact many Photoshop scripts need to rely on the Action Manager to use many of Photoshop's features.  There is an Adobe Photoshop plug-in that can record Action Manger script code for steps you perform in Photoshop like the Action Palette can record action steps. To be able to write custom Photoshop Scripts you need to be a programmer of sorts . Know Photoshop well and be able to use the Scriptlistener Plug-in.  If that is not you you need to educate yourself.

           

          Photoshop ships with quit a few script for user to use. No programming is needed to use these. There are also many scripts you can download and use. Some are used from menu File>Script>Script Name,Others from menu File>Automate>Automation Name others are use from other features. Some you may know about.  Image Processor, Image Processor Pro(download), Photomerge, Lens Correction, Fit Image, import export preferences, straighten. There are many more.

           

          If you need a custom script you should ask for help with the code you developing over in the Photoshop Scripting forum. However at least one of the best contributor over there has drop because of CC. Paul Riggott write he would no longer participate a big loss.

          http://forums.adobe.com/community/photoshop/photoshop_scripting?view=all

          • 2. Re: Merge and export layer groups separately into jpgs via script
            Misnomer23 Level 1

            Thank you for taking the time to respond, I will do as you suggest.

            • 3. Re: Merge and export layer groups separately into jpgs via script
              zachmandt Level 1

              Hey Misnomer, I stumbled upon this bit of code here and it looked familiar.   If you are still trying to get this thing working, I have a finished, working version of it available here:

               

              http://www.zachrm.com/tools/

               

              It will work on cs4, cs5, and it will work on a mac on cs6, but I'm having some issues with the code for some reason getting it to work on a pc for cs6.  I've got a new version coming out here in the near future with some added stuff.  Hopefully it helps you.

              • 4. Re: Merge and export layer groups separately into jpgs via script
                AffectDesign Level 1

                Hello,

                 

                Were you ever able to get this working? I'm looking to do the same thing. I would like to save all top level groups in a psd file out to separate images (jpg or png).

                 

                Thanks.