forked from Paul-Riggott/PS-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Run Action.jsx
292 lines (284 loc) · 10.1 KB
/
Run Action.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#target photoshop
app.bringToFront();
main();
function main(){
if(!documents.length){
alert("You need to have at least one document open!");
return;
}
var win = new Window( 'dialog', 'RA' );
g = win.graphics;
var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.99, 0.99, 0.99, 1]);
g.backgroundColor = myBrush;
win.orientation='stack';
win.p1= win.add("panel", undefined, undefined, {borderStyle:"black"});
win.g1 = win.p1.add('group');
win.g1.orientation = "row";
win.title = win.g1.add('statictext',undefined,'Run Action(s)');
win.title.alignment="fill";
var g = win.title.graphics;
g.font = ScriptUI.newFont("Georgia","BOLDITALIC",22);
win.p1.p1= win.p1.add("panel", undefined, undefined, {borderStyle:"black"});
win.p1.p1.preferredSize=[300,2];
win.g3 =win.p1.add('group');
win.g3.orientation = "row";
win.g3.alignment='fill';
win.g3.spacing=10;
win.g3.cb1 = win.g3.add('checkbox',undefined,'Create Snapshop');
win.g5 =win.p1.add('group');
win.g5.orientation = "row";
win.g5.alignment='fill';
win.g5.spacing=10;
win.g5.cb1 = win.g5.add('checkbox',undefined,'Run Action(s) On All Layers');
win.g5.cb1.value=true;
win.g6 =win.p1.add('group');
win.g6.orientation = "row";
win.g6.alignment='fill';
win.g6.spacing=10;
win.g6.cb1 = win.g6.add('checkbox',undefined,'Run Action(s) On Selected Layer(s)');
if(activeDocument.layers.length < 2){
win.g6.cb1.enabled=false;
}
var selectedLayers= getSelectedLayersIdx();
if(selectedLayers.length <1) win.g6.cb1.enabled=false;
win.g5.cb1.onClick=function(){
if(win.g5.cb1.value){
win.g6.cb1.value=false;
}else{
win.g6.cb1.value=true;
if(selectedLayers.length >1) win.g6.cb1.enabled=true;
}
}
win.g6.cb1.onClick=function(){
if(win.g6.cb1.value){
win.g5.cb1.value=false;
}else{
win.g5.cb1.value=true;
}
}
win.g5.cb1.onClick();
win.p1.p2= win.p1.add("panel", undefined, undefined, {borderStyle:"black"});
win.p1.p2.preferredSize=[300,2];
win.g10 =win.p1.add('group');
win.g10.orientation = "row";
win.g10.alignment='fill';
win.g10.spacing=10;
win.g10.dd1 = win.g10.add('dropdownlist');
win.g10.dd2 = win.g10.add('dropdownlist');
win.g15 =win.p1.add('group');
win.g15.orientation = "row";
win.g15.alignment='fill';
win.g15.spacing=10;
win.g15.st1 = win.g15.add('statictext',undefined,'Selected Actions');
win.g15.et1 = win.g15.add('edittext',undefined,'0');
win.g15.et1.preferredSize=[40,20];
win.g15.et1.enabled=false;
win.g15.bu1 = win.g15.add('button',undefined,'Add Action');
win.g15.bu2 = win.g15.add('button',undefined,'Remove Action');
win.p1.p3= win.p1.add("panel", undefined, undefined, {borderStyle:"black"});
win.p1.p3.preferredSize=[300,2];
win.g20 =win.p1.add('group');
win.g20.orientation = "row";
win.g20.alignment='center';
win.g20.spacing=10;
win.g20.bu1 = win.g20.add('button',undefined,'Process');
win.g20.bu1.preferredSize=[170,35];
win.g20.bu2 = win.g20.add('button',undefined,'Cancel');
win.g20.bu2.preferredSize=[170,35];
var actionSets = new Array();
actionSets = getActionSets();
for (var i=0,len=actionSets.length;i<len;i++) {
win.g10.dd1.add ('item', "" + actionSets[i]);
};
win.g10.dd1.selection=0;
var actions = new Array();
actions = getActions(actionSets[0]);
for (var i=0,len=actions.length;i<len;i++) {
win.g10.dd2.add ('item', "" + actions[i]);
};
win.g10.dd2.selection=0;
win.g10.dd1.onChange = function() {
win.g10.dd2.removeAll();
actions = getActions(actionSets[parseInt(this.selection)]);
for (var i=0,len=actions.length;i<len;i++) {
win.g10.dd2.add ('item', "" + actions[i]);
}
win.g10.dd2.selection=0;
};
actionArray=[];
win.g15.bu1.onClick = function() {
actionArray.push([[win.g10.dd2.selection.text],[win.g10.dd1.selection.text]]);
win.g15.et1.text = actionArray.length;
}
win.g15.bu2.onClick = function() {
actionArray.pop();
win.g15.et1.text = actionArray.length;
}
win.g20.bu1.onClick=function(){
if(actionArray.length ==0) actionArray.push([[win.g10.dd2.selection.text],[win.g10.dd1.selection.text]]);
win.close(1);
if(win.g5.cb1.value){
selectAllLayers();
selectedLayers= getSelectedLayersIdx();
}
if(win.g3.cb1.value) snapShot();
for(var a in selectedLayers){
makeActiveByIndex(Number(selectedLayers[a]));
if(app.activeDocument.activeLayer.kind != LayerKind.NORMAL) continue;
for(var z in actionArray){
try{
doAction(actionArray[z][0].toString(), actionArray[z][1].toString());
}catch(e){alert(e+" - "+e.line);}
}
}
};
win.center();
win.show();
function getActionSets() {
cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
var i = 1;
var sets = [];
while (true) {
var ref = new ActionReference();
ref.putIndex(cTID("ASet"), i);
var desc;
var lvl = $.level;
$.level = 0;
try {
desc = executeActionGet(ref);
} catch (e) {
break;
} finally {
$.level = lvl;
}
if (desc.hasKey(cTID("Nm "))) {
var set = {};
set.index = i;
set.name = desc.getString(cTID("Nm "));
set.toString = function() { return this.name; };
set.count = desc.getInteger(cTID("NmbC"));
set.actions = [];
for (var j = 1; j <= set.count; j++) {
var ref = new ActionReference();
ref.putIndex(cTID('Actn'), j);
ref.putIndex(cTID('ASet'), set.index);
var adesc = executeActionGet(ref);
var actName = adesc.getString(cTID('Nm '));
set.actions.push(actName);
}
sets.push(set);
}
i++;
}
return sets;
};
function getActions(aset) {
cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
var i = 1;
var names = [];
if (!aset) {
throw "Action set must be specified";
}
while (true) {
var ref = new ActionReference();
ref.putIndex(cTID("ASet"), i);
var desc;
try {
desc = executeActionGet(ref);
} catch (e) {
break;
}
if (desc.hasKey(cTID("Nm "))) {
var name = desc.getString(cTID("Nm "));
if (name == aset) {
var count = desc.getInteger(cTID("NmbC"));
var names = [];
for (var j = 1; j <= count; j++) {
var ref = new ActionReference();
ref.putIndex(cTID('Actn'), j);
ref.putIndex(cTID('ASet'), i);
var adesc = executeActionGet(ref);
var actName = adesc.getString(cTID('Nm '));
names.push(actName);
}
break;
}
}
i++;
}
return names;
};
function snapShot() {
var desc9 = new ActionDescriptor();
var ref5 = new ActionReference();
ref5.putClass( charIDToTypeID('SnpS') );
desc9.putReference( charIDToTypeID('null'), ref5 );
var ref6 = new ActionReference();
ref6.putProperty( charIDToTypeID('HstS'), charIDToTypeID('CrnH') );
desc9.putReference( charIDToTypeID('From'), ref6 );
desc9.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('HstS'), charIDToTypeID('FllD') );
executeAction( charIDToTypeID('Mk '), desc9, DialogModes.NO );
};
function hasLayerMask() {
var ref = new ActionReference();
ref.putEnumerated( stringIDToTypeID( "layer" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ));
var Mask= executeActionGet( ref );
return Mask.hasKey(charIDToTypeID('Usrs'));
};
function getSelectedLayersIdx(){
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count
var selectedLayers = new Array();
for(var i=0;i<c;i++){
try{
activeDocument.backgroundLayer;
selectedLayers.push( desc.getReference( i ).getIndex() );
}catch(e){
selectedLayers.push( desc.getReference( i ).getIndex()+1 );
}
}
}else{
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
try{
activeDocument.backgroundLayer;
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
}catch(e){
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
}
}
return selectedLayers;
};
function makeActiveByIndex( idx, visible,add ){
if(visible == undefined) visible = false;
if(add == undefined) add=false;
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIndex(charIDToTypeID( "Lyr " ), idx)
desc.putReference( charIDToTypeID( "null" ), ref );
if(add) desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
desc.putBoolean( charIDToTypeID( "MkVs" ), visible );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
};
function selectAllLayers(layer) {
if(layer == undefined) layer = 0;
activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length-1];
var BL = activeDocument.activeLayer.name;
activeDocument.activeLayer = activeDocument.layers[layer];
var desc5 = new ActionDescriptor();
var ref3 = new ActionReference();
ref3.putName( charIDToTypeID('Lyr '), BL);
desc5.putReference( charIDToTypeID('null'), ref3 );
desc5.putEnumerated( stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelectionContinuous') );
desc5.putBoolean( charIDToTypeID('MkVs'), false );
executeAction( charIDToTypeID('slct'), desc5, DialogModes.NO );
};
}