15 Replies Latest reply on May 18, 2017 3:08 AM by Pedro Cortez Marques

    Automatically run scripts on files in a specific folder

    thomasm96501510 Level 1

      Hello all!

       

      I have a series of very effective scripts that I am running against a series of photoshop files. Now the big quiestion!

       

      Is there a program to

      1) "watch" a folder, so when a file is saved in that folder.

       

      2) It runs my script(s) on that file?

       

      If you look at it simply-- I would like to replace the "drag and drop" method used for droplets, with the "drop in the folder" method.

      No doubt this would require a separate program, to consistently check the folder for new files.

       

      This is something that I would prefer to tackle with python, but maybe you all in the community could provide me with a more "industry accepted" solution?

       

      Oh and Ideally I would run this on both mac and PC.... though I understand if windows makes more  sense for an operation like this.

       

      Best,

      Thomas Murphy

        • 1. Re: Automatically run scripts on files in a specific folder
          JJMack Most Valuable Participant

          I do not think there is a standard "Watched Folder"  than works the same on different platforms.  If you search this forum site you will find a question similar to yours in forum for several of Adobe products.  There are also questions on how the process should work.  Thing to considered like how to handle  new files being dropped in while the process is running where output is stored are process file moved out/deleted etc from the watched folder.   Should the process be started by a drop in event or on a schedule. There many thought but no standard feature that I know of.    

          • 2. Re: Automatically run scripts on files in a specific folder
            thomasm96501510 Level 1

            Can you name a program that you have used at least for mac?

            • 3. Re: Automatically run scripts on files in a specific folder
              JJMack Most Valuable Participant

              I chose to use Windows more things are available for windows compared to Mac and Linux.

              • 4. Re: Automatically run scripts on files in a specific folder
                thomasm96501510 Level 1

                What programs have you used for windows?

                • 5. Re: Automatically run scripts on files in a specific folder
                  JJMack Most Valuable Participant

                  I don't do anything like that and if a wanted to automate something like that I thing  I would just schedule a job the would check a folder on some schedule.  If I want it done immediately I would run the process rather than use a watched folder. I would also think that Mac OSX would have a scheduler like windows.

                  • 6. Re: Automatically run scripts on files in a specific folder
                    thomasm96501510 Level 1

                    yeah I know these programs exist. I'm asking for recommendations.

                    • 7. Re: Automatically run scripts on files in a specific folder
                      Pedro Cortez Marques Level 3

                      Hi Tomas

                       

                      If you manage to have a computer just to deal with it having Bridge opened on the "hotFolder", you can use Bridge to do that.

                      1. Select a folder on your network to be the "hotFolder" (you can change its name and in the code itself);
                      2. On the computer that will be the listener over the hotFolder, just put this code on the Bridge "Startup Scripts" folder and reopen Bridge
                      3. Using another computer, put some images in the folder "hotFolder" and check is the listener event handler reacts automatically.
                      4. You can then add your code in the lines (see line 25) or use the file as an argument to run other code you want;
                      5. In the end, after the file was processed, just move it or remove it;
                      6. This listener will be triggered anytime a file is added or removed or the cache is refreshed. It works only on a folder called "hotFolder";

                       

                      Hope it helps,

                       

                      var listenerHotFolder = function( event ) {
                          // IMPORTANT: $.hiresTimer > 180000 this will make sure to trigger only once on cached folders like the hot folder, because when a file is detected it trigers twice (start building cache and finishing building cache).
                          if ( event.object instanceof Document && event.type == 'loaded' && $.hiresTimer > 180000) {
                              // only works if the active folder name is "hotFolder" case sensitive
                              if (app.document.thumbnail.name.toLowerCase() == "hotfolder") {
                                  var myFiles_array = [];
                                  // if doesn't exist, creates a subfolder to move the active processing files
                                  var processingFolder = Folder(app.document.presentationPath + "\\processing");
                                  if (!processingFolder.exists) processingFolder.create();
                                  // You can change the image kind you want to deal with, or other file kind
                                  var items = Folder(app.document.presentationPath).getFiles( function(f) { return (f instanceof File && f.name.toLowerCase().match(/\.dng|cr2|jpe?g$/) != null);}); // JPG | CR2 | DNG
                                  for (var a=0 ; a < items.length ; a++) {
                                      var copied = File(decodeURI(items[a])).copy( File(File(decodeURI(items[a])).path + "/processing/" + File(decodeURI(items[a])).name ) );
                                      if (copied) {
                                          $.writeln(File(File(decodeURI(items[a])).path + "/processing/" + File(decodeURI(items[a])).name ).fsName)
                                          myFiles_array.push(File(File(decodeURI(items[a])).path + "/processing/" + File(decodeURI(items[a])).name ).fsName);
                                          File(decodeURI(items[a])).remove();
                                      }
                                  }
                                  app.document.thumbnail.refresh();
                                  // if files were dropped on the hotFolder, and were collected, any script can be running on each file.
                                  if (myFiles_array.length > 0) {
                                      for (var a=0 ; a < myFiles_array.length ; a++) {
                                          // here you can put your code
                                          
                                          // this is just an exemple to test (you can delete this later:
                                          alert("Processing the File:\n" + File(decodeURI(myFiles_array[a])).name);
                                          
                                      }
                                  }
                              }
                              //
                              return {
                                  // FALSE + block last path repetition  »»  só repete o evento numa numa pasta (não repete em F5 ou em cache rebuild) Resumo: sá actua se navegar numa nova pasta
                                  // TRUE  + block last path repetition  »»  repete em várias situações: numa nova pasta, ao criar/apagar ficheiros/pastas no folder activo, ao reordenar a primeira vez (qd cria a primeira vez o file invisivel .bridgesort)
                                  handled:true
                              }
                          }
                      }
                      app.eventHandlers.push( {handler: listenerHotFolder} );
                      
                      3 people found this helpful
                      • 9. Re: Automatically run scripts on files in a specific folder
                        thomasm96501510 Level 1

                        Opps. Seems like it's not working for some reason.. I need it to launch photoshop, and it seems to fail-- is there a way to run this script via photoshop-- or can you show me an example where you plop the code in? It seems when I place my script in here it fails to run or even open photoshop.

                        • 10. Re: Automatically run scripts on files in a specific folder
                          Pedro Cortez Marques Level 3

                          Hi

                           

                          The script is prepared to function with Bridge always active (bring to front).

                          If you are using photoshop, Bridge will loose focus and will fail.

                          That is why i said to send images to the network hotFolder from another computer and leave this one to have Bridge always on focus.

                           

                          There is also another chance of using the 2 apps. But more complex. If you make sure some how to make Bridge "bring to front" (to focus) when you need to run this listener, it should work. You can use BridgeTalk to communicate between the 2 apps to embed the other app code.

                          • 11. Re: Automatically run scripts on files in a specific folder
                            Kasyan Servetsky Level 5

                            Hi Pedro,

                             

                            This script is really awesome!    I hope you wouldn't mind if I post it on my site with some sample code of my own.

                             

                            Regards,
                            Kasyan

                            • 12. Re: Automatically run scripts on files in a specific folder
                              Pedro Cortez Marques Level 3

                              Thank you Kasyan.

                              Please do.

                               

                              All we need is to bring forward the hidden gems that Bridge still has.

                              Bridge has a lot more to give that people don't use. Specially big teams and big image data.

                              • 13. Re: Automatically run scripts on files in a specific folder
                                Kasyan Servetsky Level 5

                                Thanks, Pedro!

                                Here is the page on my site for the future generations.

                                — Kas

                                • 14. Re: Automatically run scripts on files in a specific folder
                                  brioanimation Level 1

                                  Thanks for the useful script. can this be modified to watch two (or more) folders at the same time?

                                  Help will be appreciated.

                                  • 15. Re: Automatically run scripts on files in a specific folder
                                    Pedro Cortez Marques Level 3

                                    Hi,

                                     

                                    I never tested to watch 2 hotFolders the same time, but I would prefer to have those 2 different folders inside an hotFolder and check them both and its contents.