10 Replies Latest reply on Sep 16, 2014 1:04 PM by JJMack

    Opening files one by one

    Wafel1

      Hi,

       

      I'm trying to write a script to open file1 from folder Z, if file1 contains '10' in it's name then run action named '10', then open file2 from folder Z, if file2 contains '20' in it's name then run action '20' and so on for multiple files.

      I've been searching for hours but can't find any help regarding opening a file, running action, and THEN opening the next file. All threads just regard opening multiple files at the same time.

       

      Any kind of help would be very much appreciated.

       

      Regards

        • 1. Re: Opening files one by one
          c.pfaffenbichler Level 9

          Does this help?

          #target photoshop

          var theFolder = Folder.selectDialog ("select folder");

          if (theFolder) {

          var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i);

          for (var m = 0; m < theFiles.length; m++) {

          var thisFile = app.open(File(theFiles[m]));

          if (thisFile.name.indexOf("10") != -1) {app.doAction(/*insert name of action*/, /*insert name of action set*/)}

          }

          };

          • 2. Re: Re: Opening files one by one
            Wafel1 Level 1

            Wow. You're a hero - this worked perfectly. Just one more thing! I want to open these pdfs with certain settings so I'll add the below, but if you could just show me where to add the pdfOptions in the code.

             

            var pdfOptions= new PDFOpenOptions(); 

             

            pdfOpenOptions.antiAlias = true

            pdfOpenOptions.mode = DocumentMode.RGB; 

            pdfOpenOptions.bitsPerChannel = BitsPerChannelType.EIGHT; 

            pdfOpenOptions.resolution = 300

            pdfOpenOptions.cropPage = CropToType.TRIMBOX;

             

             

            Thanks!

            • 3. Re: Opening files one by one
              c.pfaffenbichler Level 9

              var thePdf = app.open(/* the file*/, pdfOpenOptions);

              • 4. Re: Opening files one by one
                Wafel1 Level 1

                This may sound a little silly but I can't figure out where to put this within the code. Also I'm not sure what to put in place of /* the file*/ that you put in your reply.

                 

                Could you please show me how to inject this

                var thePdf = app.open(/* the file*/, pdfOpenOptions);

                Into this

                #target photoshop

                var theFolder = Folder.selectDialog ("select folder");

                if (theFolder) {

                var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i);

                for (var m = 0; m < theFiles.length; m++) {

                var thisFile = app.open(File(theFiles[m]));

                if (thisFile.name.indexOf("10") != -1) {app.doAction(/*insert name of action*/, /*insert name of action set*/)}

                }

                };

                Also what to put in place of /* the file*/.

                 

                I'm very new to this. Just barely grasping javascript - have just started learning.

                • 5. Re: Opening files one by one
                  JJMack Most Valuable Participant

                  var thisFile =app.open(File(theFiles[m]), pdfOptions);

                  Capture.jpg

                  • 6. Re: Opening files one by one
                    Wafel1 Level 1

                    Funny - have tried this but assumed it must be wrong because the file size was different if I did the exact same steps manually. Have just tested some more and it seems that this opens with correct settings but for whatever reason the filesize is different.

                     

                    Anyway thanks to you both for the help!

                    • 7. Re: Opening files one by one
                      Kevinoneil

                      how can I modify this to run a function instead of an action or action set?

                      • 8. Re: Opening files one by one
                        JJMack Most Valuable Participant

                        Kevinoneil wrote:

                         

                        how can I modify this to run a function instead of an action or action set?

                        I do not understand what you mean by THIS

                        "how can I modify this to run a function instead of an action or action set?"

                        • 9. Re: Re: Opening files one by one
                          Kevinoneil Level 1

                          Sorry, I supposed putting my reply in context would help

                           

                          I wanted to modify the script in the previous post to run a function instead of an action. I was able to make it work, sort of. But I think that my issue is the function I have set up.

                           

                          function newDocFromLayer(docName,layerName){ 
                              docName == undefined ? docName = "Untitled":docName; 
                              layerName == undefined ? layerName = activeDocument.activeLayer.name:layerName; 
                              var desc = new ActionDescriptor(); 
                                  var ref = new ActionReference(); 
                                  ref.putClass( charIDToTypeID( "Dcmn" ) ); 
                              desc.putReference( charIDToTypeID( "null" ), ref ); 
                              desc.putString( charIDToTypeID( "Nm  " ), docName ); 
                                  var ref1 = new ActionReference(); 
                                  ref1.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) ); 
                              desc.putReference( charIDToTypeID( "Usng" ), ref1 ); 
                              desc.putString( charIDToTypeID( "LyrN" ), layerName ); 
                          executeAction( charIDToTypeID( "Mk  " ), desc, DialogModes.NO ); 
                          }; 
                          //newDocFromLayer ();
                          #target photoshop
                          var theFolder = Folder.selectDialog ("select folder");
                          if (theFolder) {
                          var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i);
                          for (var m = 0; m < theFiles.length; m++) {
                          var thisFile = app.open(File(theFiles[m]));
                          newDocFromLayer ();
                          }
                          };
                          

                           

                          What I wanted to do was open each file in a folder, and then copy all of the layers from each file into a new doc. Right now, It's opening each file in the folder, ad copying a single layer group to a new doc.

                           

                          let me know if you think I should start a new thread, I don't want to hijack this one!

                          • 10. Re: Re: Opening files one by one
                            JJMack Most Valuable Participant

                            To process all layers in a document requires a recursive process.  I do not know javascript I just hack code I steal.  I admit it I'm a thief that make other's code do my bidding.  I'm not proud or vain just an ordinary thief.

                             

                            Here is something I hacked to look at a documents layers. Become a thief  perhaps you can hack a hack. processArtLayers.jsx don't let my ownership statement put you off  I'm a thief you should not believe a thief. And don't ask how it works or it if 100%.  After I hack I forget all too much junk for me little mind to remember.

                             

                            // A Photoshop Script by JJMack's

                            // This script processes ArtLayers.

                            // This script is supplied as is. It is provided as freeware.

                            // The author accepts no liability for any problems arising from its use.

                             

                            /*

                            <javascriptresource>

                            <about>$$$/JavaScripts/processArtLayers/About=JJMack's Process All Layers.^r^rCopyright 2009 Mouseprints.^r^rScript utility for action.^rNOTE:Deletes Seleted Area from background and all normal layers.</about>

                            </javascriptresource>

                            */

                            //<category>JJMack's Script</category>

                             

                            // enable double-clicking from Mac Finder or Windows Explorer

                            #target photoshop // this command only works in Photoshop CS2 and higher

                             

                            // bring application forward for double-click events

                            app.bringToFront();

                             

                            // ensure at least one document open

                            if (!documents.length) {

                              alert('There are no documents open.', 'No Document');

                            }

                            // if at least one document exists, then proceed

                            else {

                              app.activeDocument.suspendHistory('processArtLayers','main()');

                            }

                             

                            ///////////////////////////////////////////////////////////////////////////////

                            // main - main function

                            ///////////////////////////////////////////////////////////////////////////////

                            function main() {

                             

                              // declare local variables

                              var saveactiveLayer = activeDocument.activeLayer;

                             

                              // Set the ruler units to PIXELS

                              var orig_ruler_units = app.preferences.rulerUnits;

                              app.preferences.rulerUnits = Units.PIXELS;

                             

                              try {

                              processArtLayers(activeDocument);

                              removeAllEmptyLayerSets(activeDocument);

                              }

                              // display error message if something goes wrong

                              catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }

                             

                              try { activeDocument.activeLayer = saveactiveLayer;}

                              catch(e) { } // may have been deleted

                             

                              // Reset units to original settings

                              app.preferences.rulerUnits = orig_ruler_units;

                            }

                            ///////////////////////////////////////////////////////////////////////////////

                            // End - main function

                            ///////////////////////////////////////////////////////////////////////////////

                             

                            ///////////////////////////////////////////////////////////////////////////////

                            // Function: processsArtLayers

                            // Input: document or layer set

                            // Return: <none>, all layers that were invisible are now gone

                            ///////////////////////////////////////////////////////////////////////////////

                            function processArtLayers(obj) {

                                for( var i = obj.artLayers.length-1; 0 <= i; i--) {

                                    try {

                                        processLayer(obj.artLayers[i]);

                              }

                                    catch (e) {

                                    }

                                }

                                for( var i = obj.layerSets.length-1; 0 <= i; i--) {

                              infoLayer(obj.layerSets[i]);    // Display layerSet informarion

                                    processArtLayers(obj.layerSets[i]);

                                }

                            }

                             

                            ///////////////////////////////////////////////////////////////////////////////

                            // Function: removeAllEmptyLayerSets

                            // Usage: find all empty layer sets and remove them, recursively

                            // Input: document or layer set

                            // Return: empty layer sets are now gone

                            ///////////////////////////////////////////////////////////////////////////////

                            function removeAllEmptyLayerSets(obj) {

                                var foundEmpty = true;

                                for( var i = obj.layerSets.length-1; 0 <= i; i--) {

                                    if( removeAllEmptyLayerSets(obj.layerSets[i])) {

                                        obj.layerSets[i].remove();

                                    } else {

                                        foundEmpty = false;

                                    }

                                }

                                if (obj.artLayers.length > 0) {

                              foundEmpty = false;

                              }

                                return foundEmpty;

                            }

                             

                            function processLayer(layer) {

                              infoLayer(layer);

                            }

                             

                            function infoLayer(layer){

                              try{

                              alert("layer = " + layer

                              + "\rallLocked = " + layer.allLocked

                              + "\rblendMode = " + layer.blendMode

                              + "\rbounds = " + layer.bounds

                              + "\rfillOpacity = " + layer.fillOpacity

                            // + "\rfilterMaskDensity = " + layer.filterMaskDensity // undefined for groups not valid others ??

                            // + "\rfilterMaskFather = " + layer.filterMaskFeather // undefined for groups not valid others ??

                              + "\rgrouped = " + layer.grouped

                              + "\risBackgroundLayer = " + layer.isBackgroundLayer

                              + "\rkind = " + layer.kind

                              + "\rlayerMaskDensity = " + layer.layerMaskDensity

                              + "\rlayerMaskFeather = " + layer.layerMaskFeather

                              + "\rlinkedLayers = " + layer.linkedLayers

                              + "\rname = " + layer.name

                              + "\ropacity = " + layer.opacity

                              + "\rparent = " + layer.parent

                              + "\rpixelsLocked = " + layer.pixelsLocked

                              + "\rpositionLocked = " + layer.positionLocked

                            // + "\rtextItem = " + layer.textItem // Valid only when kind = LayerKind.TEXT.

                              + "\rtransparentPixelsLocked = " + layer.transparentPixelsLocked

                              + "\rtypename = " + layer.typename

                              + "\rvectorMaskDensity = " + layer.vectorMaskDensity

                              + "\rvectorMaskFeather = " + layer.vectorMaskFeather

                              + "\rvisible = " + layer.visible

                              + "\rxmpMetadata = " + layer.xmpMetadata

                              );

                              }

                              catch(e){

                              }

                            };

                             

                            /*

                            allLocked               * boolean read-write. true to completely lock the contents and settings of this layer.

                            blendMode               * blendmode read-write. the blending mode.

                            bounds                  * array of unitvalue read-only. an array of coordinates that describes the bounding rectangle of the layer.

                            fillOpacity             * number [0.0..100] read-write. the interior opacity of the layer, a percentage value.

                            filterMaskDensity        double read-write. the density of the filter mask (between 0.0 and 250.0)

                            filterMaskFeather        double read-write. the feather of the filter mask (between 0.0 and 250.0)

                            grouped                 * boolean read-write. true if this layer is grouped with the layer beneath it.

                            isBackgroundLayer       * boolean read-write. true if this is the background layer of the document. a document can have only one background layer. if there is no background layer,

                                                     setting this to true causes this to become the background layer.

                            kind                    * layerkind read-write. sets the type (such as 'text layer') for an empty layer. valid only when the layer is empty and when isbackgroundlayer.

                                                     you can use the kind property to make a background layer a normal layer; however, to make a layer a background layer, you must set isbackgroundlayer to true.

                            layerMaskDensity        * double read-write. the density of the layer mask (between 0.0 and 100.0)

                            layerMaskFeather        * double read-write. the feather of the layer mask (between 0.0 and 250.0)

                            linkedlayers            * array of artlayer or layerset read-only. the layers linked to this layer. see artlayer.link.

                            name                    * string read-write. the name.

                            opacity                 * number [0.0..100.0]. read-write. the master opacity of the layer, a percentage value.

                            parent                  * document read-only. the object's container.

                            pixelsLocked            * boolean read-write. true if the pixels in the layer’s image cannot be edited using the paintbrush tool.

                            positionLocked          * boolean read-write. true if the pixels in the layer’s image cannot be moved within the layer.

                            textitem                 textitem read-only. the text item that is associated with the layer. valid only when kind = layerkind.text.

                            transparentpixelslocked * boolean read-write. true if editing is confined to the opaque portions of the layer.

                            typename                * string read-only. the class name of the referenced artlayer object.

                            vectorMaskDensity       * double read-write. the density of the vector mask (between 0.0 and 250.0)

                            vectorMaskFeather       * double read-write. the feather of the vector mask (between 0.0 and 250.0)

                            visible                 * boolean read-write. true if the layer is visible.

                            xmpMetadata             * xmpmetadata read-write. metadata for the layerb

                            */