0 Replies Latest reply on Apr 5, 2013 12:21 PM by Mark Ashton

    Help with PS Javascript.

    Mark Ashton Level 1

      Hey,

       

      So I have compiled a script (with a lot of help from the community here!) and it works half the time and the other half it doesn't and I can't seem to isolate the problem.

       

      Basically, i have lots of different images with irregular filenames and they all have duplicates with a lower exposure. The script loops through open documents, looks for the duplicate image by compareing the shoot time in the EXIF data, works out which one is lighter by getting the mean value and pastes the lighter one ontop of the dark image, closes the light document and moves.

       

      Like I said it works perfectly half the time and then other times it does nothing!

       

      Here is the script;

       

       

      var docRef = activeDocument;
      var alldocs = app.documents;
      
      
      
      
      for (var i = 0; i < app.documents.length; i++) {
      try {
      var thedoc = alldocs[i];
           
      var thedata = thedoc.info.exif;
      var thetime = thedata[12];
      
      
      ////// get mean
      var histo = thedoc.histogram;
      var mean = 0;
      var total = 0;
      for (var n = 0; n < histo.length; n++) {
      total = total + histo[n];
      };
      for (var m = 0; m < histo.length; m++) {
      var thisValue = histo[m];
      mean = mean + (m * thisValue / total);
      };
      var alldocs = app.documents;
      
      
           for (var i = 0; i < alldocs.length; i++) {
               
               
               
             if (String(thedoc) == String(alldocs[i])){
                 /////alert (alldocs[i]);
                 }
             else{
                  var tempdata = alldocs[i].info.exif;
                  var temptime = tempdata[12];
                  
                  ////// get mean
              var histo = alldocs[i].histogram;
              var tempmean = 0;
              var total = 0;
              for (var n = 0; n < histo.length; n++) {            
              total = total + histo[n];
              };
              for (var m = 0; m < histo.length; m++) {
              var thisValue = histo[m];
              tempmean = tempmean + (m * thisValue / total);
              };
          
                  
                  if (String(temptime) == String(thetime)){
                      
                      try{
                          if (tempmean > mean ){
                              activeDocument = alldocs[i];
                              var tempdoc = activeDocument;
                              activeDocument.activeLayer.name = "light";
                              newlayermask () ;
                              var thelayer = activeDocument.activeLayer;
                              thelayer.duplicate(thedoc, ElementPlacement.PLACEATBEGINNING);
                                     
                              //////
                              ////////
                              
                              
                              activeDocument = tempdoc;
                              tempdoc.close(SaveOptions.DONOTSAVECHANGES);
                              break;
                              
                          }
                      else if (tempmean < mean ){
                              activeDocument = thedoc;
                              activeDocument.activeLayer.name = "light";
                              newlayermask () ;
                              var thelayer = activeDocument.activeLayer;
                              var tempdoc = alldocs[i];
                              thelayer.duplicate(tempdoc, ElementPlacement.PLACEATBEGINNING);      
                              
                              //////
                              
                              ////////
                              
                              activeDocument = thedoc;
                              thedoc.close(SaveOptions.DONOTSAVECHANGES);
                              break;
                          }
                          }
                      catch (e){}
                      }
                      
                      
                 }
             }
         }
         
         catch(e){alert(e);
             break;}
         }
             
             
             
             
         
         
         
         
         ///////////////// FUNCTIONS
         
         
      function  newlayermask () {
      var idMk = charIDToTypeID( "Mk  " );
          var desc124 = new ActionDescriptor();
          var idNw = charIDToTypeID( "Nw  " );
          var idChnl = charIDToTypeID( "Chnl" );
          desc124.putClass( idNw, idChnl );
          var idAt = charIDToTypeID( "At  " );
              var ref88 = new ActionReference();
              var idChnl = charIDToTypeID( "Chnl" );
              var idChnl = charIDToTypeID( "Chnl" );
              var idMsk = charIDToTypeID( "Msk " );
              ref88.putEnumerated( idChnl, idChnl, idMsk );
          desc124.putReference( idAt, ref88 );
          var idUsng = charIDToTypeID( "Usng" );
          var idUsrM = charIDToTypeID( "UsrM" );
          var idRvlA = charIDToTypeID( "RvlA" );
          desc124.putEnumerated( idUsng, idUsrM, idRvlA );
      executeAction( idMk, desc124, DialogModes.NO );
      }
      

       

       

      If anyone can show me where I could be going wrong or point in the right direction that would be much appreciated!

       

      Thanks,

       

      Mark