4 Replies Latest reply on Jun 1, 2015 1:03 AM by marco mda

    Load files from a remote folder (on a server) as a stack

    marco mda

      Hello everyone, I'm back with another problem :-)

      I created* this script to check the uniformity of a bunch of files in a folder.

      *When I say "created" I mean I wrote a few lines by myself and then copied and pasted several bits from several sources. I actually don't know where this code come from since part of it was my predecessor's, apologies for not reporting the credits when needed, if any (I definitively guess so).

       

      In the end I was able to figure out how to assemble something that, given an open file in Photoshop, does this stuff:

      - Load all the other .jpg or .jpeg files currently in the parent folder of the opened one;

      - name them after their document name, stripping off the extension;

      - create 4 colour samplers and position them always in the same place, based on the file height and width;

      - evaluates the R, G and B reading against a reference (to evaluate the overall lightness and WB);

      - evaluate the L value (of Lab) samples against the other horizontal sample (to evaluate the light uniformity),

      - rename every layer with a suffix ": OK" or ": NO";

      - report all this stuff in a .txt log file on the desktop.

       

      Now for some reason this all works fine IF the folder with the images is on my hard drive (desktop in my case), but if I try with a folder on a server it freezes, like in an infinite loop.

       

      My best guess is that this is caused by the function "placeFile", that was recorded with Scripting Listener, and is the only bit I cannot clearly understand. I had to encapsulate it in a try-catch clause to make it work.

       

      Could anyone help me on this?

      I can attach the script since it does not contain any sensible information about the company I work for:

       

      #target photoshop
      if (app.documents.length) { // if length is 0 -> false, true otherwise
      var conf = confirm ("This script will delete any actual Color Sampler and create its own,\nAt the end you'll find a .txt log on your desktop and your layers renamed accordingly./nDo you want to continue?");
      if (conf===true) {
      //////////////////////////////////////////////////
      var strtRulerUnits = app.preferences.rulerUnits;
      var strtTypeUnits = app.preferences.typeUnits;
      app.preferences.rulerUnits = Units.PIXELS;
      app.preferences.typeUnits = TypeUnits.PIXELS;
      //////////////////////////////////////////////////
      var docRef = app.activeDocument;
      var H = docRef.height
      var W = docRef.width
      var H25 = Math.round (H*0.25);
      var H65 = Math.round(H*0.65);
      var W12 = Math.round(W*0.12);
      var W88 = Math.round(W*0.88);
      //////////////////////////////////////////////////
      //////////////////////////////////////////////////
      //// LOAD ALL JPG or JPEG FILES IN FOLDER ////
      //////////////////////////////////////////////////
      //////////////////////////////////////////////////
      var docRef = activeDocument;
      var sourceFolder = Folder (docRef.path);
      try{
         // declare local variables
                      var fileArray = new Array();
                      var extRE = /\.(?:jpg|jpeg)$/i;
      
      
                      // get all files in source folder
                      var docs = sourceFolder.getFiles();
                      var len = docs.length;
                      for (var i = 0; i < len; i++) {
                                      var doc = docs[i];
      
      
                                      // only match files (not folders)
                                      if (doc instanceof File) {
                                                      // store all recognized files into an array
                                                      var docName = doc.name;
                                                      if (docName.match(extRE)) {
                                                                      fileArray.push(doc);
                                                      }
                                                  }
                                              }
                                  }                            
                            catch(e) {}
      var docname = (String(docRef.name));
      var delimeter = '.';
      var splitted = docname.split(delimeter);         
      docRef.activeLayer.name = (splitted[0]);
      var mainlayer = docRef.activeLayer;
      var numberfile = 1;
      while (numberfile <= fileArray.length){
      ///////////call 'placeFile' function///////////                    
                          placeFile (fileArray[numberfile] );
                                          var layer = docRef.activeLayer;
                                          var numberfile = (numberfile+1);
              }
      app.activeDocument.rasterizeAllLayers()
      /////////////Move the Ref file to the bottom /////////////////////////////////////
      var lyrs = docRef.artLayers;
      for (var l=0; l< lyrs.length ; i++){
          if (lyrs[l].name === "WW REF"){
                  lyrs[l].move (docRef, ElementPlacement.PLACEATEND);
      break;   
              }
          }
      ///////////////////////////////////////////////////////////////////////
      //////FUNCTION TO PLACE FILES FROM PARENT FOLDER AS LAYERS////////
      ///////////////////////////////////////////////////////////////////////
      function placeFile(file) {
      //Doesn't work without the try-catch 
       try {
      /*     function charIDToTypeID(s) { return app.charIDToTypeID(s); };
        function sTID(s) { return app.stringIDToTypeID(s); };
          var desc124 = new ActionDescriptor();
          desc124.putPath( charIDToTypeID('null'), new File(file) );
          desc124.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
              var desc125 = new ActionDescriptor();
              desc125.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 );
              desc125.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 );
          desc124.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc125 );
          desc124.putBoolean( charIDToTypeID('AntA'), true );
          executeAction( charIDToTypeID('Plc '), desc124, DialogModes.NO );*/
          ///NEW RECORDING///
      var idPlc = charIDToTypeID( "Plc " );
          var desc146 = new ActionDescriptor();
          var idnull = charIDToTypeID( "null" );
          desc146.putPath( idnull, new File( file ) );
          var idFTcs = charIDToTypeID( "FTcs" );
          var idQCSt = charIDToTypeID( "QCSt" );
          var idQcsa = charIDToTypeID( "Qcsa" );
          desc146.putEnumerated( idFTcs, idQCSt, idQcsa );
          var idOfst = charIDToTypeID( "Ofst" );
              var desc147 = new ActionDescriptor();
              var idHrzn = charIDToTypeID( "Hrzn" );
              var idPxl = charIDToTypeID( "#Pxl" );
              desc147.putUnitDouble( idHrzn, idPxl, 0.000000 );
              var idVrtc = charIDToTypeID( "Vrtc" );
              var idPxl = charIDToTypeID( "#Pxl" );
              desc147.putUnitDouble( idVrtc, idPxl, 0.000000 );
          var idOfst = charIDToTypeID( "Ofst" );
          desc146.putObject( idOfst, idOfst, desc147 );
      executeAction( idPlc, desc146, DialogModes.NO );    
          }
      catch(e){};
      };
      //////////////////////////////////////////////////
      //////////////////////////////////////////////////
      //////////////////////////////////////////////////
      var lyrArray = docRef.layers
      var lyrNo = lyrArray.length;
      var lyrRef = docRef.activeLayer;
      var noLayers = activeDocument.artLayers.length;
      ////MAKE ALL LAYERS VISIBLE
        try {
            for(var a = 0; a<lyrNo; a++){   
          activeDocument.layers[a].visible=true;  
         };
       } catch(e) {}
      //////////////////////////////////////////////////
      //DODGY WAY TO SELECT TOPMOST LAYER//////////
      //////////////////////////////////////////////////
      var ghostLyr = docRef.artLayers.add()
      ghostLyr.move (docRef, ElementPlacement.PLACEATBEGINNING)
      docRef.activeLayer.remove()
      //////////////////////////////////////////////////
      var okno = 0;
      ///remove all the color samplers
      docRef.colorSamplers.removeAll()
      //////////////CREATE THE SAMPLES AND POSITION THEM
      var sampler1 = docRef.colorSamplers.add ([W12, H25]);
      var sampler2 = docRef.colorSamplers.add ([W88, H25]);
      var sampler3 = docRef.colorSamplers.add ([W12, H65]);
      var sampler4 = docRef.colorSamplers.add ([W88, H65]);
      var alertMessage = "=================================================================\n#1 = top left, #2 = top right, #3 = bottom left, #4= bottom right\n=================================================================\n\n\n";
      /////////////MAIN FUNCTION
      function createPositionReport(H25, H65, W12, W88) {
      var colorSamplersArray = app.activeDocument.colorSamplers;
      var colorSamplerLength = colorSamplersArray.length;
      if (lyrRef.isBackgroundLayer){
      var docname = (String(docRef.name));
      var delimeter = '.';
      var splitted = docname.split(delimeter);         
      lyrRef.name = (splitted[0]);}
      ///////////EVALUATE THE SAMPLES ONE BY ONE AND REPORT
      for (var i = 0; i < colorSamplerLength; i++) {
          var R = Math.round(colorSamplersArray[i].color.rgb.red);
          var G = Math.round(colorSamplersArray[i].color.rgb.green); 
          var B = Math.round(colorSamplersArray[i].color.rgb.blue);
          var alert1 = ("#" +(i+1)+" ");   
          var alert2 = (" "+"R="+R+" G="+ G+" B="+B+"\n");
                  if (R>=240 && R<=248 && G>=240 && G<=248 && B>=242 && B<=250) {
                        alertMessage+= (alert1+"good:" +alert2);
                           okno++       
                      };
                     else {
                           alertMessage+= (alert1+"NOT good:" +alert2);
                            };
      };
      //////////////////CHECK THE UNIFORMITY AND REPORT
      var colorSamplersArray = app.activeDocument.colorSamplers;
      var L1 = Math.round(colorSamplersArray[0].color.lab.l);
      var L2 = Math.round(colorSamplersArray[1].color.lab.l);
      var L3 = Math.round(colorSamplersArray[2].color.lab.l);
      var L4 = Math.round(colorSamplersArray[3].color.lab.l);
              if (Math.abs(L1-L2)>3 || Math.abs(L3-L4)>3) {
                     alertMessage +=("\nThe light is NOT properly centered on the subject.\n"+"#1 L = "+L1+"     #2 L = "+L2+"\n"+"#3 L = "+L3+"     #4 L = "+L4+"\n\n");
            }
              else {okno++ 
                    alertMessage +=("\nThe uniformity seems to be good!\n"+"#1 L = "+L1+"     #2 L = "+L2+"\n"+"#3 L = "+L3+"     #4 L = "+L4+"\n\n")
                     }
      var today = new Date();
      var dd = today.getDate();
      var mm = today.getMonth()+1; //January is 0!
      var yyyy = today.getFullYear();
      var preferancelog = new File("~/Desktop/Lighting_Test_Report_"+dd+"-"+mm+"-"+yyyy+".txt");
      preferancelog.open('w');
      preferancelog.writeln(alertMessage);
      };
      for (y=0; y<(lyrNo); y++) {
      okno = 0;    
      alertMessage += ("==========>   "+lyrArray[y].name+"   <=========="+"\n\n");
      createPositionReport(H25, H65, W12, W88);
          if (okno == 5){lyrArray[y].name += ": OK";
              alertMessage += ("==========>   "+lyrArray[y].name+"   <=========="+"\n\n\n\n");}
          else{lyrArray[y].name += ": NO";
              alertMessage += ("==========>   "+lyrArray[y].name+"   <=========="+"\n\n\n\n");}
      lyrArray[y].visible = false ;
      };
      };
      }
      else {
          alert ("You have no open documents")}
      

       

      If this looks patchy to you please bear in mind I just started with JS and this is real copy-paste-adapt from different sources.

      I tried as well to look at Photoshop default script to load files as layers but I cannot understand it...

      Thank you for all your help!

      Marco

        • 1. Re: Load files from a remote folder (on a server) as a stack
          c.pfaffenbichler Level 9

          If you include the line

          $.writeln(numberfile+"_"+fileArray[numberfile]+"_"+File(fileArray[numberfile]).exists)

          in the while-clause can you observe the progress in ESTK’s JavaScript Console?

          • 2. Re: Load files from a remote folder (on a server) as a stack
            marco mda Level 1

            Hi Christoph, yes I can.

            Here's the result:

             

            1_/asos/Studios%201-11/Lighting%20Tests/WW/CHECKED/04-APRIL_2015/010415/Studio%207.jpg_tru e

            2_/asos/Studios%201-11/Lighting%20Tests/WW/CHECKED/04-APRIL_2015/010415/Studio%208.jpg_tru e

            3_/asos/Studios%201-11/Lighting%20Tests/WW/CHECKED/04-APRIL_2015/010415/studio%209.jpg_tru e

            4_/asos/Studios%201-11/Lighting%20Tests/WW/CHECKED/04-APRIL_2015/010415/STUDIO%20TEN.jpg_t rue

            5_/asos/Studios%201-11/Lighting%20Tests/WW/CHECKED/04-APRIL_2015/010415/STUDIO5.jpg_true

            6_undefined_false

             

            The first file open (studio 6) it's the only one missing from here.

            Should I find a way to exclude it from the "placeFile" iteration?

            • 3. Re: Load files from a remote folder (on a server) as a stack
              DBarranca Level 4

              Hi Marco, try substituting line 64:

               

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

               

              with:

               

              for (var l=0; l< lyrs.length ; l++){
              
              

               

              ("l++" in place of "i++", which causes the loop to never quit);

               

              That done, on my test (three jpgs on a server folder: 1.jpg, 2.jpg, 3.jpg) it still endlessly loops, creating layers named such as "1: NO: NO: NO" but this might be because I've seen in your code you look for layers named "WW REF" which I haven't - so it possibly work on your side with your actual production files, following your own naming convention.

               

              (UPDATE - no, it doesn't endlessly loop, I must have done something weird myself while debugging - it outputs a Lighting_Test_Report_29-5-2015.txt file and renames the layers as 1: NO, 2: NO, 3: NO - so I'd say it works as expected)

               

              Cheers,

               

              Davide Barranca

              ---

              www.davidebarranca.com

              www.cs-extensions.com

              • 4. Re: Load files from a remote folder (on a server) as a stack
                marco mda Level 1

                Something as stupid as typing a wrong counter.... Thanks a lot!!!

                Now it seems fine, I'll test this thoroughly in the next days and if anything happens I'll post again.

                Thanks all for the help!

                MD