0

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]);
}
5
  • Are they empty or is the content merely moved off screen? Image > Reveal All .... anything visible after that?
    – Scott
    Commented Jan 9 at 21:15
  • Good idea. Image > Reveal All seems to have no effect, so I guess nothing was out of frame. Double-clicking each layer (smart object) takes me into the layer where the images look normal and untouched. It's only when I'm not "inside" that smart layer that they look empty. @Scott
    – Ryan
    Commented Jan 9 at 23:15
  • 1
    so.. option 2.. if you highlight one the smart objects (not the inside contents) and hit Edit > Transform (Command/Ctrl+t) what's the bounding box do? May have to zoom out to see it. I'm guessing that content got moved, especially since it's all still within the smart objects themselves. Beyond this, I'm afraid it may take direct file examination, which unfortunately we really don't do here.
    – Scott
    Commented Jan 10 at 0:40
  • @Scott I figured it out! 🤦‍♂️ Thanks for your help!
    – Ryan
    Commented Jan 10 at 13:50
  • 1
    Glad you sorted it Ryan. I had a sneaky feeling it was all due to measurements in some way.
    – Scott
    Commented Jan 10 at 15:09

1 Answer 1

1

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);
}

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.