Skip to main content
Known Participant
April 20, 2023
Answered

Move the selection below the guide line

  • April 20, 2023
  • 1 reply
  • 854 views

How to move the selection to the bottom of the reference line, the reference line is horizontal, the coordinates of the selection are random, how to move the selection up to the bottom of the reference line through the script? Thank you 

This topic has been closed for replies.
Correct answer Stephen Marsh

Perhaps something like this?

 

/*
Move Selection Top to Horizontal Guide Coordinate.jsx
v1.0 - 20th April 2023, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/move-the-selection-below-the-guide-line/td-p/13738648
*/

#target photoshop

var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
if (activeDocument.guides.length === 1 && activeDocument.guides[0].direction == Direction.HORIZONTAL) {
    var hGuidePos = activeDocument.guides[0].coordinate.value;
    try {
        activeDocument.selection.bounds;
        var selTop = activeDocument.selection.bounds[1].value;
        var vMove = selTop - hGuidePos;
        moveSel(0, -vMove);
    } catch (e) {
        alert("There is no active selection!");
    }
} else {
    alert("There are multiple guides and or the guide is vertical!");
}
app.preferences.rulerUnits = savedRuler;

function moveSel(horizontal, vertical) {
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putProperty(s2t("channel"), s2t("selection"));
    descriptor.putReference(s2t("null"), reference);
    descriptor2.putUnitDouble(s2t("horizontal"), s2t("pixelsUnit"), horizontal);
    descriptor2.putUnitDouble(s2t("vertical"), s2t("pixelsUnit"), vertical);
    descriptor.putObject(s2t("to"), s2t("offset"), descriptor2);
    executeAction(s2t("move"), descriptor, DialogModes.NO);
}

 

 

1 reply

Stephen Marsh
Community Expert
Community Expert
April 20, 2023

Just to be clear on terminology, by reference line, do you mean guide? As in View menu > New Guide?

 

If so, will there only be one guide – or at least only one horizontal guide?

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
April 20, 2023

Perhaps something like this?

 

/*
Move Selection Top to Horizontal Guide Coordinate.jsx
v1.0 - 20th April 2023, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/move-the-selection-below-the-guide-line/td-p/13738648
*/

#target photoshop

var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
if (activeDocument.guides.length === 1 && activeDocument.guides[0].direction == Direction.HORIZONTAL) {
    var hGuidePos = activeDocument.guides[0].coordinate.value;
    try {
        activeDocument.selection.bounds;
        var selTop = activeDocument.selection.bounds[1].value;
        var vMove = selTop - hGuidePos;
        moveSel(0, -vMove);
    } catch (e) {
        alert("There is no active selection!");
    }
} else {
    alert("There are multiple guides and or the guide is vertical!");
}
app.preferences.rulerUnits = savedRuler;

function moveSel(horizontal, vertical) {
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putProperty(s2t("channel"), s2t("selection"));
    descriptor.putReference(s2t("null"), reference);
    descriptor2.putUnitDouble(s2t("horizontal"), s2t("pixelsUnit"), horizontal);
    descriptor2.putUnitDouble(s2t("vertical"), s2t("pixelsUnit"), vertical);
    descriptor.putObject(s2t("to"), s2t("offset"), descriptor2);
    executeAction(s2t("move"), descriptor, DialogModes.NO);
}

 

 

meadXAuthor
Known Participant
April 20, 2023

Thank you for your help. Thank you