• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script to align active layer to bottom of document

New Here ,
Jun 26, 2022 Jun 26, 2022

Copy link to clipboard

Copied

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!

TOPICS
Actions and scripting , Windows

Views

395

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Jun 26, 2022 Jun 26, 2022

Copy link to clipboard

Copied

@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:

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jun 26, 2022 Jun 26, 2022

Copy link to clipboard

Copied

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]

Photoshop_GwAI3Q3I87.jpg

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 26, 2022 Jun 26, 2022

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jun 26, 2022 Jun 26, 2022

Copy link to clipboard

Copied

- 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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 26, 2022 Jun 26, 2022

Copy link to clipboard

Copied

"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."

Personally I never had any problem when using Select All > Align. I can not recall, at least I can not remember Canvas option in CS4-5-6 or even later but that may be only my memory.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jun 26, 2022 Jun 26, 2022

Copy link to clipboard

Copied

My use case was not the very simplest, and when I used the basic alignment stuff that I tried first, another (color) layer was shifting too, which I didn't expect. I solved that by making it a Solid Color adj. layer. I forgot what problem I had next, leading me to try the script... Then that gave the described problem, after which I realized Align to Selection will always work for my use case. I just wanted to avoid the marching ants, or having to do Select All and Deselect for every realigment (I have a button for). Turns out this goes so fast you don't even see the ants flashing by.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 26, 2022 Jun 26, 2022

Copy link to clipboard

Copied

LATEST

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;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines