-
Notifications
You must be signed in to change notification settings - Fork 33
/
Link All Masks.jsx
513 lines (473 loc) · 17.7 KB
/
Link All Masks.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// Link All Masks.jsx
// Based on Delete All Layer masks:
// Adjusted by Bruno Herfst 2015
// c2008 Adobe Systems, Inc. All rights reserved.
// based on "flatten all layer masks" by Jeffrey Tranberry
// modified by Joergen Geerds
/*
@@@BUILDINFO@@@ Link All Masks.jsx 1.0.0.1
*/
/*
// BEGIN__HARVEST_EXCEPTION_ZSTRING
<javascriptresource>
<name>$$$/JavaScripts/LinkAllMasks/Menu=Link All Masks</name>
<about>$$$/JavaScripts/LinkAllMasks/About=Link All Masks^r^rCopyright 2008 Adobe Systems Incorporated. All rights reserved.^r^rLink Layer Masks for all layers.</about>
<category>Link</category>
<enableinfo>true</enableinfo>
<eventid>e805a6ee-6d75-4b62-b6fe-f5873b5fdf20</eventid>
<terminology><![CDATA[<< /Version 1
/Events <<
/e805a6ee-6d75-4b62-b6fe-f5873b5fdf20 [($$$/JavaScripts/LinkAllMasks/Menu=Link All Masks) /noDirectParam <<
>>]
>>
>> ]]></terminology>
</javascriptresource>
// END__HARVEST_EXCEPTION_ZSTRING
*/
// enable double clicking from the
// Macintosh Finder or the Windows Explorer
#target photoshop
// Make Photoshop the frontmost application
app.bringToFront();
/////////////////////////
// SETUP
/////////////////////////
// all the strings that need localized
strLinkAllMasksHistoryStepName = localize( "$$$/JavaScripts/LinkAllMasks/Menu=Link All Masks" );
/////////////////////////
// MAIN
/////////////////////////
var doc = app.activeDocument; // remember the document, the selected layer, the visibility setting of the selected layer
var currentLayer = doc.activeLayer; // remember the selected layer
var currentVisible = currentLayer.visible;// remember the visibility setting of the selected layer
var mySelectedLayers = getSelectedLayers(); // remember the selected layers
// Create only one history state for the entire script
doc.suspendHistory(strLinkAllMasksHistoryStepName, "main()");
// restore the selected layer
try{
doc.activeLayer = currentLayer;
}catch(e) {
; // do nothing
}
// restore the visibility setting of the original layer
try{
currentLayer.visible = currentVisible;
}catch(e) {
; // do nothing
}
// restore the visibility setting of the original layer
try{
currentLayer.visible = currentVisible;
}catch(e) {
; // do nothing
}
if (mySelectedLayers.length != 0){//,more than one layer selected
// restore the selected layers
try{
setSelectedLayers(mySelectedLayers);
}catch(e) {
; // do nothing
}
}
// Record the script in the Actions palette when recording an action
try{
var playbackDescription = new ActionDescriptor;
var playbackReference = new ActionReference;
playbackReference.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
playbackDescription.putReference( charIDToTypeID("null"), playbackReference);
app.playbackDisplayDialogs = DialogModes.NO;
app.playbackParameters = playbackDescription;
}catch(e) {
; // do nothing
}
/////////////////////////
// FUNCTIONS
/////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Function: main
// Usage: container function to hold all the working code that generates history states
// Input: <none> Must have an open document
// Return: <none>
///////////////////////////////////////////////////////////////////////////////
function main(){
// Just in case we forgot!
if (!confirm("This script is WIP and deletes all layer masks!\nDo you want to continue?")){
return;
}
// Do some voodoo on the layer selection incase no layer is selected or multiple layers are selected
try{
touchUpLayerSelection()
}catch(e) {
; // do nothing
}
// create an array and store all the art layers in that array
var allArtLayers = new Array;
var allVisibleInfo = new Array;
getAllArtLayers(doc, allArtLayers, allVisibleInfo);
// Walk the layer stack
for (var i = 0; i < allArtLayers.length; i++){
try{
doc.activeLayer = allArtLayers[i];
if (hasVectorMask() == true){ // Only if it has a layer mask
rasterizeLayer(); // Rasterize the layer in case it's a fill layer, smart object, video or 3D layer (since you can't apply masks to these kinds of layers)
selectVectorMask(); // Select the vector mask
rasterizeVectorMask(); // rasterize the vector mask
LinkLayerMask(); // Apply the layer mask
}
if (hasLayerMask() == true){ // Only if it has a layer mask
rasterizeLayer(); // Rasterize the layer in case it's a fill layer, smart object, video or 3D layer (since you can't apply masks to these kinds of layers)
selectLayerMask(); // Select the layer mask
LinkLayerMask(); // Apply the layer mask
}
if (hasFilterMask() == true){ // Only if it has a Smart Filter mask
rasterizeLayer(); // Rasterize the layer - It's the only way to apply a Smart Filter Mask
}
allArtLayers[i].visible = allVisibleInfo[i];
}catch(e) {
; // do nothing
}
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: touchUpLayerSelection
// Usage: deal with odd layer selections of no layer selected or multiple layers
// Input: <none> Must have an open document
// Return: <none>
///////////////////////////////////////////////////////////////////////////////
function touchUpLayerSelection() {
try{
// Select all Layers
var idselectAllLayers = stringIDToTypeID( "selectAllLayers" );
var desc252 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref174 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref174.putEnumerated( idLyr, idOrdn, idTrgt );
desc252.putReference( idnull, ref174 );
executeAction( idselectAllLayers, desc252, DialogModes.NO );
// Select the previous layer
var idslct = charIDToTypeID( "slct" );
var desc209 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref140 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idBack = charIDToTypeID( "Back" );
ref140.putEnumerated( idLyr, idOrdn, idBack );
desc209.putReference( idnull, ref140 );
var idMkVs = charIDToTypeID( "MkVs" );
desc209.putBoolean( idMkVs, false );
executeAction( idslct, desc209, DialogModes.NO );
}catch(e) {
; // do nothing
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: hasLayerMask
// Usage: see if there is a raster layer mask
// Input: <none> Must have an open document
// Return: true if there is a vector mask
///////////////////////////////////////////////////////////////////////////////
function hasLayerMask() {
var hasLayerMask = false;
try {
var ref = new ActionReference();
var keyUserMaskEnabled = app.charIDToTypeID( 'UsrM' );
ref.putProperty( app.charIDToTypeID( 'Prpr' ), keyUserMaskEnabled );
ref.putEnumerated( app.charIDToTypeID( 'Lyr ' ), app.charIDToTypeID( 'Ordn' ), app.charIDToTypeID( 'Trgt' ) );
var desc = executeActionGet( ref );
if ( desc.hasKey( keyUserMaskEnabled ) ) {
hasLayerMask = true;
}
}catch(e) {
hasLayerMask = false;
}
return hasLayerMask;
}
///////////////////////////////////////////////////////////////////////////////
// Function: hasVectorMask
// Usage: see if there is a vector layer mask
// Input: <none> Must have an open document
// Return: true if there is a vector mask
///////////////////////////////////////////////////////////////////////////////
function hasVectorMask() {
var hasVectorMask = false;
try {
var ref = new ActionReference();
var keyVectorMaskEnabled = app.stringIDToTypeID( 'vectorMask' );
var keyKind = app.charIDToTypeID( 'Knd ' );
ref.putEnumerated( app.charIDToTypeID( 'Path' ), app.charIDToTypeID( 'Ordn' ), keyVectorMaskEnabled );
var desc = executeActionGet( ref );
if ( desc.hasKey( keyKind ) ) {
var kindValue = desc.getEnumerationValue( keyKind );
if (kindValue == keyVectorMaskEnabled) {
hasVectorMask = true;
}
}
}catch(e) {
hasVectorMask = false;
}
return hasVectorMask;
}
///////////////////////////////////////////////////////////////////////////////
// Function: hasFilterMask
// Usage: see if there is a Smart Filter mask
// Input: <none> Must have an open document
// Return: true if there is a Smart Filter mask
///////////////////////////////////////////////////////////////////////////////
function hasFilterMask() {
var hasFilterMask = false;
try {
var ref = new ActionReference();
var keyFilterMask = app.stringIDToTypeID("hasFilterMask");
ref.putProperty( app.charIDToTypeID( 'Prpr' ), keyFilterMask);
ref.putEnumerated( app.charIDToTypeID( 'Lyr ' ), app.charIDToTypeID( 'Ordn' ), app.charIDToTypeID( 'Trgt' ) );
var desc = executeActionGet( ref );
if ( desc.hasKey( keyFilterMask ) && desc.getBoolean( keyFilterMask )) {
hasFilterMask = true;
}
}catch(e) {
hasFilterMask = false;
}
return hasFilterMask;
}
///////////////////////////////////////////////////////////////////////////////
// Function: selectLayerMask
// Usage: select the layer mask on the current layer
// Input: <none> Must have an open document
// Return: <none>
///////////////////////////////////////////////////////////////////////////////
function selectLayerMask() {
try{
var id759 = charIDToTypeID( "slct" );
var desc153 = new ActionDescriptor();
var id760 = charIDToTypeID( "null" );
var ref92 = new ActionReference();
var id761 = charIDToTypeID( "Chnl" );
var id762 = charIDToTypeID( "Chnl" );
var id763 = charIDToTypeID( "Msk " );
ref92.putEnumerated( id761, id762, id763 );
desc153.putReference( id760, ref92 );
var id764 = charIDToTypeID( "MkVs" );
desc153.putBoolean( id764, false );
executeAction( id759, desc153, DialogModes.NO );
}catch(e) {
; // do nothing
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: selectVectorMask
// Usage: select the vector mask on the current layer
// Input: <none> Must have an open document
// Return: <none>
///////////////////////////////////////////////////////////////////////////////
function selectVectorMask() {
try{
var id55 = charIDToTypeID( "slct" );
var desc15 = new ActionDescriptor();
var id56 = charIDToTypeID( "null" );
var ref13 = new ActionReference();
var id57 = charIDToTypeID( "Path" );
var id58 = charIDToTypeID( "Path" );
var id59 = stringIDToTypeID( "vectorMask" );
ref13.putEnumerated( id57, id58, id59 );
var id60 = charIDToTypeID( "Lyr " );
var id61 = charIDToTypeID( "Ordn" );
var id62 = charIDToTypeID( "Trgt" );
ref13.putEnumerated( id60, id61, id62 );
desc15.putReference( id56, ref13 );
executeAction( id55, desc15, DialogModes.NO );
}catch(e) {
; // do nothing
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: LinkLayerMask
// Usage: apply the vector mask on the current layer
// Input: <none> Must have an open document
// Return: <none>
///////////////////////////////////////////////////////////////////////////////
function LinkLayerMask() {
try{
var idDlt = charIDToTypeID( "Dlt " );
var desc6 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref5 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref5.putEnumerated( idChnl, idOrdn, idTrgt );
desc6.putReference( idnull, ref5 );
executeAction( idDlt, desc6, DialogModes.NO );
}catch(e) {
; // do nothing
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: rasterizeLayer
// Usage: rasterize the current layer to pixels
// Input: <none> Must have an open document
// Return: <none>
///////////////////////////////////////////////////////////////////////////////
function rasterizeLayer() {
try{
var id1242 = stringIDToTypeID( "rasterizeLayer" );
var desc245 = new ActionDescriptor();
var id1243 = charIDToTypeID( "null" );
var ref184 = new ActionReference();
var id1244 = charIDToTypeID( "Lyr " );
var id1245 = charIDToTypeID( "Ordn" );
var id1246 = charIDToTypeID( "Trgt" );
ref184.putEnumerated( id1244, id1245, id1246 );
desc245.putReference( id1243, ref184 );
executeAction( id1242, desc245, DialogModes.NO );
}catch(e) {
; // do nothing
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: rasterizeVectorMask
// Usage: rasterize the vector mask on the
// current layer to pixels
// Input: <none> Must have an open document
// Return: <none>
///////////////////////////////////////////////////////////////////////////////
function rasterizeVectorMask() {
try{
var id488 = stringIDToTypeID( "rasterizeLayer" );
var desc44 = new ActionDescriptor();
var id489 = charIDToTypeID( "null" );
var ref29 = new ActionReference();
var id490 = charIDToTypeID( "Lyr " );
var id491 = charIDToTypeID( "Ordn" );
var id492 = charIDToTypeID( "Trgt" );
ref29.putEnumerated( id490, id491, id492 );
desc44.putReference( id489, ref29 );
var id493 = charIDToTypeID( "What" );
var id494 = stringIDToTypeID( "rasterizeItem" );
var id495 = stringIDToTypeID( "vectorMask" );
desc44.putEnumerated( id493, id494, id495 );
executeAction( id488, desc44, DialogModes.NO );
}catch(e) {
; // do nothing
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: getAllArtLayers
// Usage: get a reference to all artLayers in
// the document, does recursion into groups
// Input: obj, current object, document or layerSet
// layersArray, place to put the resulting artLayers,
// layersArray is both input and output
// Return: <none>
///////////////////////////////////////////////////////////////////////////////
function getAllArtLayers(obj, layersArray, visibleArray) {
for( var i = 0; i < obj.artLayers.length; i++) {
layersArray.push(obj.artLayers[i]);
visibleArray.push(obj.artLayers[i].visible);
}
for( var i = 0; i < obj.layerSets.length; i++) {
getAllArtLayers(obj.layerSets[i], layersArray, visibleArray); // recursive call
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: getSelectedLayers
// Usage: creates and array of the currently selected layers
// Input: <none> Must have an open document
// Return: Array selectedLayers
///////////////////////////////////////////////////////////////////////////////
function getSelectedLayers() {
var selectedLayers = [];
try {
var backGroundCounter = activeDocument.artLayers[activeDocument.artLayers.length-1].isBackgroundLayer ? 0 : 1;
var ref = new ActionReference();
var keyTargetLayers = app.stringIDToTypeID( 'targetLayers' );
ref.putProperty( app.charIDToTypeID( 'Prpr' ), keyTargetLayers );
ref.putEnumerated( app.charIDToTypeID( 'Dcmn' ), app.charIDToTypeID( 'Ordn' ), app.charIDToTypeID( 'Trgt' ) );
var desc = executeActionGet( ref );
if ( desc.hasKey( keyTargetLayers ) ) {
var layersList = desc.getList( keyTargetLayers );
for ( var i = 0; i < layersList.count; i++) {
var listRef = layersList.getReference( i );
selectedLayers.push( listRef.getIndex() + backGroundCounter );
}
//hasLayerMask = true;
}
}catch(e) {
; // do nothing
}
return selectedLayers;
}
///////////////////////////////////////////////////////////////////////////////
// Function: setSelectedLayers
// Usage: Selects an array of layers
// Input: Array selectedLayers
// Return: <none>
///////////////////////////////////////////////////////////////////////////////
function setSelectedLayers( layerIndexesOrNames ) {
// first select the first one
setSelectedLayer( layerIndexesOrNames[0] );
// then add to the selection
for ( var i = 1; i < layerIndexesOrNames.length; i++) {
addSelectedLayer( layerIndexesOrNames[i] );
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: setSelectedLayer
// Usage: Selects the first layer
// Input: Array selectedLayers
// Return: <none>
///////////////////////////////////////////////////////////////////////////////
function setSelectedLayer( layerIndexOrName ) {
try {
var id239 = charIDToTypeID( "slct" );
var desc45 = new ActionDescriptor();
var id240 = charIDToTypeID( "null" );
var ref43 = new ActionReference();
var id241 = charIDToTypeID( "Lyr " );
if ( typeof layerIndexOrName == "number" ) {
ref43.putIndex( id241, layerIndexOrName );
} else {
ref43.putName( id241, layerIndexOrName );
}
desc45.putReference( id240, ref43 );
var id242 = charIDToTypeID( "MkVs" );
desc45.putBoolean( id242, false );
executeAction( id239, desc45, DialogModes.NO );
}catch(e) {
; // do nothing
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: addSelectedLayer
// Usage: adds the rest of the layers in the array to the first layer
// Input: Array selectedLayers
// Return: <none>
///////////////////////////////////////////////////////////////////////////////
function addSelectedLayer( layerIndexOrName ) {
try {
var id243 = charIDToTypeID( "slct" );
var desc46 = new ActionDescriptor();
var id244 = charIDToTypeID( "null" );
var ref44 = new ActionReference();
var id245 = charIDToTypeID( "Lyr " );
if ( typeof layerIndexOrName == "number" ) {
ref44.putIndex( id245, layerIndexOrName );
} else {
ref44.putName( id245, layerIndexOrName );
}
desc46.putReference( id244, ref44 );
var id246 = stringIDToTypeID( "selectionModifier" );
var id247 = stringIDToTypeID( "selectionModifierType" );
var id248 = stringIDToTypeID( "addToSelection" );
desc46.putEnumerated( id246, id247, id248 );
var id249 = charIDToTypeID( "MkVs" );
desc46.putBoolean( id249, false );
executeAction( id243, desc46, DialogModes.NO );
}catch(e) {
; // do nothing
}
}
// End Link All Masks.jsx