Skip navigation
funreizer
Currently Being Moderated

Script to Open Folder Automatically?

Sep 10, 2012 4:45 AM

Good day,

 

I've been searching the net for 2 days for something probably simple, but can't find it.

 

Is there a script that can open all files within a folder automatically?

 

What I want to do is to have Photoshop open a folder full of files, save each one for web. I can automate save for web part with actions. but can't find a way to import a folder.

 

Image processor is great, except one thing that annoys the hell out of me which is dialog popup. So I'm looking for script that automatically, without any dialog boxes, opens up all files within a folder.

 

I found this:

 

 

var dir = Folder('Q:/Images');

var selectedFile = dir.openDlg("Select Image" , "Select:*.nef;*.cr2;*.crw;*.dcs;*.raf;*.arw;*.orf;*.dng;*.psd;*.tif;* .jpg;*.png;*.bmp");

if( selectedFile !=null ){open(File(selectedFile));}

 

but it's a prompt. Is there some script that just loads a folder with files automatically?

 

thanx

 
Replies
  • JJMack
    5,991 posts
    Jan 9, 2006
    Currently Being Moderated
    Sep 10, 2012 6:43 AM   in reply to funreizer

    I'm sure if you look at a script like the Image Processor that ships with Photoshop you will see code to do that. You can just point it at a folder and it will process all the image files it it. There is also a option to process sub foldere so it may show you how to use recursion.

     

    I quick look I found this in Image Processor

     

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

    // Function: FindAllFiles

    // Usage: Find all the files in the given folder recursively

    // Input: srcFolder is a string to a folder

    //                      destArray is an Array of File objects

    // Return: Array of File objects, same as destArray

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

    function FindAllFiles( srcFolderStr, destArray ) {

              var fileFolderArray = Folder( srcFolderStr ).getFiles();

     

     

              for ( var i = 0; i < fileFolderArray.length; i++ ) {

                        var fileFoldObj = fileFolderArray[i];

                        if ( fileFoldObj instanceof File ) {

                                  destArray.push( fileFoldObj );

                        } else { // folder

                                  FindAllFiles( fileFoldObj.toString(), destArray );

                        }

              }

     

     

              return destArray;

    }

     

    To do what you want I've used code that looks like this

     

    // Loop Image File Start

                                  var fileList = inputFolder.getFiles(/\.(nef|cr2|crw|dcs|raf|arw|orf|dng|jpg|tif|psd| eps|png|bmp)$/i);

                                  // Loop through files

                                  for (var i = 0; i < fileList.length; i++) {

     

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 10, 2012 6:34 AM   in reply to funreizer

    The folder object has a getFiles() method which you can pass a regexp to filter otherwise it will return all files… Lots of samples of this here in this forum…

     
    |
    Mark as:
  • JJMack
    5,991 posts
    Jan 9, 2006
    Currently Being Moderated
    Sep 10, 2012 6:57 AM   in reply to funreizer

    Files can be opened, placed, executed, deleted etc in my loop I was placeing files not opening

     

     

                                  // Loop Image File Start

                                  var fileList = inputFolder.getFiles(/\.(nef|cr2|crw|dcs|raf|arw|orf|dng|jpg|tif|psd| eps|png|bmp)$/i);

                                  // Loop through files

                                  for (var i = 0; i < fileList.length; i++) {

                                            // Only process the returned file objects

                                            // The filter 'should' have missed out any folder objects

                                            //  get next image file

                                            if (fileList[i] instanceof File) {

                                                      //alert(fileList[i]);

                                                      var layers = activeDocument.layers;

                                                      activeDocument.activeLayer = layers[layers.length-1-imageNumber+1];           // Target Background Layer or last placed image

     

                                                      placeImage(fileList[i]);                                                      // Place in Image   you would open instead of place

     

                                                      // Get Smart Object current width and height


     
    |
    Mark as:
  • JJMack
    5,991 posts
    Jan 9, 2006
    Currently Being Moderated
    Sep 10, 2012 7:41 AM   in reply to funreizer

    If you know the folder you do not need to put up a select dialog just  process the folder you know.  If you keep on loading image you may start boging down the system. Here some code I used to open files flatten then resize them to tile size copy to clipboard close no save then paste them into place into a new document I'm creating to print images on roll paper.

     

     

    var currrow=0; var pasted=0;

                        for (var i=0;i<file.length;i++) {

                                  app.load(file[i]); //load it into documents

                                  var backFile= app.activeDocument; //prepare your image layer as active document

                                  flatten(); //handle layered images

                                  if (backFile.width.value<backFile.height.value&&width>height ) { backFile.rotateCanvas(-90.0);  } // Rotate portraits

                                  if (backFile.height.value<backFile.width.value&&height>width ) { backFile.rotateCanvas(-90.0);  } // Rotate landscapes

                                  if (backFile.width.value/backFile.height.value > width/height) { backFile.resizeImage(null, height, null, ResampleMethod.BICUBIC); } // wider

                                  else {backFile.resizeImage(width, null, null, ResampleMethod.BICUBIC);} // same aspect ratio or taller

                                  backFile.selection.selectAll();

                                  backFile.selection.copy(); //copy resized image into clipboard

                                  backFile.close(SaveOptions.DONOTSAVECHANGES); //close image without saving changes

     

                                  for (var n=0;n<copies;n++) {

     

     

                                            var x =pasted*width;

                                            var y =currrow*height;

                                            var selectedRegion = Array(Array(x,y), Array(x+width,y), Array(x+width,y+height), Array(x,y+height));

     

     

                                            doc.selection.select(selectedRegion);

                                            doc.paste(true); //paste image into masked layer your document

                                            doc.selection.select(selectedRegion);

                                            align('AdCH'); align('AdCV');

                                            doc.selection.deselect();

                                            pasted++

                                            if ( pasted==cols ) { pasted=0; currrow++; }

                                            }

                                  }

     

     

    var inputFolder = "~/My Documents/My Pictures/";

    var fileList = inputFolder.getFiles(/\.(nef|cr2|crw|dcs|raf|arw|orf|dng|jpg|tif|psd| eps|png|bmp)$/i);

     

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 11, 2012 4:11 AM   in reply to funreizer

    Try this:

     

    var inputFolder = Folder( Folder.desktop + '/Output_Files' ); // Make sure this is correct for your hard coded path???
     
    var fileList = inputFolder.getFiles(/\.(nef|cr2|crw|dcs|raf|arw|orf|dng|jpg|tif|psd| eps|png|bmp)$/i);
     
    alert( fileList.length );
     
    for ( var i = 0; i < fileList.length; i++ ) {
        
        app.open( fileList[i] );
        
    };
    
     
    |
    Mark as:
  • JJMack
    5,991 posts
    Jan 9, 2006
    Currently Being Moderated
    Sep 11, 2012 6:05 AM   in reply to funreizer

    Yes I was to fast with the fingers heet to be a folder object use code like  Muppet Mark  does not have to ne on the desktop and you maght want to test ir see if it exists

    var testFolder = new Folder("~/My Documents/My Pictures");

                        if (!testFolder.exists) {

     
    |
    Mark as:
  • JJMack
    5,991 posts
    Jan 9, 2006
    Currently Being Moderated
    Sep 11, 2012 8:27 AM   in reply to funreizer

    Remember if there are a great number of large images in the folder that script will tax your system....

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points