12 Replies Latest reply on Dec 7, 2010 3:14 PM by sobiuno

    layer to picts with background

    sobiuno

      Hey guys. Hoping you can help me out here. I'm brand new to scripting and was wondering if one of you wizards could present me a script that would help me with the following.

      I need to save all visible layers with the background into their own PICT files. They would be coming from an open file, and going to this location on my network.   /Volumes/PIG/_REPOSITORY

      Also, they would need to be sent to a second location of   /Volumes/PIG/_REPOSITORY/copy

       

      They would need to be saved with the current layer name they already have. Also, I'm a bit new to this all, so I'll need a full rundown of how to use it after I enter it. I'm using applescript and CS4.

       

      I appreciate any help.

      Thanks!

        • 1. Re: layer to picts with background
          c.pfaffenbichler Level 9

          There is at least one knowledgeable and helpful contributor in this Forum with AppleScript-experience, but if you want to make it easier on yourself you might want to consider using JavaScript, because the number of people who can help with that seems considerably larger and more Scripts are available in that language.

           

          As for the task at hand:

          Are there any Groups and or Clipping Masks in the file or are all the Layers top-level?

          (If not things would be considerably more easy.)

           

          Edit: Could you post a screenshot of the Layers Panel?

          • 2. Re: layer to picts with background
            Muppet Mark-QAl63s Level 4

            I would just like to add to this… If you are just starting off with learning to script then you should do a little research into the pros n cons of the various languages on offer considering both your own platform and an overview of what you want your script to have access to. I do some fairly basic routine work flows in both AppleScript and JavaScript. (These are the two languages available to you on the mac from the Adobe Suite point of view). AppleScript allows you a wider access to your file system and the other applications installed on it. So if you need your script to communicate between applications then this is possibly the easier language to use. If on the other hand you just want a script to operate within apps of the Adobe suite then JavaScript may well prove to be the better choice. Given that JavaScript is platform independent your scripting questions are going to get a much wider audience… And they are a lot better at this too… But if you want AppleScript then I will try to help…

            • 3. Re: layer to picts with background
              sobiuno Level 1

              Here is the example. The ones that are marked red and hidden have to stay in the file for future use/reference. Technically, I guess I could do away with the heirarchy of groups and just bring them all to the top. Anyhow, the visible layers need to each be saved out individually with the background as a pict file to the two different locations, and possibly a third.

               

              This could be done in Java or Applescript. The format doesn't really matter to me. I have next to zero experience with either. Applescript seemed a bit easier to understand, but I'm open to trying to learn either one. Seeing how Java seems to be much more widely accepted and used, I'd probably lean towards that.

               

               

              Picture 4.png

              • 4. Re: layer to picts with background
                c.pfaffenbichler Level 9

                Well, that should be feasible without too much fuss in JavaScript.

                 

                Unfortunately Photoshop’s DOM does not have a »visible layers« property, but xbytor has provided a function to assess which layers are visible.

                Edit: Sorry, I was mistaken, I was actually thinking of »selected layers« – the visible ones are not that hard to determine.

                 

                So starting from that it should be possible to hide them and make one visible at a time and save the copies off of that.

                 

                Saving twice might be avoided by having the Script copy the resulting File into the second folder instead.

                • 5. Re: layer to picts with background
                  c.pfaffenbichler Level 9

                  Hmm … to quote the Photoshop CS5 Help:

                  »Though Photoshop can open raster PICT files, it cannot open QuickDraw PICTs or save to PICT format.«

                   

                  Seems to be new to CS5.

                  • 6. Re: layer to picts with background
                    sobiuno Level 1

                    I'm actually working with CS4, so this isn't an issue.

                    • 7. Re: layer to picts with background
                      sobiuno Level 1

                      So starting from that it should be possible to hide them and make one visible at a time and save the copies off of that

                       

                      Not sure how that would work. Every time I update a file, there is going to be different numbers of new files, and always in different areas. I just figure leaving them visible would be easiest as there is already a similar script as a default.

                      • 8. Re: layer to picts with background
                        Paul Riggott Level 6

                        You could try this....

                        #target photoshop
                        main();
                        function main(){
                        if(!documents.length) return; 
                        app.displayDialogs = DialogModes.NO;
                        var strtRulerUnits = app.preferences.rulerUnits; 
                        var strtTypeUnits = app.preferences.typeUnits; 
                        app.preferences.rulerUnits = Units.PIXELS; 
                        app.preferences.typeUnits = TypeUnits.PIXELS;
                        var Name = app.activeDocument.name.replace(/\.[^\.]+$/, ''); 
                        processLayers(app.activeDocument);
                        app.preferences.rulerUnits = strtRulerUnits; 
                        app.preferences.typeUnits = strtTypeUnits; 
                        function processLayers (objectRef) {
                        var myNumber = objectRef.layers.length; 
                        for (var i = 0; i < myNumber ; i++) { 
                        var myLayer = objectRef.layers[i]; 
                        if(myLayer.visible==false) continue;
                          activeDocument.activeLayer = myLayer; 
                          if (activeDocument.activeLayer.isBackgroundLayer) continue;
                          if(myLayer.kind == LayerKind.NORMAL){
                              var LB = app.activeDocument.activeLayer.bounds; 
                                var Width = LB[2].value - LB[0].value;
                                var Height = LB[3].value - LB[1].value;
                         if(Width > 0){
                             var LayerName = app.activeDocument.activeLayer.name;
                             selectLayerByName("Background",true);
                              var Path = decodeURI(activeDocument.path);
                              dupLayers(LayerName);
                              var saveFile= File(Path + "/" + LayerName + ".pct");
                             SavePict(saveFile);
                             app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                             }
                              } 
                        if (myLayer.typename == 'LayerSet') { 
                          processLayers (myLayer);
                                } 
                            } 
                        }
                        }
                        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 selectLayerByName(lyrName,add){ 
                              add == undefined ? add = false : add = true;
                              var desc = new ActionDescriptor(); 
                              var ref = new ActionReference(); 
                              ref.putName( charIDToTypeID( "Lyr " ), lyrName); 
                              desc.putReference( charIDToTypeID( "null" ), ref ); 
                              if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
                              desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
                              executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO ); 
                           };
                        function SavePict(saveFile){
                        pictSaveOptions = new PICTFileSaveOptions ();
                        pictSaveOptions.alphaChannels = false;
                        pictSaveOptions.embedColorProfile = false;
                        pictSaveOptions.resolution = PICTBitsPerPixels.THIRTYTWO;
                        pictSaveOptions.compression = PICTCompression.JPEGHIGHPICT;
                        activeDocument.saveAs(saveFile, pictSaveOptions, true, Extension.LOWERCASE); 
                        }
                        
                        • 9. Re: layer to picts with background
                          sobiuno Level 1

                          Thanks for the help, Paul. It seemed to hang up though. I got the following error message...

                           

                          Error 8800: General Photoshop error occurred. This functionality may not be available in this version of photoshop.

                          -The object "layer "Background"" is not currently available.

                          Line:60

                          ->         executeAction( charIDToTypeID( slct" ), desc,

                          DialogModes.NO );

                           

                          The only way I changed the code is adding a location to save to as seen below.

                           

                          #target photoshop
                          main();
                          function main(){
                          if(!documents.length) return; 
                          app.displayDialogs = DialogModes.NO;
                          var strtRulerUnits = app.preferences.rulerUnits; 
                          var strtTypeUnits = app.preferences.typeUnits; 
                          app.preferences.rulerUnits = Units.PIXELS; 
                          app.preferences.typeUnits = TypeUnits.PIXELS;
                          var Name = app.activeDocument.name.replace(/\.[^\.]+$/, ''); 
                          processLayers(app.activeDocument);
                          app.preferences.rulerUnits = strtRulerUnits; 
                          app.preferences.typeUnits = strtTypeUnits; 
                          function processLayers (objectRef) {
                          var myNumber = objectRef.layers.length; 
                          for (var i = 0; i < myNumber ; i++) { 
                          var myLayer = objectRef.layers[i]; 
                          if(myLayer.visible==false) continue;
                            activeDocument.activeLayer = myLayer; 
                            if (activeDocument.activeLayer.isBackgroundLayer) continue;
                            if(myLayer.kind == LayerKind.NORMAL){
                                var LB = app.activeDocument.activeLayer.bounds; 
                                  var Width = LB[2].value - LB[0].value;
                                  var Height = LB[3].value - LB[1].value;
                           if(Width > 0){
                               var LayerName = app.activeDocument.activeLayer.name;
                               selectLayerByName("Background",true);
                                var Path = decodeURI(activeDocument.path);
                                dupLayers(LayerName);
                                var saveFile= File(Path + "/volumes/pig/_repository/!headshots/2010 nfl/process/afc south (jr)/houston texans/z_test" + LayerName + ".pct");
                               SavePict(saveFile);
                               app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                               }
                                } 
                          if (myLayer.typename == 'LayerSet') { 
                            processLayers (myLayer);
                                  } 
                              } 
                          }
                          }
                          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 selectLayerByName(lyrName,add){ 
                                add == undefined ? add = false : add = true;
                                var desc = new ActionDescriptor(); 
                                var ref = new ActionReference(); 
                                ref.putName( charIDToTypeID( "Lyr " ), lyrName); 
                                desc.putReference( charIDToTypeID( "null" ), ref ); 
                                if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
                                desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
                                executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO ); 
                             };
                          function SavePict(saveFile){
                          pictSaveOptions = new PICTFileSaveOptions ();
                          pictSaveOptions.alphaChannels = false;
                          pictSaveOptions.embedColorProfile = false;
                          pictSaveOptions.resolution = PICTBitsPerPixels.THIRTYTWO;
                          pictSaveOptions.compression = PICTCompression.JPEGHIGHPICT;
                          activeDocument.saveAs(saveFile, pictSaveOptions, true, Extension.LOWERCASE); 
                          }
                          
                          
                          • 10. Re: layer to picts with background
                            c.pfaffenbichler Level 9

                            I also attempted a Script, but as I don’t have CS4 installed anymore it saves JPGs into two folders.

                             

                            // 2010, use it at your own risk;
                            #target photoshop
                            // the target folders;
                            var thePath = "/Backup HD/12";
                            var theSecondPath = "/Backup HD/123";
                            // verify the conditions;
                            if (app.documents.length > 0 && app.activeDocument.mode != DocumentMode.BITMAP && app.activeDocument.mode != DocumentMode.INDEXEDCOLOR 
                            && app.activeDocument.mode != DocumentMode.MULTICHANNEL && Folder(thePath).exists == true && Folder(theSecondPath).exists == true) {
                              var myDocument = app.activeDocument;
                                var docName = myDocument.name;
                                if (docName.indexOf(".") != -1) {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}
                                else {var basename = docName};
                            // jpg options;
                                var jpegOptions = new JPEGSaveOptions();
                                jpegOptions.quality = 9;
                                jpegOptions.embedColorProfile = true;
                                jpegOptions.matte = MatteType.NONE;
                            // get the visible layers;
                                var theLayers = collectVisibleLayers(myDocument);
                            // hide them;
                                for (var m = 1; m < theLayers.length; m++) {
                                    theLayers[m].visible = false;
                                    };
                            // work through the collection minus the first one;
                                for (var n = 1; n < theLayers.length; n++) {
                                    theLayers[n].visible = true;
                                    var theFile = thePath+"/"+basename+"_"+theLayers[n].name+".jpg";
                                    myDocument.saveAs((new File(theFile)),jpegOptions,true);
                                    File(theFile).copy(theSecondPath+"/"+basename+"_"+theLayers[n].name+".jpg");
                                    theLayers[n].visible = false;
                                    };
                            // show them again;
                                for (var o = 1; o < theLayers.length; o++) {
                                    theLayers[o].visible = true;
                                    };
                                };
                            ////////////////////////////////////
                            ////////////////////////////////////
                            ////////////////////////////////////
                            ////// function to collect all visible layers //////
                            function collectVisibleLayers (theParent) {
                                 if (!allLayers) {var allLayers = new Array} 
                                 else {};
                                 for (var m = theParent.layers.length - 1; m >= 0;m--) {
                                      var theLayer = theParent.layers[m];
                                      if (theLayer.typename == "ArtLayer" && theLayer.visible == true) {
                                           allLayers.push(theLayer)
                                           }
                                      else {
                                        if (theLayer.visible == true) {
                                            allLayers = allLayers.concat(collectVisibleLayers(theLayer))
                                            }
                                           }
                                      };
                                 return allLayers
                                };

                            • 11. Re: layer to picts with background
                              Paul Riggott Level 6

                              Ah, looking at your screenshot, you are not using the background only a layer named background!

                              Change line 27 selectLayerByName("Background",true); to selectLayerByName("background",true); as your layer name is all lower case.

                              1 person found this helpful
                              • 12. Re: layer to picts with background
                                sobiuno Level 1

                                everything is working smoothly now. Only question is how I would get it to save to a particular folder/folders