1 Reply Latest reply on Oct 29, 2010 7:33 AM by Michael L Hale

    Trim error

    Ghoulfool Level 1

      Greeting scripters!

      Is there a way of avoiding the error "Could not trim because this would create an empty documents" in the probably occurance that a blank (single colour) document has been selected (and under my trim function would be reduce it to width of zero). At the mome the script pops up the error and does not process the file. However I need to process those files to a specific width for blank files.

      And my error catchment didn't work either

       

      try {
      app.activeDocument.trim(TrimType.TOPLEFT, false, true, false, true);
          } catch (myError)
             {
         alert ("time to ignore this.\r" + myError);
             }

       

      The other alternative is a way to find out if the document is blank (a single colour)- which may unnecessay checking of every pixel in every file - unless there is a better way of doing that.

       

      Cheers

        • 1. Re: Trim error
          Michael L Hale Level 5

          I think you will need to check if the doc has only one color. You don't have to check every pixel, just search the histogram. The function below work with RGB mode. You may need to localize the channel names if you don't use the English version of Photoshop.

           

          function hasSingleColor() {
              var res = false;
              var rArray = activeDocument.channels["Red"].histogram.toString().replace(/0,|,0/g,"").split(',');
              var gArray = activeDocument.channels["Green"].histogram.toString().replace(/0,|,0/g,"").split(',');
              var bArray = activeDocument.channels["Blue"].histogram.toString().replace(/0,|,0/g,"").split(',');
              if(rArray.length == 1 && gArray.length == 1 && bArray.length == 1 ){
                  res = true;// has single non black color
              }
              if(rArray.length == 0 && gArray.length == 0 && bArray.length == 0 ){
                  res = true;// has black as single color
              }
              return res;
          };