5 Replies Latest reply on May 6, 2010 6:57 AM by CraigOverman

    Layers to Files

    CraigOverman

      I'm looking for altered versions of the 'Layers to Files' script that will export jpegs that are named by group & layer names.

       

      For example, if I have a group/layer structure like this:

       

      ('>' are groups and '*' are layers)

       

      > Phones

            > Blackberry

                  * Storm

            > Apple

                  * iPhone

            > HTC

                  * NexusOne

       

      Is there a way to export the layers to files so that the files are named something similar to:

       

      - Phones_Blackberry_Storm.jpg

      - Phones_Apple_iPhone.jpg

      - Phones_HTC_NexusOne.jpg

       

      I have a very large psd with many nested groups and layers that need to be exported individually with a very specific naming scheme. Is there a script anywhere that will achieve this?  Or is there a work-around that I've not been able to find? I've looked everywhere and have only been able to find script alterations that remove the group/layer numbers.

       

      Any help is appreciated.

       

      Thanks

        • 1. Re: Layers to Files
          jugenjury Level 2

          I have some code that is close to what you would be needing, though it's used for a different purpose. I could adapt it, I guess.

           

          Are your document layers all NORMAL layers? Are there any adjustment layers? Smart Objects?

          • 2. Re: Layers to Files
            Paul Riggott Level 6

            This should be close....

            #target Photoshop
            main();
            function main(){
            if(!documents.length) return;
            selectAllLayers();
            var Lrs =  getSelectedLayersIdx();
            var Path = decodeURI(activeDocument.path);
            for(var a in Lrs){
               makeActiveByIndex( Lrs[a], false );
                if(app.activeDocument.activeLayer.kind == LayerKind.NORMAL){
                    var APPN = app.activeDocument.activeLayer.parent.parent.name
                    var APN = app.activeDocument.activeLayer.parent.name
                    var AL = app.activeDocument.activeLayer.name;
                    var Name = APPN + "_" +APN+"_"+AL+".jpg";
                    dupLayers(Name);
                    var saveFile = File(Path +"/" + Name);
                    SaveJPEG(saveFile, 12);      
                    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                    }
                }
            }
            function selectAllLayers(){  
               var desc = new ActionDescriptor(); 
                    var ref = new ActionReference(); 
                    ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) ); 
                desc.putReference( charIDToTypeID( "null" ), ref ); 
            executeAction( stringIDToTypeID( "selectAllLayers" ), desc, DialogModes.NO ); 
            }
            function getSelectedLayersIdx(){ 
               var selectedLayers = new Array; 
               var ref = new ActionReference(); 
               ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
               var desc = executeActionGet(ref); 
               if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){ 
                  desc = desc.getList( stringIDToTypeID( 'targetLayers' )); 
                  var c = desc.count 
                  var selectedLayers = new Array(); 
                  for(var i=0;i<c;i++){ 
                     selectedLayers.push(  desc.getReference( i ).getIndex() ); 
                  } 
               }else{ 
                  try{ 
                  activeDocument.backgroundLayer; 
                  var mod = 1; 
               }catch(e){ 
                  var mod = 0 
               } 
                  var ref = new ActionReference(); 
                  ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" )); 
                  ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
                  selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-mod); 
               } 
               return selectedLayers; 
            } 
            function makeActiveByIndex( idx, forceVisible ){
                 try{
                      var desc = new ActionDescriptor();
                      var ref = new ActionReference();
                      ref.putIndex(charIDToTypeID( "Lyr " ), idx)
                      desc.putReference( charIDToTypeID( "null" ), ref );
                      desc.putBoolean( charIDToTypeID( "MkVs" ), forceVisible );
                      executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
                 }catch(e){ return -1;}
            }
            function dupLayers(NewDocName) { 
                var desc143 = new ActionDescriptor();
                    var ref73 = new ActionReference();
                    ref73.putClass( charIDToTypeID('Dcmn') );
                desc143.putReference( charIDToTypeID('null'), ref73 );
                desc143.putString( charIDToTypeID('Nm  '), NewDocName );
                    var ref74 = new ActionReference();
                    ref74.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
                desc143.putReference( charIDToTypeID('Usng'), ref74 );
                executeAction( charIDToTypeID('Mk  '), desc143, DialogModes.NO );
            };
            function SaveJPEG(saveFile, jpegQuality){
            jpgSaveOptions = new JPEGSaveOptions();
            jpgSaveOptions.embedColorProfile = true;
            jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
            jpgSaveOptions.matte = MatteType.NONE;
            jpgSaveOptions.quality = jpegQuality; 
            activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
            }
            
            • 3. Re: Layers to Files
              Michael L Hale Level 5

              I think that I have modified 'Export Layers to Files' to meet your request. Because it is such a long script I have posted the new version here http://ps-scripts.com/bb/viewtopic.php?p=15029

              1 person found this helpful
              • 4. Re: Layers to Files
                CraigOverman Level 1

                They are all normal layers.  No effects or anything fancy at all.

                • 5. Re: Layers to Files
                  CraigOverman Level 1

                  Thank you VERY much!!  Your code worked, mostly.  It kept throwing me weird errors, so I just commented out those lines. (They didn't seem to add anything to the end result I was looking for, anyway)  I also tweaked the file name output to be specifically what I need.

                   

                  I really appreciate the help.