-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathswapPosition-mirror.jsx
151 lines (133 loc) · 6.25 KB
/
swapPosition-mirror.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// swapPosition-mirror.jsx - Adobe Photoshop Script
// Version: 0.2.1
// Author: Anton Lyubushkin (nvkz.nemo@gmail.com)
// Website: http://lyubushkin.pro/
// ============================================================================
// Installation:
// 1. Place script in:
// PC(32): C:\Program Files (x86)\Adobe\Adobe Photoshop CC#\Presets\Scripts\
// PC(64): C:\Program Files\Adobe\Adobe Photoshop CC# (64 Bit)\Presets\Scripts\
// Mac: <hard drive>/Applications/Adobe Photoshop CC#/Presets/Scripts/
// 2. Restart Photoshop
// 3. Choose File > Scripts > swapPosition-mirror
// ============================================================================
#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 xpos = activeDocument.activeLayer.bounds[0];
var ytop = activeDocument.activeLayer.bounds[1];
var ybot = activeDocument.activeLayer.bounds[3];
var xleft = activeDocument.activeLayer.bounds[0]
var xright = activeDocument.activeLayer.bounds[2]
var height = activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1];
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 height = activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1];
var xleft = activeDocument.activeLayer.bounds[0]
var xright = activeDocument.activeLayer.bounds[2]
}
sLayers.push([activeDocument.activeLayer, xpos, ytop, ybot, height, xleft, xright]);
}
var delta0_y = 0;
var delta1_y = 0;
function differentHeight() {
if (sLayers[1][4] != sLayers[0][4]) {
return true
} else {
return false
}
}
function returnSmaller() {
if (sLayers[1][4] < sLayers[0][4]) {
return [sLayers[1][0], sLayers[0][0]]
} else {
return [sLayers[0][0], sLayers[1][0]]
}
}
if (sLayers[1][2] != sLayers[0][2]) {
if (differentHeight() == true) {
var smallLyr = returnSmaller();
if (smallLyr[0].bounds[1] < smallLyr[1].bounds[1] || smallLyr[0].bounds[3] > smallLyr[1].bounds[3]) {
delta0_y = (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2]))) - (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2])));
delta1_y = (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2]))) - (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2])));
} else {
delta0_y = 0;
delta1_y = 0;
}
} else {
delta0_y = (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2]))) - (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2])));
delta1_y = (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2]))) - (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2])));
}
}
if (sLayers[0][5] < sLayers[1][5] && sLayers[0][6] < sLayers[1][6]) {
var delta0_x = sLayers[1][6] - sLayers[0][6];
var delta1_x = sLayers[0][5] - sLayers[1][5];
} else {
var delta0_x = sLayers[1][5] - sLayers[0][5];
var delta1_x = sLayers[0][6] - sLayers[1][6];
}
sLayers[0][0].translate(delta0_x, delta0_y);
sLayers[1][0].translate(delta1_x, delta1_y);
for (var i = 0, l = selectedLayers.length; i < l; i++) {
selectLayerByIndex(selectedLayers[i], true);
}
}
} catch (e) {
alert("Oops! These layers cannot be repositioned");
}
}
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);}