Skip to main content
Participant
June 26, 2022
Question

Script to align active layer to bottom of document

  • June 26, 2022
  • 3 replies
  • 764 views

Hello all,

 

I'm trying to find what JS code can be used to very simply align the current active layer to the bottom of the canvas, exactly the same was what would happen using the "Align Bottom Edges" option through the move tool. Thank you in advance for any response, I really appreciate it!

This topic has been closed for replies.

3 replies

Chuck Uebele
Community Expert
Community Expert
June 27, 2022

Here's my version of aligning a layer to the bottom of the canvas. Just have to make sure you have the layer you want aligned selected.

#target photoshop
var oldPref = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var doc = activeDocument;
var curLay = doc.activeLayer;
curLay.translate(0, doc.height - curLay.bounds[3]);
app.preferences.rulerUnits = oldPref;
Zesty_wanderlust15A7
Known Participant
June 26, 2022

Had the same issue some days ago and had to figure out a different way...

If a tool preset would record this Canvas bit, you could have an action select that tool preset and record your alignment. You could then get away w/o a script (too). But it doesn't get recorded here... I hoped a script would help, but for me the script still needs that option set to Canvas or it throws an error. So check that out, as that tool often reverts to "Selection," not "Canvas."

[upd: Stephen's script is slightly different now than the one I tested with]

Bojan Živković11378569
Community Expert
Community Expert
June 27, 2022

Will Ctrl + A > Align Bottom Edges work for you? It can be recorded as action step.

 

"So check that out, as that tool often reverts to "Selection," not "Canvas."

Which version of Photoshop? Are you talking about same session or not?

Zesty_wanderlust15A7
Known Participant
June 27, 2022

- I redesigned my solution so I could use Align to Selection and not need a script.

- When I used the (I think slightly different) script, I was happy for some days, so I think it does remember it within the same session. Then after a week I got errors and noticed what I described (v23.3.1). AFAIK, an action does not record this change to Canvas in the Move Tool, neither does it get included in a Tool Preset.

There are probably many omissions (things not getting recorded in actions). I don't keep a list as they hardly ever fix it anyway. Recent example: I don't think an action can call a UXP panel to the front, possibly not even a script.
- Stephen told me Select All works as a fallback in case one method doesn't work. I can imagine that may not give the correct result in 100% of cases, but usually it would, so I guess that script should work. When I tested, I used a script w/o the Select All command.

Stephen Marsh
Community Expert
Community Expert
June 26, 2022

@Chris23527003pau9 – For layer content without a layer mask:

 

// Align Active Layer to Select All.jsx

// Change as required
align2SelectAll('AdBt');

function align2SelectAll(method) {
    //www.ps-scripts.com/viewtopic.php?f=66&t=7036&p=35273&hilit=align+layer#p35273
    /* 
    //macscripter.net/viewtopic.php?id=38890
    AdLf = Align Left
    AdRg = Align Right
    AdCH = Align Centre Horizontal
    AdTp = Align Top
    AdBt = Align Bottom
    AdCV = Align Centre Vertical
    */

    app.activeDocument.selection.selectAll();

    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    desc.putReference(charIDToTypeID("null"), ref);
    desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("ADSt"), charIDToTypeID(method));
    try {
        executeAction(charIDToTypeID("Algn"), desc, DialogModes.NO);
    } catch (e) {}

    app.activeDocument.selection.deselect();

}

 

https://gist.github.com/MarshySwamp/df372e342ac87854ffe08e79cbdbcbb5

 

If there is a layer mask, then try this one: