title: Photoshop scripting crop away x pixels from every side of document
body: <p>I have a lot of images like this one which need to be processed as a batch:</p>
  
  <p><a href="https://i.stack.imgur.com/EcF9D.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EcF9D.jpg" alt="unprocessed image"></a></p>
  
  <p>What I'm trying to accomplish is to crop away the border on every side, but here's the catch: trimming doesn't work because the color in every corner of the image is not black but white, so instead I'm trying to crop away about 5 pixels on every side. I've written some code to do just that but there seems to be an error somewhere resulting in funny, mirrored and wrongly cropped images:</p>
  
  <pre><code>function cropLayerToBounds(){
  var layer = activeDocument.activeLayer; //Grab the currently selected layer
  
  // get height and width
  var actualHeight = layer.bounds[2]-layer.bounds[0]; //Grab the height
  var actualWidth = layer.bounds[3]-layer.bounds[1]; //Grab the width
  
  // calculate desired height and width
  var desiredHeight = actualHeight - 5;
  var desiredWidth = actualWidth - 5;
  
  // not sure if necessary
      var doc = app.activeDocument;
      var halfWidth = (desiredWidth/2);
      var halfHeight = (desiredHeight/2);
      var centerX = (doc.width/2);
      var centerY = (doc.height/2);
  
  // error seems to be here
  
     // tried this
     var bounds = [0,0,desiredHeight,desiredWidth];
  
     // and this
     var bounds = [(centerX-halfWidth),(centerY-halfHeight),(centerX+halfWidth),(centerY+halfHeight)];
  
      doc.crop(bounds);
  
  
  }
  </code></pre>
  
  <p>The images i process this way look somewhat like this:</p>
  
  <p><a href="https://i.stack.imgur.com/khpOU.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/khpOU.jpg" alt="processed image"></a></p>