Copy link to clipboard
Copied
In Photoshop, when I select two layers, I want their images' coordinates to swap. For example, if Image A is at coordinate 0 and Image B is at coordinate 100, how can I move Image A to coordinate 100 and Image B to coordinate 0?
I found similar code in the forum, but it doesn't work when I run it. I've selected two layers, but running the code has no effect
Thank you all for your help
#target photoshop
app.bringToFront();
var doc = app.activeDocument;
var selectedLayers = getSelectedLayersIndex(doc);
if (selectedLayers.length == 2) {
try {
for (var i = 0; selectedLayers.length > i; i++) {
var sLayers = new Array();
for (var i = 0, l = selectedLayers.length; i < l; i++) {
selectLayerByIndex(selectedLayers, false);
if (app.activeDocument.activeLayer.typename == "LayerSet") {
executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO);
var xleft = activeDocument.activeLayer.bounds[0]
var ytop = activeDocument.activeLayer.bounds[1];
var ybot = activeDocument.activeLayer.bounds[3];
var width = activeDocument.activeLayer.bounds[2] - activeDocument.activeLayer.bounds[0];
var height = activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1];
var xcenter = activeDocument.activeLayer.bounds[0] + width / 2
var ycenter = activeDocument.activeLayer.bounds[1] + height / 2
var xright = activeDocument.activeLayer.bounds[2]
app.activeDocument.activeHistoryState = app.activeDocument.historyStates[app.activeDocument.historyStates.length - 2];
selectLayerByIndex(selectedLayers, false);
} else {
var xpos = activeDocument.activeLayer.bounds[0];
var ytop = activeDocument.activeLayer.bounds[1];
var ybot = activeDocument.activeLayer.bounds[3];
var width = activeDocument.activeLayer.bounds[2] - activeDocument.activeLayer.bounds[0];
var height = activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1];
var xcenter = activeDocument.activeLayer.bounds[0] + width / 2
var ycenter = activeDocument.activeLayer.bounds[1] + height / 2
var xleft = activeDocument.activeLayer.bounds[0]
var xright = activeDocument.activeLayer.bounds[2]
}
sLayers.push([activeDocument.activeLayer, xleft, ytop, width, height, xcenter, ycenter, xright, ybot]);
}
var oneWidth = sLayers[0][3]
var oneHeight = sLayers[0][4]
var oneXcenter = sLayers[0][5]
var oneYcenter = sLayers[0][6]
var twoWidth = sLayers[1][3]
var twoHeight = sLayers[1][4]
var twoXcenter = sLayers[1][5]
var twoYcenter = sLayers[1][6]
var layer1 = doc.activeLayer = sLayers[0][0]
if (oneXcenter <= twoXcenter) Xdelta = twoXcenter - oneXcenter;
else Xdelta = -twoXcenter + oneXcenter;
if (oneYcenter <= twoYcenter) Ydelta = twoYcenter - oneYcenter;
else Ydelta = -twoYcenter + oneYcenter;
if (oneWidth <= twoWidth) percentWidth = twoWidth / oneWidth * 100;
else percentWidth = oneWidth / twoWidth * 100;
if (oneHeight <= twoHeight) percentHeight = oneHeight / twoHeight * 100;
else percentHeight = twoHeight / oneHeight * 100;
transform(Xdelta, Ydelta, percentWidth, percentHeight);
var layer2 = doc.activeLayer = sLayers[1][0]
if (twoXcenter <= oneXcenter) Xdelta = twoXcenter - oneXcenter;
else Xdelta = -twoXcenter + oneXcenter;
if (twoYcenter <= oneYcenter) Ydelta = twoYcenter - oneYcenter;
else Ydelta = -twoYcenter + oneYcenter;
if (twoWidth <= oneWidth) percentWidth = twoWidth / oneWidth * 100;
else percentWidth = oneWidth / twoWidth * 100;
if (twoHeight <= oneHeight) percentHeight = oneHeight / twoHeight * 100;
else percentHeight = twoHeight / oneHeight * 100;
transform(Xdelta, Ydelta, percentWidth, percentHeight);
///////////////////////////////////////////////////////
for (var i = 0, l = selectedLayers.length; i < l; i++) {
selectLayerByIndex(selectedLayers, true);
}
}
} catch (e) {
alert(e + ': on line ' + e.line, 'Script Error', true);
}
}
function transform(DeltaX, DeltaY, WidthPercent, HeightPercent) {
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
descriptor.putEnumerated(stringIDToTypeID("freeTransformCenterState"), stringIDToTypeID("quadCenterState"), stringIDToTypeID("QCSAverage"));
descriptor2.putUnitDouble(stringIDToTypeID("horizontal"), stringIDToTypeID("pixelsUnit"), DeltaX);
descriptor2.putUnitDouble(stringIDToTypeID("vertical"), stringIDToTypeID("pixelsUnit"), DeltaY);
descriptor.putObject(stringIDToTypeID("offset"), stringIDToTypeID("offset"), descriptor2);
descriptor.putUnitDouble(stringIDToTypeID("width"), stringIDToTypeID("percentUnit"), WidthPercent);
descriptor.putUnitDouble(stringIDToTypeID("height"), stringIDToTypeID("percentUnit"), HeightPercent);
try {
executeAction(stringIDToTypeID("transform"), descriptor, DialogModes.NO);
} catch (e) {
alert(e + ': on line ' + e.line, 'Script Error', true);
}
}
function getSelectedLayersIndex(doc) {
var selectedLayers = [];
var ref = new ActionReference();
ref.putEnumerated(cTID('Dcmn'), cTID('Ordn'), cTID('Trgt'));
var desc = executeActionGet(ref);
if (desc.hasKey(sTID('targetLayers'))) {
desc = desc.getList(sTID('targetLayers'));
var c = desc.count;
for (var i = 0; i < c; i++) {
try {
doc.backgroundLayer;
selectedLayers.push(desc.getReference(i).getIndex());
} catch (e) {
selectedLayers.push(desc.getReference(i).getIndex() + 1);
}
}
} else {
var ref = new ActionReference();
ref.putProperty(cTID('Prpr'), cTID('ItmI'));
ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
try {
doc.backgroundLayer;
selectedLayers.push(executeActionGet(ref).getInteger(cTID('ItmI')) - 1);
} catch (e) {
selectedLayers.push(executeActionGet(ref).getInteger(cTID('ItmI')));
}
}
return selectedLayers;
}
function selectLayerByIndex(index, add) {
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref);
if (add) desc.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection"));
desc.putBoolean(charIDToTypeID("MkVs"), false);
try {
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
} catch (e) {}
}
function cTID(s) {
return app.charIDToTypeID(s);
}
function sTID(s) {
return app.stringIDToTypeID(s);
}
Try this
#target photoshop
app.bringToFront();
var doc = app.activeDocument;
var selectedLayers = getSelectedLayersIndex(doc);
if (selectedLayers.length == 2) {
try {
for (var i = 0; selectedLayers.length > i; i++) {
var sLayers = new Array();
for (var i = 0, l = selectedLayers.length; i < l; i++) {
selectLayerByIndex(selectedLayers[i], false);
if (app.activeDocument.activeLayer.typename == "LayerSet") {
execu
...
Copy link to clipboard
Copied
Layers have a bounds property which is four points. There is no single "coordinate." Which corner point do you want swapped and what happens to layers which move off the canvas as a result?
Copy link to clipboard
Copied
Swap the positions of picture A and picture B, and align them in the center
Copy link to clipboard
Copied
Picture A shows a triangle and picture B shows a circle and they are different sizes. What do you want aligned where?
Can you post some screenshots to illustrate what you are doing?
Copy link to clipboard
Copied
Try this
#target photoshop
app.bringToFront();
var doc = app.activeDocument;
var selectedLayers = getSelectedLayersIndex(doc);
if (selectedLayers.length == 2) {
try {
for (var i = 0; selectedLayers.length > i; i++) {
var sLayers = new Array();
for (var i = 0, l = selectedLayers.length; i < l; i++) {
selectLayerByIndex(selectedLayers[i], false);
if (app.activeDocument.activeLayer.typename == "LayerSet") {
executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO);
var xleft = activeDocument.activeLayer.bounds[0]
var ytop = activeDocument.activeLayer.bounds[1];
var ybot = activeDocument.activeLayer.bounds[3];
var width = activeDocument.activeLayer.bounds[2] - activeDocument.activeLayer.bounds[0];
var height = activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1];
var xcenter = activeDocument.activeLayer.bounds[0] + width / 2
var ycenter = activeDocument.activeLayer.bounds[1] + height / 2
var xright = activeDocument.activeLayer.bounds[2]
app.activeDocument.activeHistoryState = app.activeDocument.historyStates[app.activeDocument.historyStates.length - 2];
selectLayerByIndex(selectedLayers[i], false);
} else {
var xpos = activeDocument.activeLayer.bounds[0];
var ytop = activeDocument.activeLayer.bounds[1];
var ybot = activeDocument.activeLayer.bounds[3];
var width = activeDocument.activeLayer.bounds[2] - activeDocument.activeLayer.bounds[0];
var height = activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1];
var xcenter = activeDocument.activeLayer.bounds[0] + width / 2
var ycenter = activeDocument.activeLayer.bounds[1] + height / 2
var xleft = activeDocument.activeLayer.bounds[0]
var xright = activeDocument.activeLayer.bounds[2]
}
sLayers.push([activeDocument.activeLayer, xleft, ytop, width, height, xcenter, ycenter, xright, ybot]);
}
var oneWidth = sLayers[0][3]
var oneHeight = sLayers[0][4]
var oneXcenter = sLayers[0][5]
var oneYcenter = sLayers[0][6]
var twoWidth = sLayers[1][3]
var twoHeight = sLayers[1][4]
var twoXcenter = sLayers[1][5]
var twoYcenter = sLayers[1][6]
doc.activeLayer = sLayers[0][0]
Xdelta = twoXcenter - oneXcenter;
Ydelta = twoYcenter - oneYcenter;
transform(Xdelta, Ydelta, 100, 100);
doc.activeLayer = sLayers[1][0]
Xdelta = oneXcenter - twoXcenter;
Ydelta = oneYcenter - twoYcenter;
transform(Xdelta, Ydelta, 100, 100);
///////////////////////////////////////////////////////
for (var i = 0, l = selectedLayers.length; i < l; i++) {
selectLayerByIndex(selectedLayers[i], true);
}
}
} catch (e) {
alert(e + ': on line ' + e.line, 'Script Error', true);
}
}
function transform(DeltaX, DeltaY, WidthPercent, HeightPercent) {
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
descriptor.putEnumerated(stringIDToTypeID("freeTransformCenterState"), stringIDToTypeID("quadCenterState"), stringIDToTypeID("QCSAverage"));
descriptor2.putUnitDouble(stringIDToTypeID("horizontal"), stringIDToTypeID("pixelsUnit"), DeltaX);
descriptor2.putUnitDouble(stringIDToTypeID("vertical"), stringIDToTypeID("pixelsUnit"), DeltaY);
descriptor.putObject(stringIDToTypeID("offset"), stringIDToTypeID("offset"), descriptor2);
descriptor.putUnitDouble(stringIDToTypeID("width"), stringIDToTypeID("percentUnit"), WidthPercent);
descriptor.putUnitDouble(stringIDToTypeID("height"), stringIDToTypeID("percentUnit"), HeightPercent);
try {
executeAction(stringIDToTypeID("transform"), descriptor, DialogModes.NO);
} catch (e) {
alert(e + ': on line ' + e.line, 'Script Error', true);
}
}
function getSelectedLayersIndex(doc) {
var selectedLayers = [];
var ref = new ActionReference();
ref.putEnumerated(cTID('Dcmn'), cTID('Ordn'), cTID('Trgt'));
var desc = executeActionGet(ref);
if (desc.hasKey(sTID('targetLayers'))) {
desc = desc.getList(sTID('targetLayers'));
var c = desc.count;
for (var i = 0; i < c; i++) {
try {
doc.backgroundLayer;
selectedLayers.push(desc.getReference(i).getIndex());
} catch (e) {
selectedLayers.push(desc.getReference(i).getIndex() + 1);
}
}
} else {
var ref = new ActionReference();
ref.putProperty(cTID('Prpr'), cTID('ItmI'));
ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
try {
doc.backgroundLayer;
selectedLayers.push(executeActionGet(ref).getInteger(cTID('ItmI')) - 1);
} catch (e) {
selectedLayers.push(executeActionGet(ref).getInteger(cTID('ItmI')));
}
}
return selectedLayers;
}
function selectLayerByIndex(index, add) {
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref);
if (add) desc.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection"));
desc.putBoolean(charIDToTypeID("MkVs"), false);
try {
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
} catch (e) {}
}
function cTID(s) {
return app.charIDToTypeID(s);
}
function sTID(s) {
return app.stringIDToTypeID(s);
}
Copy link to clipboard
Copied
You're very welcome! Glad I could help
Copy link to clipboard
Copied
After swapping positions, how can I rearrange the layer order?
After using your code, for example, if Image 1 is above Layer 1 and Image 2 is above Layer 2, how can I move Image 1 above Layer 2 and Image 2 above Layer 1?