forked from geeklystrips/pslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Layer Metadata Editor.jsx
987 lines (888 loc) · 28.5 KB
/
Layer Metadata Editor.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
// layer metadata editor
#target photoshop
// ############# PER-LAYER METADATA FUNCTIONS
Pslib = function(){};
// define default namespace
Pslib.XMPNAMESPACE = "http://custom/";
Pslib.XMPNAMESPACEPREFIX = "cstm:";
Pslib.DEFAULTPROPERTYNAME = "default";
Pslib.DEFAULTPROPERTYVALUE = "value";
// for replacing huge whitespace chunk in XMP
var XmpWhitespace = " \
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
";
try
{
// load library
if(!ExternalObject.AdobeXMPScript)
{
ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
}
// register custom namespace
XMPMeta.registerNamespace(Pslib.XMPNAMESPACE, Pslib.XMPNAMESPACEPREFIX);
}
catch(e)
{
// if ExternalObject.AdobeXMPScript not present, hardcode the namespace to exif
Pslib.XMPNAMESPACE = "http://ns.adobe.com/exif/1.0/";
}
// load XMP
Pslib.loadXMPLibrary = function()
{
if (!ExternalObject.AdobeXMPScript)
{
try
{
if($.level) $.writeln("Loading XMP Script Library");
ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
return true;
}
catch (e)
{
if($.level) $.writeln("Error loading XMP Script Library\n" + e);
return false;
}
}
return true;
};
// unload XMP
Pslib.unloadXMPLibrary = function()
{
if(ExternalObject.AdobeXMPScript)
{
try
{
if($.level) $.writeln("Unloading XMP Script Library");
ExternalObject.AdobeXMPScript.unload();
ExternalObject.AdobeXMPScript = undefined;
return true;
}
catch(e)
{
if($.level) $.writeln("Error unloading XMP Script Library\n" + e);
return false;
}
}
};
// get layer's existing XMP if present
Pslib.getXmp = function (layer, createNew)
{
var layer = (layer == undefined ? app.activeDocument.activeLayer : layer);
var createNew = (createNew == undefined ? false : createNew);
var xmp;
// if library loads without problem, proceed to get the layer's xmpMetadata
if(Pslib.loadXMPLibrary())
{
try
{
if($.level) $.writeln("Attempting to get metadata for layer \"" + layer.name + "\"");
xmp = new XMPMeta( layer.xmpMetadata.rawData );
}
catch( e )
{
if($.level) $.writeln("Metadata could not be found for layer \"" + layer.name + "\"" + (createNew ? "\nCreating new XMP object." : "") );
if(createNew) xmp = new XMPMeta();
}
return xmp;
}
else
{
return xmp;
}
};
// get property: returns a string
Pslib.getXmpProperty = function (layer, property)
{
// make sure XMP lib stuff is available
if(Pslib.loadXMPLibrary())
{
var layer = layer == undefined ? app.activeDocument.activeLayer : layer;
var value;
var xmp;
// make sure we're not working with a background layer
if(layer.isBackgroundLayer)
{
if($.level) $.writeln("Metadata cannot exist on a background layer, so it cannot be accessed.");
return null;
}
// access metadata
try
{
xmp = new XMPMeta( layer.xmpMetadata.rawData );
value = xmp.getProperty(Pslib.XMPNAMESPACE, property)
// unload library
// Pslib.unloadXMPLibrary();
return value;
} catch( e ) {
if($.level) $.writeln("XMP metadata could not be found for layer \"" + layer.name + "\"");
// xmp = new XMPMeta();
return null
}
}
else
{
return null;
}
};
// delete specific property
Pslib.deleteXmpProperty = function (layer, property)
{
// load library
if(Pslib.loadXMPLibrary())
{
var layer = layer == undefined ? app.activeDocument.activeLayer : layer;
var xmp = Pslib.getXmp(layer);
try
{
xmp.deleteProperty(Pslib.XMPNAMESPACE, property);
layer.xmpMetadata.rawData = xmp.serialize();
return true;
}
catch( e )
{
if($.level) $.writeln("Metadata property could not be deleted from layer \"" + layer.name + "\"");
//xmp = new XMPMeta();
return false;
}
}
else
{
return false;
}
};
// set multiple properties
// expects a two-dimensional array
Pslib.setXmpProperties = function (layer, propertiesArray)
{
// make sure XMP lib stuff is available
if(Pslib.loadXMPLibrary())
{
var layer = layer == undefined ? app.activeDocument.activeLayer : layer;
var prop;
var val;
var xmp;
// make sure we're not working with a background layer
if(layer.isBackgroundLayer)
{
if($.level) $.writeln("XMP Metadata cannot be placed on a background layer. Aborting.");
return false;
}
// access metadata
try
{
xmp = new XMPMeta( layer.xmpMetadata.rawData );
if($.level) $.writeln("XMP Metadata successfully fetched from layer \"" + layer.name + "\"");
} catch( e )
{
if($.level) $.writeln("XMP metadata could not be found for layer \"" + layer.name + "\".\nCreating new XMP metadata container.");
xmp = new XMPMeta( );
}
// loop through array properties and assign them
if($.level) $.writeln("\nLooping through properties...");
for (var i = 0; i < propertiesArray.length; i++)
{
prop = propertiesArray[i][0];
val = propertiesArray[i][1];
// modify metadata
try
{
var propertyExists = xmp.doesPropertyExist(Pslib.XMPNAMESPACE, prop);
// add new property if not found
if(!propertyExists)
{
xmp.setProperty(Pslib.XMPNAMESPACE, prop, val);
if($.level) $.writeln("\tadding [" + prop + ": " + val +"] " + typeof val);
}
// if property found and value different, update
else if(propertyExists && xmp.getProperty(Pslib.XMPNAMESPACE, prop).toString() != val.toString() )
{
xmp.setProperty(Pslib.XMPNAMESPACE, prop, val);
if($.level) $.writeln("\tupdating [" + prop + ": " + val +"] " + typeof val);
}
else
{
if($.level) $.writeln("\tno change to existing property [" + prop + ": " + val +"] " + typeof val);
}
}
catch( e )
{
var msg = "Could not place metadata property on provided layer.\n[" + prop + ": " + val + +"] " + typeof val + "\n" + e;
if($.level) $.writeln( msg );
else alert(msg);
return false;
}
}
// applly and serialize
layer.xmpMetadata.rawData = xmp.serialize();
if($.level) $.writeln("Provided properties were successfully added to object \"" + layer.name + "\"");
// unload library
// Pslib.unloadXMPLibrary();
return true;
}
else
{
return false;
}
};
// clear XMP : current workaround is to replace current data by empty data
// this is problematic if you actually want to strip the layer from its metadata object entirely
Pslib.clearXmp = function (layer)
{
if(Pslib.loadXMPLibrary())
{
var layer = layer == undefined ? app.activeDocument.activeLayer : layer;
// if metadata not found, return
try
{
var xmp = new XMPMeta(layer.xmpMetadata.rawData);
}
catch(e)
{
//if($.level) $.writeln(msg + "\n" + e);
return true;
}
// if metadata found, replace by empty version
var emptyXmp = new XMPMeta();
layer.xmpMetadata.rawData = emptyXmp.serialize();
// Pslib.unloadXMPLibrary();
return true;
}
else
{
return false;
}
};
// clear entire namespace
Pslib.clearNamespace = function (layer, namespace)
{
if(Pslib.loadXMPLibrary())
{
var layer = layer == undefined ? app.activeDocument.activeLayer : layer;
// if metadata not found, return
try
{
var xmp = new XMPMeta(layer.xmpMetadata.rawData);
XMPUtils.removeProperties(xmp, namespace, undefined, XMPConst.REMOVE_ALL_PROPERTIES);
layer.xmpMetadata.rawData = xmp.serialize();
//alert(xmp.serialize());
}
catch(e)
{
if($.level) $.writeln("Metadata not found\n" + e);
return false;
}
// if metadata found, replace by empty version
//var emptyXmp = new XMPMeta();
//layer.xmpMetadata.rawData = emptyXmp.serialize();
return true;
}
else
{
return false;
}
};
// save metadata to XML file
Pslib.exportLayerMetadata = function (layer, path, alertMsg)
{
if(Pslib.loadXMPLibrary())
{
var xmp;
// verify that xmp data is available
try
{
var xmp = layer.xmpMetadata.rawData.toString();
//
if(alertMsg) alert(xmp);
//
}
catch(e)
{
var msg = "";
if(e.message.match("missing") != null)
{
msg += "There doesn't seem to be any metadata attached to layer \"" + layer.name + "\"";
}
if($.level) $.writeln(msg + "\n" + e);
else alert(msg + "\n" + e);
return false;
}
var path = path == undefined ? File.saveDialog() : path;
if(path != null)
{
var file = new File(path);
if(file.exists)
{
if($.level) $.writeln("\nFile already present. Prompting user for permission to replace.");
// if file exists, present the user with the option to replace it
if(confirm ("Do you wish to overwrite this file?\n\n" + file.fsName, true, "Replace file?"))
{
try
{
if($.level) $.writeln("Removing file:\n" + file.fsName );
file.remove();
}
catch(e)
{
if($.level) $.writeln("\nCould not remove file. Please verify that it is not open by a different process.");
return false;
}
}
else
{
if($.level) $.writeln("User opted not to replace the file.");
return false;
}
}
file.encoding = "UTF-8";
try
{
file.open("w");
file.write(xmp);
file.close();
}
catch(e)
{
var msg = "";
if($.level) $.writeln("Unable to write to file.\n" + msg + "\n" + e);
else alert("Unable to write to file.\n" + msg + "\n" + e);
return false;
}
file.close();
}
// Pslib.unloadXMPLibrary();
return true;
}
else
{
return false;
}
};
// app functions
function Main()
{
// prepare XMP container
var XMPObj = null;
var style = ScriptUI.newFont("Arial", "REGULAR", 18);
// build dialog window
var win = new Window('dialog', "Layer Metadata Editor");
// background color
//win.graphics.backgroundColor = win.graphics.newBrush (win.graphics.BrushType.SOLID_COLOR, [0.05, 0.5, 0.25]);
win.alignChildren = 'fill';
var maingroup = win.add('group');
maingroup.orientation = "column";
maingroup.alignChildren = 'fill';
var propxmp = maingroup.add('group');
propxmp.orientation = "column";
propxmp.alignChildren = 'fill';
var layerObjectPanel = propxmp.add('panel', undefined, "Layer Object");
layerObjectPanel.alignChildren = 'right';
var layerObjectGroup = layerObjectPanel.add('group');
var layerNameField = layerObjectGroup.add('statictext', undefined, "Name:");
layerNameField.enabled = false;
var layerNameField2 = layerObjectGroup.add('statictext', undefined, layerInfo.name);
layerNameField2.graphics.font = style;
layerNameField2.graphics.foregroundColor = layerNameField2.graphics.newPen (win.graphics.PenType.SOLID_COLOR, [0.25, 0.25, 0.6], 1);
var layerTypeField = layerObjectGroup.add('statictext', undefined, "Type:");
layerTypeField.enabled = false;
layerObjectGroup.add('statictext', undefined, layerObject.typename);
if(layerInfo.typename != "LayerSet")
{
var layerKindField = layerObjectGroup.add('statictext', undefined, "Kind:");
layerKindField.enabled = false;
layerObjectGroup.add('statictext', undefined, layerInfo.kind);
}
var layerObjectCoordsGroup = layerObjectPanel.add('group');
var layerWidthField = layerObjectCoordsGroup.add('statictext', undefined, "Width:");
layerWidthField.enabled = false;
layerObjectCoordsGroup.add('statictext', undefined, layerInfo.width);
var layerHeightField = layerObjectCoordsGroup.add('statictext', undefined, "Height:");
layerHeightField.enabled = false;
layerObjectCoordsGroup.add('statictext', undefined, layerInfo.height);
var layerXField = layerObjectCoordsGroup.add('statictext', undefined, "X:");
layerXField.enabled = false;
layerObjectCoordsGroup.add('statictext', undefined, layerInfo.x);
var layerYField = layerObjectCoordsGroup.add('statictext', undefined, "Y:");
layerYField.enabled = false;
layerObjectCoordsGroup.add('statictext', undefined, layerInfo.y);
var propertiesPanel = propxmp.add('panel', undefined, "Individual Properties");
propertiesPanel.alignChildren = 'right';
var propertyGroup = propertiesPanel.add('group');
propertyGroup.add('statictext', undefined, "Property:");
var property = propertyGroup.add('edittext', undefined, Pslib.DEFAULTPROPERTYNAME);
property.characters = 20;
propertyGroup.add('statictext', undefined, "Value:");
var propertyValue = propertyGroup.add('edittext', undefined, Pslib.DEFAULTPROPERTYVALUE );
propertyValue.characters = 20;
var namespacePanel = propxmp.add('panel', undefined, "Namespace");
namespacePanel.alignChildren = 'right';
var namespaceGroup = namespacePanel.add('group');
//namespaceGroup.add('statictext', undefined, "Namespace:");
var namespace = namespaceGroup.add('edittext', undefined, Pslib.XMPNAMESPACE);
namespace.characters = 20;
namespaceGroup.add('statictext', undefined, "Prefix:");
var namespacePrefix = namespaceGroup.add('edittext', undefined, Pslib.XMPNAMESPACEPREFIX);
namespacePrefix.characters = 20;
var registerNamespace = namespaceGroup.add('button', undefined, "Register Namespace");
var propertyDebug = propertiesPanel.add('statictext', undefined, "");
propertyDebug.characters = 75;
propertyDebug.enabled = false;
var dataButtonsGroup = propertiesPanel.add('group');
var getValue = dataButtonsGroup.add('button', undefined, "Get");
var setValue = dataButtonsGroup.add('button', undefined, "Set");
var removeProperty = dataButtonsGroup.add('button', undefined, "Remove");
var xmpDisplay = maingroup.add('group');
xmpDisplay.alignChildren = 'fill';
var xmpDisplayPanel = xmpDisplay.add('panel', undefined, "XMPMeta Object");
xmpDisplayPanel.alignChildren = 'left';
var xmpDisplayText = xmpDisplayPanel.add("edittext", undefined, XMPObj);
xmpDisplayText.characters = 80;
xmpDisplayText.multiline = true;
xmpDisplayText.preferredSize.height = 350;
xmpDisplayText.graphics.foregroundColor = xmpDisplayText.graphics.newPen (xmpDisplayText.graphics.PenType.SOLID_COLOR, [1, 1, 1], 1);
xmpDisplayText.graphics.backgroundColor = xmpDisplayText.graphics.newBrush (xmpDisplayText.graphics.BrushType.SOLID_COLOR, [0.4, 0.4, 0.4]);
var xmpDebug = xmpDisplayPanel.add('statictext', undefined, "");
xmpDebug.characters = 75;
xmpDebug.enabled = false;
var xmpButtonsGroup = xmpDisplayPanel.add('group');
var addXMPObject = xmpButtonsGroup.add('button', undefined, "Add Empty XMP to Layer");
var getXMPObject = xmpButtonsGroup.add('button', undefined, "Get XMP from Layer");
// var iterateXMPObject = xmpButtonsGroup.add('button', undefined, "Iterate");
var saveToXML = xmpButtonsGroup.add('button', undefined, "Save XMP to XML");
saveToXML.enabled = exportXML;
saveToXML.helpTip = "You should save your document before using this feature.";
// var removeNamespace = xmpButtonsGroup.add('button', undefined, "Remove Namespace");
// var removeXMPObject = xmpButtonsGroup.add('button', undefined, "Remove XMP");
//
// individual properties callbacks
//
// get value for specified property
getValue.onClick = function()
{
var prop = property.text
var val = propertyValue.text;
if(prop != "")
{
if(!layerObject.isBackgroundLayer)
{
// check for existing XMP object
XMPObj = Pslib.getXmp(layerObject);
if(XMPObj != null)
{
var data = Pslib.getXmpProperty(layerObject, prop);
propertyValue.text = data != undefined ? data : propertyValue.text;
if(data == undefined) propertyDebug.text = "[GET PROPERTY: Property not found]";
XMPObj = new XMPMeta( layerObject.xmpMetadata.rawData);
xmpDisplayText.text = XMPObj.serialize().replace(XmpWhitespace, "");
}
else
{
var msg = "[GET PROPERTY: No XMP object found on layer]";
xmpDebug.text = msg;
xmpDisplayText.text = msg;
}
}
else
{
var msg = "[GET PROPERTY: A background layer cannot contain an XMP object]";
xmpDebug.text = msg;
xmpDisplayText.text = msg;
}
}
else
{
var msg = "[GET PROPERTY: Please enter a property name]";
propertyDebug.text = msg;
xmpDisplayText.text = msg;
}
};
// set value for specified property -- creates property if not found
setValue.onClick = function()
{
if(property.text != "" && propertyValue.text != "" && propertyValue.text != "[INVALID]")
{
if(!layerObject.isBackgroundLayer)
{
XMPObj = Pslib.getXmp (layerObject);
if(XMPObj != null)
{
var propertyExists;
var testXmp;
try
{
testXmp = new XMPMeta( layerObject.xmpMetadata.rawData );
propertyExists = testXmp.doesPropertyExist(Pslib.XMPNAMESPACE, property.text);
}
catch(e)
{
exists = false;
}
if(Pslib.setXmpProperties(layerObject, [[property.text, propertyValue.text]]))
{
propertyDebug.text = "[ADD PROPERTY: Property " + (propertyExists? "updated" : "added") + " successfully]";
XMPObj = new XMPMeta( layerObject.xmpMetadata.rawData);
xmpDisplayText.text = XMPObj.serialize().replace(XmpWhitespace, "");
}
else
{
var msg = "[ADD PROPERTY: Error adding property]";
propertyDebug.text = msg;
xmpDisplayText.text = msg;
}
}
else
{
var msg = "[ADD PROPERTY: XMP Object not found.]";
propertyDebug.text = msg;
xmpDisplayText.text = msg;
}
}
else
{
var msg = "[ADD PROPERTY: A background layer cannot contain metadata]";
xmpDebug.text = msg;
xmpDisplayText.text = msg;
}
}
else
{
var msg = "[ADD PROPERTY: Provided value is either invalid or empty]";
propertyDebug.text = msg;
xmpDisplayText.text = msg;
}
};
// remove specified property
removeProperty.onClick = function()
{
if(property.text != "")
{
if(!layerObject.isBackgroundLayer)
{
if(Pslib.deleteXmpProperty(layerObject, property.text))
{
propertyDebug.text = "[REMOVE PROPERTY: Property removed successfully]";
XMPObj = new XMPMeta( layerObject.xmpMetadata.rawData);
xmpDisplayText.text = XMPObj.serialize().replace(XmpWhitespace, "");
}
else
{
var msg = "[REMOVE PROPERTY: Error removing property]";
propertyDebug.text = msg;
xmpDisplayText.text = msg;
}
}
else
{
var msg = "[REMOVE PROPERTY: A background layer cannot contain an XMP object]";
xmpDebug.text = msg;
xmpDisplayText.text = msg;
}
}
else
{
var msg = "[REMOVE PROPERTY: Please enter a property name]";
xmpDebug.text = msg;
xmpDisplayText.text = msg;
}
};
namespace.onChanging = function()
{
Pslib.XMPNAMESPACE = namespace.text;
propertyDebug.text = "[NAMESPACE: " + Pslib.XMPNAMESPACE + " PREFIX: " + Pslib.XMPNAMESPACEPREFIX + "]";
};
namespacePrefix.onChanging = function()
{
Pslib.XMPNAMESPACEPREFIX = namespacePrefix.text;
propertyDebug.text = "[NAMESPACE: " + Pslib.XMPNAMESPACE + " PREFIX: " + Pslib.XMPNAMESPACEPREFIX + "]";
};
registerNamespace.onClick = function()
{
try
{
XMPMeta.registerNamespace(Pslib.XMPNAMESPACE, Pslib.XMPNAMESPACEPREFIX);
propertyDebug.text = "[REGISTERING NAMESPACE: " + Pslib.XMPNAMESPACE + " PREFIX: " + Pslib.XMPNAMESPACEPREFIX + " SUCCESS]";
}
catch(e)
{
propertyDebug.text = "[ERROR REGISTERING NAMESPACE]";
}
}
//
// XMP object callbacks
//
// add XMP object to provided layer
addXMPObject.onClick = function()
{
if(!layerObject.isBackgroundLayer)
{
var hasXMP = Pslib.getXmp(layerObject);
// if layer already has XMP, confirm with user before replacing it
if(hasXMP != null)
{
// confirm replacement of current XMP object
if(confirm ("Do you wish to replace the existing XMP object by a new one?"))
{
XMPObj = new XMPMeta( );
layerObject.xmpMetadata.rawData = XMPObj.serialize();
xmpDisplayText.text = XMPObj.serialize().replace(XmpWhitespace, "");
xmpDebug.text = "[ADD XMP: Current layer XMP object successfully replaced by empty XMP object]";
}
else
{
xmpDebug.text = "[ADD XMP: Current layer XMP object preserved]";
XMPObj = new XMPMeta( layerObject.xmpMetadata.rawData);
xmpDisplayText.text = XMPObj.serialize().replace(XmpWhitespace, "");
}
}
// if layer does NOT have XMP
else
{
XMPObj = new XMPMeta();
layerObject.xmpMetadata.rawData = XMPObj.serialize();
xmpDisplayText.text = XMPObj.serialize().replace(XmpWhitespace, "");
xmpDebug.text = "[ADD XMP: XMP object successfully added to layer]";
}
}
else
{
var msg = "[ADD XMP: Cannot add XMP object to a background layer]";
xmpDebug.text = msg;
xmpDisplayText.text = msg;
}
};
// get XMP object
getXMPObject.onClick = function()
{
if(!layerObject.isBackgroundLayer)
{
// get existing XMP Object
if(Pslib.getXmp(layerObject))
{
xmpDebug.text = "[GET XMP: Existing XMP object successfully harvested from layer]";
XMPObj = new XMPMeta( layerObject.xmpMetadata.rawData);
xmpDisplayText.text = XMPObj.serialize().replace(XmpWhitespace, "");
}
else
{
var msg = "[GET XMP: No XMP object found on layer]";
xmpDebug.text = msg;
xmpDisplayText.text = msg;
}
}
else
{
var msg = "[GET XMP: A background layer cannot contain an XMP object]";
xmpDebug.text = msg;
xmpDisplayText.text = msg;
}
};
//~ // iterate through XMP properties
//~ iterateXMPObject.onClick = function()
//~ {
//~ if(!layerObject.isBackgroundLayer)
//~ {
//~ // get existing XMP Object
//~ if(Pslib.getXmp(layerObject))
//~ {
//~ xmpDebug.text = "[ITERATE XMP: Existing XMP object successfully harvested from layer]";
//~ XMPObj = new XMPMeta( layerObject.xmpMetadata.rawData);
//~ var iterator = XMPObj.iterator(undefined, Pslib.XMPNAMESPACE, "color"); // ************ ????
//~ alert(iterator.next());
/*
next()
XMPIteratorObj.next ( )
Retrieves the next item in the metadata.
Returns an XMPProperty object, or null if there are no more items.
skipSiblings()
XMPIteratorObj.skipSiblings ( )
Skips the subtree below and the siblings of the current node on the subsequent call to next(). Returns undefined.
skipSubtree()
XMPIteratorObj.skipSubtree ( )
Skips the subtree below the current node on the subsequent call to next(). Returns undefined.
*/
//~ xmpDisplayText.text = XMPObj.serialize().replace(XmpWhitespace, "");
//~ }
//~ else
//~ {
//~ var msg = "[ITERATE XMP: No XMP object found on layer]";
//~ xmpDebug.text = msg;
//~ xmpDisplayText.text = msg;
//~ }
//~ }
//~ else
//~ {
//~ var msg = "[ITERATE XMP: A background layer cannot contain an XMP object]";
//~ xmpDebug.text = msg;
//~ xmpDisplayText.text = msg;
//~ }
//~ }
// save XMP object to XML file
saveToXML.onClick = function()
{
if(!layerObject.isBackgroundLayer)
{
if(Pslib.exportLayerMetadata(layerObject, doc.path + "/" + doc.name + "_" + layerObject.name + ".xml", false))
{
xmpDebug.text = "[SAVE XML: XMP object successfully exported to XML file]";
XMPObj = new XMPMeta( layerObject.xmpMetadata.rawData);
xmpDisplayText.text = XMPObj.serialize().replace(XmpWhitespace, "");
}
else
{
var msg = "[SAVE XML: No XMP object found on layer]";
xmpDebug.text = msg;
xmpDisplayText.text = msg;
}
}
else
{
var msg = "[SAVE XML: A background layer cannot contain an XMP object]";
xmpDebug.text = msg;
xmpDisplayText.text = msg;
}
};
// remove XMP object from layer (replaces the existing XMP by an empty one)
//~ removeXMPObject.onClick = function()
//~ {
//~ if(!layerObject.isBackgroundLayer)
//~ {
//~ var testXmp;
//~ var clearXMP = Pslib.clearXmp(layerObject);
//~
//~ if(clearXMP)
//~ {
//~ // need one more try/catch block here, because Pslib.clearXmp also returns true when there is no XMP object attached to the layer
//~ try
//~ {
//~ //testXmp = new XMPMeta( layerObject.xmpMetadata.rawData );
//~ XMPObj = new XMPMeta( layerObject.xmpMetadata.rawData);
//~ layerObject.xmpMetadata.rawData = XMPObj.serialize();
//~ xmpDisplayText.text = XMPObj.serialize().replace(XmpWhitespace, "");
//~ xmpDebug.text = "[REMOVE XMP: layer XMP object successfully replaced by empty XMP object]";
//~ }
//~ catch(e)
//~ {
//~ var msg = "[REMOVE XMP: No XMP object found on layer]";
//~ xmpDisplayText.text = msg;
//~ xmpDebug.text = msg;
//~ }
//~ }
//~ else
//~ {
//~ var msg = "[REMOVE XMP: Error removing XMP object from layer]";
//~ xmpDebug.text = msg;
//~ xmpDisplayText.text = msg;
//~ }
//~ }
//~ else
//~ {
//~ var msg = "[REMOVE XMP: A background layer cannot contain metadata]";
//~ xmpDebug.text = msg;
//~ xmpDisplayText.text = msg;
//~ }
//~ };
// remove specific namespace from XMP object
//~ removeNamespace.onClick = function()
//~ {
//~ if(!layerObject.isBackgroundLayer)
//~ {
//~ var testXmp;
//~ var clearXMP = Pslib.clearXmp(layerObject);
//~
//~ if(clearXMP)
//~ {
//~ // need one more try/catch block here, because Pslib.clearXmp also returns true when there is no XMP object attached to the layer
//~ try
//~ {
//~ Pslib.clearNamespace(layerObject, Pslib.XMPNAMESPACE);
//~ XMPObj = new XMPMeta(layerObject.xmpMetadata.rawData);
//~ // layerObject.xmpMetadata.rawData
//~ // layerObject.xmpMetadata.rawData = XMPObj.serialize();
//~ xmpDisplayText.text = XMPObj.serialize().replace(XmpWhitespace, "");
//~
//~ //var xmp = new XMPMeta(layer.xmpMetadata.rawData);
//~ //XMPUtils.removeProperties(xmp, namespace, undefined, XMPConst.REMOVE_ALL_PROPERTIES);
//~ //layer.xmpMetadata.rawData = xmp.serialize();
//~
//~ xmpDebug.text = "[REMOVE NAMESPACE: successful]";
//~ }
//~ catch(e)
//~ {
//~ var msg = "[REMOVE NAMESPACE: No XMP object found on layer]";
//~ xmpDisplayText.text = msg;
//~ xmpDebug.text = msg;
//~ }
//~ }
//~ else
//~ {
//~ var msg = "[REMOVE NAMESPACE: Error removing XMP object from layer]";
//~ xmpDebug.text = msg;
//~ xmpDisplayText.text = msg;
//~ }
//~ }
//~ else
//~ {
//~ var msg = "[REMOVE NAMESPACE: A background layer cannot contain metadata]";
//~ xmpDebug.text = msg;
//~ xmpDisplayText.text = msg;
//~ }
//~ };
// load XMP object
getXMPObject.onClick();
// show dialog
win.center();
win.show();
};
//
// Main app execution
//
if(app.documents.length)
{
var doc = app.activeDocument;
var layerObject = doc.activeLayer;
var layerInfo = {};
if(layerObject != undefined)
{
layerInfo.name = layerObject.name;
layerInfo.typename = layerObject.typename;
layerInfo.kind = layerObject.typename == "LayerSet" ? "LayerSet" : layerObject.kind;
var b = layerObject.bounds;
layerInfo.width = b[2].as('px') - b[0].as('px');
layerInfo.height = b[3].as('px') - b[1].as('px');
layerInfo.x = b[0].as('px');
layerInfo.y = b[1].as('px');
}
var exportXML;
// this is to determine where to save the XML file
try
{
var docpath = doc.path;
exportXML = true;
}
catch(e)
{
exportXML = false;
}
Main();
}
else
{
alert("No available document!");
}