1

I'd like to have a script that moves one of two layer in a photoshop session by one pixel, save the output as a png and repeat. Here's my attempt (it complains that transform is not avaialable, but I'm using CS6):

// Save selected layer to variable:

app.preferences.rulerUnits = Units.PIXELS;

var docRef = activeDocument;
var layerRef = docRef.activeLayer;

for (var dy = 1; dy < 20 ; dy++) {

MoveLayerTo(layerRef,-1,0);

        var opts;
        opts = new ExportOptionsSaveForWeb();
        opts.format = SaveDocumentType.PNG;
        opts.PNG8 = false;
        opts.quality = 100;
        var suffix = zeroPad(dy,5)
    var pngFile = new File("/path/to/file/"+suffix+"filename.png");

    //Folder path for new images

    docRef.exportDocument (pngFile, ExportType.SAVEFORWEB, opts);

}


function zeroPad(n, s) {   
   n = n.toString();   
   while (n.length < s)  n = '0' + n;   
   return n;   
};  

//******************************************
// MOVE LAYER TO
// Author: Max Kielland
//
// Moves layer fLayer to the absolute
// position fX,fY. The unit of fX and fY are
// the same as the ruler setting. 

function MoveLayerTo(fLayer,fX,fY) {

  var Position = fLayer.bounds;
  Position[0] = fX - Position[0];
  Position[1] = fY - Position[1];

  fLayer.translate(-Position[0],-Position[1]);
}
3
  • Is there some reason this HAS to to a script, as opposed to an action? You could run the action against batches of files, and it would be so much easier to accomplish. Commented Mar 29, 2018 at 7:19
  • The only error I got was the file path. After I made sure it leads to a real path, no more errors. Don't know what is going on. It doesn't seem like the transform is working the way you want it to work, but I didn't get any errors at least (I am using CC...). — I couldn't find anything there that would prevent it from working in older versions. That said... Since it's technically a static value that you need to move. I'd use the Scripting Listener plugin to output code for the "Move layer 1px" method: Cmd + Arrow keys.
    – Joonas
    Commented Mar 29, 2018 at 7:39
  • I've got it to work, but has mentioned by @Joonas the translate doesn't do what I want. So, how can I perform the shift of one layer?
    – Three Diag
    Commented Mar 31, 2018 at 12:34

1 Answer 1

1

Here's how I fixed it

// Save selected layer to variable:

app.preferences.rulerUnits = Units.PIXELS;


for (var dy = 1; dy < 20 ; dy++) {

activeDocument.activeLayer.translate(-1,0);


        var opts;
        opts = new ExportOptionsSaveForWeb();
        opts.format = SaveDocumentType.PNG;
        opts.PNG8 = false;
        opts.quality = 100;
        var suffix = zeroPad(dy,5)
    var pngFile = new File("/path/to/file/"+suffix+"filename.png");

    //Folder path for new images

    docRef.exportDocument (pngFile, ExportType.SAVEFORWEB, opts);

}


function zeroPad(n, s) {   
   n = n.toString();   
   while (n.length < s)  n = '0' + n;   
   return n;   
};  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.