Ryan
Ryan

Reputation: 24093

Photoshop script to arrange layers into grid

I have a simple PSD with a layer group called "faces", and there are 24 layers in it, each 175x175 pixels.

I want to automatically arrange those into a 6x4 grid.

When I run this script (with help from https://stackoverflow.com/a/12064474/470749), my layers each seem to get erased, sort of. They still exist, but the contents now appear empty.

How can I fix this?

// Check if we are running inside Adobe Photoshop
if (app.name !== "Adobe Photoshop") {
    alert("This script works only with Adobe Photoshop.");
} else {
    main();
}

function main() {
    var groupName = "faces";
    
    var itemWidth = 175;
    var itemHeight = 175;

    // Define the grid dimensions
    var columns = 6;
    var rows = 4;

    // Get the active document
    var doc = app.activeDocument;
    var layerSets = doc.layerSets;    
    var facesGroup = layerSets.getByName(groupName);

    // Check if the group exists
    if (facesGroup) {
        // Iterate over layers in the "faces" group
        for (var i = 0; i < facesGroup.artLayers.length; i++) {
            var currentLayer = facesGroup.artLayers[i];
            
            // Set the active layer
            doc.activeLayer = currentLayer;
            // Get a reference to the active layer
            var layer = doc.activeLayer;

              // Calculate column and row
              var column = i % columns;
              var row = Math.floor(i / columns);

              // Calculate x, y position
              var x = column * itemWidth;
              var y = row * itemHeight;
              
              // FIXNOW: Why does this not work?!
              
             MoveLayerTo(layer, x, y);
        }
        alert("Completed.");
    } else {
        alert("Group named 'faces' not found.");
    }
}

function MoveLayerTo(fLayer,fX,fY) {
  // https://stackoverflow.com/a/12064474/470749
  var Position = fLayer.bounds;
  Position[0] = fX - Position[0];
  Position[1] = fY - Position[1];

  fLayer.translate(-Position[0],-Position[1]);
}

Upvotes: 0

Views: 249

Answers (1)

Ryan
Ryan

Reputation: 24093

I think the answer was to change my Ruler setting (in Photoshop) from "%" to pixels.

I got it from the comment at https://stackoverflow.com/a/12064474/470749.

Now the code seems to work.

I made some minor improvements, but probably the original code above would have worked too (as long as Ruler is set to "px").

P.S. @Ghoul Fool commented that we could edit my code to include app.preferences.rulerUnits = Units.PIXELS; // See available units: https://theiviaxx.github.io/photoshop-docs/Photoshop/Units.html?highlight=ruler. I haven't tried it, but that looks like a great idea.


// Check if we are running inside Adobe Photoshop
if (app.name !== "Adobe Photoshop") {
    alert("This script works only with Adobe Photoshop.");
} else {
    main();
}

function main() {
    var groupName = "faces";
    
    var itemWidth = 175;
    var itemHeight = 175;

    // Define the grid dimensions
    var columns = 6;
    var rows = 4;

    // Get the active document
    var doc = app.activeDocument;
    var layerSets = doc.layerSets;    
    var facesGroup = layerSets.getByName(groupName);

    // Check if the group exists
    if (facesGroup) {
        // Iterate over layers in the "faces" group
        for (var i = 0; i < facesGroup.artLayers.length; i++) {
            var currentLayer = facesGroup.artLayers[i];
            
            // Set the active layer
            doc.activeLayer = currentLayer;
            // Get a reference to the active layer
            var layer = doc.activeLayer;

              // Calculate column and row
              var column = i % columns;
              var row = Math.floor(i / columns);

              // Calculate x, y position
              var x = column * itemWidth;
              var y = row * itemHeight;
              
              // FIXNOW: Why does this not work?!
              
             MoveLayerTo(layer, x, y);
        }
        alert("Completed.");
    } else {
        alert("Group named 'faces' not found.");
    }
}

function MoveLayerTo(fLayer, x, y) {
  // https://stackoverflow.com/a/12064474/470749
  var Position = fLayer.bounds;
  var originX = Position[0];
  var originY = Position[1];
  var offsetX = x - originX;
  var offsetY = y - originY;
  //var log = 'x,y = ' + x + ',' + y;
  //alert(log);
  
  //var log = 'offsetX,offsetY = ' + offsetX + ',' + offsetY; 
  //alert(log);

  fLayer.translate(-offsetX,-offsetY);
}

Upvotes: 0

Related Questions