-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtextSubstitutions.jsx
More file actions
2155 lines (1757 loc) · 94.6 KB
/
textSubstitutions.jsx
File metadata and controls
2155 lines (1757 loc) · 94.6 KB
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
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
textSubstitutions.jsx
See repo for doccumentation:
https://github.com/9yz/bridge-scripts
*/
// start and end strings must be the same size
var TS_START_DELIM;
var TS_END_DELIM;
var TS_DELIM_SIZE;
var TS_DELIM_ENUM = "#";
var TS_DELIM_FUNC = ";";
const TS_DELIMITERS = [
// starting delim, ending delim, delim length.
["[", "]", 1],
["[[", "]]", 2],
["{", "}", 1],
["{{", "}}", 2],
["=", "=", 1],
["==", "==", 2],
]
const TS_VERSION = "1.2.1";
const TS_VERSION_PREFS = 100201; // equal to 1.002.01, or 1.2.1
var TS_SUB_TABLE_BUILTIN = [];
var TS_SUB_TABLE_BUILTIN_FUNCTIONS = [];
var TS_SUB_TABLE_USER = [];
var TS_LOADED_CUSTOM_FILES = [];
var TS_NUM_CUSTOM_RULES_LOADED = 0;
var TS_SCRIPTS_DIR;
var TS_RECURSIONS;
var TS_LAST_OP_TIMER = 0; // time it took to complete last operation
var TS_LAST_OP_FILES = 0; // number of files processed in the last operation
// basically just an enum
// the categories of metadata fields
const TS_PROPERTY_CATEGORIES = {
core: 1,
credit: 2,
misc: 4,
filename: 8,
};
var TS_USER_PROP_CATS_SELECTON
#target bridge
// STARTUP FUNCTION: run when bridge starts, used for setup
if(BridgeTalk.appName == 'bridge'){
try{
if(Folder.fs == "Windows"){
if( xmpLib == undefined ) var pathToLib = Folder.startup.fsName + "/AdobeXMPScript.dll"; // Load the XMP Script library
TS_SCRIPTS_DIR = Folder.userData; // %APPDATA%
TS_SCRIPTS_DIR.changePath("./Adobe/Bridge 2025/Startup Scripts/");
}
else {
if( xmpLib == undefined ) var pathToLib = Folder.startup.fsName + "/AdobeXMPScript.framework"; // Load the XMP Script library
TS_SCRIPTS_DIR = Folder.userData; // ~/Library/Application Support
TS_SCRIPTS_DIR.changePath("./Adobe/Bridge 2025/Startup Scripts/");
}
var libfile = new File( pathToLib );
var xmpLib = new ExternalObject("lib:" + pathToLib );
app.addLegalNotice("TextSubstitutions", "View contributors at https://github.com/9yz/bridge-scripts");
var tsMenuRun = MenuElement.create('command', 'Text Substitutions...', 'at the end of Tools');
var tsMenuRunCont = MenuElement.create('command', 'Text Substitutions...', 'after Thumbnail/Open');
tsInitalizePrefs();
tsPrefsPanel();
tsBuildSubstitutionTables();
}
catch(e){
alert("Text Substitutions Error:\n" + e + ' ' + e.line);
}
}
// called when text substitutions is selected in menu
tsMenuRun.onSelect = function(){
tsRun();
}
tsMenuRunCont.onSelect = function(){
tsRun();
}
function tsPrefsPanel(){
// Event handler; called when prefs panel is opened
var tsPrefHandler = function(event){
// Can only add a panel when the Preferences dialog opens
if(event.type == "create" && event.location == "prefs"){
/*
Code for Import https://scriptui.joonas.me — (Triple click to select):
{"activeId":38,"items":{"item-0":{"id":0,"type":"Dialog","parentId":false,"style":{"enabled":true,"varName":"tsPrefsPanelObject","windowType":"Window","creationProps":{"su1PanelCoordinates":true,"maximizeButton":false,"minimizeButton":false,"independent":false,"closeButton":true,"borderless":false,"resizeable":false},"text":"Dialog","preferredSize":[400,0],"margins":16,"orientation":"column","spacing":10,"alignChildren":["left","top"]}},"item-1":{"id":1,"type":"Panel","parentId":7,"style":{"enabled":true,"varName":"panelDelimiter","creationProps":{"borderStyle":"black","su1PanelCoordinates":false},"text":"Code Delimiter","preferredSize":[0,0],"margins":10,"orientation":"column","spacing":10,"alignChildren":["fill","top"],"alignment":null}},"item-2":{"id":2,"type":"RadioButton","parentId":1,"style":{"enabled":true,"varName":"rbSingleBrackets","text":"[Single Brackets]","preferredSize":[0,0],"alignment":null,"helpTip":null,"checked":true}},"item-3":{"id":3,"type":"RadioButton","parentId":1,"style":{"enabled":true,"varName":"rbDoubleBrackets","text":"[[Double Brackets]] ","preferredSize":[0,0],"alignment":null,"helpTip":null,"checked":false}},"item-4":{"id":4,"type":"RadioButton","parentId":1,"style":{"enabled":true,"varName":"rbSingleCurly","text":"{Curly Brackets}","preferredSize":[0,0],"alignment":null,"helpTip":null,"checked":false}},"item-5":{"id":5,"type":"RadioButton","parentId":1,"style":{"enabled":true,"varName":"rbDoubleCurly","text":"{{Double Curlies}}","preferredSize":[0,0],"alignment":null,"helpTip":null,"checked":false}},"item-6":{"id":6,"type":"Panel","parentId":7,"style":{"enabled":true,"varName":"panelDateField","creationProps":{"borderStyle":"etched","su1PanelCoordinates":false},"text":"Get Creation Date From","preferredSize":[0,0],"margins":10,"orientation":"column","spacing":10,"alignChildren":["fill","top"],"alignment":null}},"item-7":{"id":7,"type":"Group","parentId":17,"style":{"enabled":true,"varName":null,"preferredSize":[0,0],"margins":0,"orientation":"column","spacing":10,"alignChildren":["left","top"],"alignment":null}},"item-8":{"id":8,"type":"RadioButton","parentId":6,"style":{"enabled":true,"varName":"rbUseEXIFDate","text":"EXIF (reccomended)","preferredSize":[0,0],"alignment":null,"helpTip":"Get date from EXIF. This field is updated by Bridge's \"Edit Capture Time\" feature."}},"item-9":{"id":9,"type":"RadioButton","parentId":6,"style":{"enabled":true,"varName":"rbUseIPTCDate","text":"IPTC","preferredSize":[0,0],"alignment":null,"helpTip":"Get date from IPTC. This field is NOT updated by Bridge's \"Edit Capture Time\" feature."}},"item-10":{"id":10,"type":"StaticText","parentId":6,"style":{"enabled":true,"varName":null,"creationProps":{},"softWrap":true,"text":"Select which field time-based substitutions like tDate and tTime should get the Creation Date from. ","justify":"left","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-11":{"id":11,"type":"RadioButton","parentId":1,"style":{"enabled":true,"varName":"rbSingleEquals","text":"=Single Equals=","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-12":{"id":12,"type":"RadioButton","parentId":1,"style":{"enabled":true,"varName":"rbDoubleEquals","text":"==Double Equals==","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-13":{"id":13,"type":"StaticText","parentId":6,"style":{"enabled":true,"varName":null,"creationProps":{},"softWrap":true,"text":"tEXIFTime and tIPTCTime will always get the ISO 8601 timestamp from their respective fields.","justify":"left","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-14":{"id":14,"type":"Divider","parentId":0,"style":{"enabled":true,"varName":null}},"item-15":{"id":15,"type":"StaticText","parentId":0,"style":{"enabled":true,"varName":null,"creationProps":{},"softWrap":true,"text":"View documentation and contribute to Text Substitutions at https://github.com/9yz/bridge-scripts","justify":"left","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-16":{"id":16,"type":"StaticText","parentId":1,"style":{"enabled":true,"varName":null,"creationProps":{},"softWrap":true,"text":"Set the delimiter used to identify substitutions. Don't forget to update any custom recursive substitutions when changing this setting!","justify":"left","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-17":{"id":17,"type":"Group","parentId":0,"style":{"enabled":true,"varName":null,"preferredSize":[0,0],"margins":0,"orientation":"row","spacing":10,"alignChildren":["left","center"],"alignment":null}},"item-18":{"id":18,"type":"Panel","parentId":19,"style":{"enabled":true,"varName":"panelSepTags","creationProps":{"borderStyle":"etched","su1PanelCoordinates":false},"text":"Separate Substitutions in Keywords","preferredSize":[0,0],"margins":10,"orientation":"column","spacing":10,"alignChildren":["fill","top"],"alignment":null}},"item-19":{"id":19,"type":"Group","parentId":17,"style":{"enabled":true,"varName":null,"preferredSize":[0,0],"margins":0,"orientation":"column","spacing":10,"alignChildren":["left","top"],"alignment":null}},"item-20":{"id":20,"type":"StaticText","parentId":18,"style":{"enabled":true,"varName":null,"creationProps":{},"softWrap":true,"text":"When set, substitutions used in the Keywords field containing commas will be split into multiple keywords.","justify":"left","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-21":{"id":21,"type":"StaticText","parentId":18,"style":{"enabled":true,"varName":null,"creationProps":{},"softWrap":true,"text":"For example, if the substitution [[foods]] is set to be substituted with \"apples, crackers\" and this setting is enabled, 2 tags will be created - one each for \"apples\" and \"crackers\". Otherwise, only one tag will be created","justify":"left","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-22":{"id":22,"type":"Checkbox","parentId":18,"style":{"enabled":true,"varName":"rbSepTags","text":"Separate Substitutions in Keywords","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-23":{"id":23,"type":"Panel","parentId":19,"style":{"enabled":true,"varName":"panelCustomSubs","creationProps":{"borderStyle":"etched","su1PanelCoordinates":false},"text":"Custom Substitutions","preferredSize":[0,0],"margins":10,"orientation":"column","spacing":10,"alignChildren":["left","top"],"alignment":"fill"}},"item-24":{"id":24,"type":"StaticText","parentId":23,"style":{"enabled":true,"varName":"txtFilesLoaded","creationProps":{},"softWrap":false,"text":"x custom substitution files loaded.\nx custom substitution rules loaded.","justify":"left","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-25":{"id":25,"type":"Button","parentId":23,"style":{"enabled":true,"varName":"butReloadCustSubs","text":"Reload Custom Substitutions","justify":"center","preferredSize":[0,0],"alignment":null,"helpTip":"This will purge all custom substitutions from memory and reload them from disk."}},"item-26":{"id":26,"type":"StaticText","parentId":23,"style":{"enabled":true,"varName":null,"creationProps":{"truncate":"none","multiline":false,"scrolling":false},"softWrap":false,"text":"All .txt files beginning with \"ts_\" in the Startup Scripts directory or /substitutions/ subdirectory will be loaded.","justify":"left","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-27":{"id":27,"type":"Panel","parentId":19,"style":{"enabled":true,"varName":"panelMaxRecursions","creationProps":{"borderStyle":"etched","su1PanelCoordinates":false},"text":"Max Substitutions","preferredSize":[0,0],"margins":10,"orientation":"column","spacing":10,"alignChildren":["left","top"],"alignment":"fill"}},"item-28":{"id":28,"type":"StaticText","parentId":27,"style":{"enabled":true,"varName":null,"creationProps":{"truncate":"none","multiline":false,"scrolling":false},"softWrap":false,"text":"Maximum number of substitutions in a single metadata field before an error occurs. Lower numbers could cause problems when many recursive substitutions are used. ","justify":"left","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-29":{"id":29,"type":"EditText","parentId":27,"style":{"enabled":true,"varName":"edMaxRecursions","creationProps":{"noecho":false,"readonly":false,"multiline":false,"scrollable":false,"borderless":true,"enterKeySignalsOnChange":false},"softWrap":false,"text":"100","justify":"left","preferredSize":[40,0],"alignment":null,"helpTip":null}},"item-30":{"id":30,"type":"Panel","parentId":31,"style":{"enabled":true,"varName":"panelPropertyCategories","creationProps":{"borderStyle":"etched","su1PanelCoordinates":false},"text":"Fields to Analyze","preferredSize":[0,0],"margins":10,"orientation":"column","spacing":10,"alignChildren":["left","top"],"alignment":null}},"item-31":{"id":31,"type":"Group","parentId":17,"style":{"enabled":true,"varName":null,"preferredSize":[0,0],"margins":0,"orientation":"column","spacing":10,"alignChildren":["left","top"],"alignment":null}},"item-32":{"id":32,"type":"StaticText","parentId":30,"style":{"enabled":true,"varName":null,"creationProps":{"truncate":"none","multiline":false,"scrolling":false},"softWrap":false,"text":"Only these fields will be checked for custom substitutions when the program is run. Reducing the number of fields analyzed will improve performance. Only fields in IPTC Core (not IPTC Extension) are analyzed.","justify":"left","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-34":{"id":34,"type":"Checkbox","parentId":30,"style":{"enabled":true,"varName":"cbPropCatCore","text":"Core","preferredSize":[0,0],"alignment":null,"helpTip":"Description, keywords, alt-text, extended description,\\nheadline, title, sublocation, city, state, country."}},"item-35":{"id":35,"type":"Checkbox","parentId":30,"style":{"enabled":true,"varName":"cbPropCatCredit","text":"Credit","preferredSize":[0,0],"alignment":null,"helpTip":"Creator contact info, credit, source,\\ndescription writer, rights, and usage terms."}},"item-36":{"id":36,"type":"Checkbox","parentId":30,"style":{"enabled":true,"varName":"cbPropCatMisc","text":"Misc","preferredSize":[0,0],"alignment":null,"helpTip":"IPTC Subject & Scene codes, intellectual genre, \\njob identifier, instructions."}},"item-37":{"id":37,"type":"StaticText","parentId":30,"style":{"enabled":true,"varName":null,"creationProps":{"truncate":"none","multiline":false,"scrolling":false},"softWrap":false,"text":"(hover for details)","justify":"left","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-38":{"id":38,"type":"Checkbox","parentId":30,"style":{"enabled":true,"varName":"cbPropCatFilename","text":"Filename","preferredSize":[0,0],"alignment":null,"helpTip":"The file's filename. Use caution\\nwhen changing file extensions."}}},"order":[0,17,7,1,16,2,3,4,5,11,12,6,10,13,8,9,19,18,20,21,22,23,26,25,24,27,28,29,31,30,32,38,34,35,36,37,14,15],"settings":{"importJSON":true,"indentSize":false,"cepExport":false,"includeCSSJS":true,"showDialog":true,"functionWrapper":false,"afterEffectsDockable":false,"itemReferenceList":"None"}}
*/
// TSPREFSPANELOBJECT
// ==================
var tsPrefsPanelObject = event.object.addPanel(" Text Substitutions");
tsPrefsPanelObject.text = "Dialog";
tsPrefsPanelObject.preferredSize.width = 600;
tsPrefsPanelObject.orientation = "column";
tsPrefsPanelObject.alignChildren = ["fill","top"];
tsPrefsPanelObject.spacing = 10;
tsPrefsPanelObject.margins = 16;
// GROUP1
// ======
var group1 = tsPrefsPanelObject.add("group", undefined, {name: "group1"});
group1.orientation = "row";
group1.alignChildren = ["left","top"];
group1.spacing = 10;
group1.margins = 0;
// GROUP2
// ======
var group2 = group1.add("group", undefined, {name: "group2"});
group2.preferredSize.width = 400;
group2.orientation = "column";
group2.alignChildren = ["fill","top"];
group2.spacing = 10;
group2.margins = 0;
// PANELDELIMITER
// ==============
var panelDelimiter = group2.add("panel", undefined, undefined, {name: "panelDelimiter", borderStyle: "black"});
panelDelimiter.text = "Code Delimiter";
panelDelimiter.orientation = "column";
panelDelimiter.alignChildren = ["fill","top"];
panelDelimiter.spacing = 10;
panelDelimiter.margins = 10;
var statictext1 = panelDelimiter.add("statictext", undefined, undefined, {name: "statictext1", multiline: true});
statictext1.text = "Set the delimiter used to identify substitutions. Don't forget to update any recursive custom substitutions when changing this setting!";
var rbSingleBrackets = panelDelimiter.add("radiobutton", undefined, undefined, {name: "rbSingleBrackets"});
rbSingleBrackets.text = "[Single Brackets]";
rbSingleBrackets.value = true;
var rbDoubleBrackets = panelDelimiter.add("radiobutton", undefined, undefined, {name: "rbDoubleBrackets"});
rbDoubleBrackets.text = "[[Double Brackets]] ";
var rbSingleCurly = panelDelimiter.add("radiobutton", undefined, undefined, {name: "rbSingleCurly"});
rbSingleCurly.text = "{Curly Brackets}";
var rbDoubleCurly = panelDelimiter.add("radiobutton", undefined, undefined, {name: "rbDoubleCurly"});
rbDoubleCurly.text = "{{Double Curlies}}";
var rbSingleEquals = panelDelimiter.add("radiobutton", undefined, undefined, {name: "rbSingleEquals"});
rbSingleEquals.text = "=Single Equals=";
var rbDoubleEquals = panelDelimiter.add("radiobutton", undefined, undefined, {name: "rbDoubleEquals"});
rbDoubleEquals.text = "==Double Equals==";
// initalize delimiter values
switch(app.preferences.tsDelimiter) {
case 0:
rbSingleBrackets.value = true;
break;
case 1:
rbDoubleBrackets.value = true;
break;
case 2:
rbSingleCurly.value = true;
break;
case 3:
rbDoubleCurly.value = true;
break;
case 4:
rbSingleEquals.value = true;
break;
case 5:
rbDoubleEquals.value = true;
break;
}
// update values on select
rbSingleBrackets.onClick = function(){
app.preferences.tsDelimiter = 0;
tsInitalizePrefsDelimiter();
butReloadCustSubs.onClick(); // reload cust subs
}
rbDoubleBrackets.onClick = function(){
app.preferences.tsDelimiter = 1;
tsInitalizePrefsDelimiter();
butReloadCustSubs.onClick();
}
rbSingleCurly.onClick = function(){
app.preferences.tsDelimiter = 2;
tsInitalizePrefsDelimiter();
butReloadCustSubs.onClick();
}
rbDoubleCurly.onClick = function(){
app.preferences.tsDelimiter = 3;
tsInitalizePrefsDelimiter();
butReloadCustSubs.onClick();
}
rbSingleEquals.onClick = function(){
app.preferences.tsDelimiter = 4;
tsInitalizePrefsDelimiter();
butReloadCustSubs.onClick();
}
rbDoubleEquals.onClick = function(){
app.preferences.tsDelimiter = 5;
tsInitalizePrefsDelimiter();
butReloadCustSubs.onClick();
}
var statictext2 = panelDelimiter.add("statictext", undefined, undefined, {name: "statictext1", multiline: true});
statictext2.text = "Updating this setting will also reload custom substitutions.";
// PANELDATEFIELD
// ==============
var panelDateField = group2.add("panel", undefined, undefined, {name: "panelDateField"});
panelDateField.text = "Get Creation Date From";
panelDateField.orientation = "column";
panelDateField.alignChildren = ["fill","top"];
panelDateField.spacing = 10;
panelDateField.margins = 10;
var statictext2 = panelDateField.add("statictext", undefined, undefined, {name: "statictext2", multiline: true});
statictext2.text = "Select which field time-based substitutions like tDate and tTime should get the Creation Date from. ";
statictext2.alignment = ["fill","top"];
var statictext3 = panelDateField.add("statictext", undefined, undefined, {name: "statictext3", multiline: true});
statictext3.text = "tEXIFTime and tIPTCTime will always get the ISO 8601 timestamp from their respective fields.";
statictext3.alignment = ["fill","top"];
var rbUseEXIFDate = panelDateField.add("radiobutton", undefined, undefined, {name: "rbUseEXIFDate"});
rbUseEXIFDate.helpTip = "Get date from EXIF. This field is updated by Bridge's \u0022Edit Capture Time\u0022 feature.";
rbUseEXIFDate.text = "EXIF (reccomended)";
var rbUseIPTCDate = panelDateField.add("radiobutton", undefined, undefined, {name: "rbUseIPTCDate"});
rbUseIPTCDate.helpTip = "Get date from IPTC. This field is NOT updated by Bridge's \u0022Edit Capture Time\u0022 feature.";
rbUseIPTCDate.text = "IPTC";
// initalize values
switch (app.preferences.tsDateField) {
case 0:
rbUseEXIFDate.value = true;
break;
case 1:
rbUseIPTCDate.value = true;
break;
}
// update values on select
rbUseEXIFDate.onClick = function(){
app.preferences.tsDateField = 0;
}
rbUseIPTCDate.onClick = function(){
app.preferences.tsDateField = 1;
}
// GROUP3
// ======
var group3 = group1.add("group", undefined, {name: "group3"});
group3.preferredSize.width = 400;
group3.orientation = "column";
group3.alignChildren = ["left","top"];
group3.spacing = 10;
group3.margins = 0;
// PANELSEPTAGS
// ============
var panelSepTags = group3.add("panel", undefined, undefined, {name: "panelSepTags"});
panelSepTags.text = "Separate Substitutions in Keywords";
panelSepTags.orientation = "column";
panelSepTags.alignChildren = ["left","top"];
panelSepTags.spacing = 10;
panelSepTags.margins = 10;
var statictext4 = panelSepTags.add("statictext", undefined, undefined, {name: "statictext4", multiline: true});
statictext4.text = "When set, substitutions used in the Keywords field containing commas will be split into multiple keywords.";
statictext4.alignment = ["fill","top"];
var statictext5 = panelSepTags.add("statictext", undefined, undefined, {name: "statictext5", multiline: true});
statictext5.text = "For example, if the substitution [[foods]] is set to be substituted with \u0022apples,crackers\u0022 and this setting is enabled, 2 tags will be created - one each for \u0022apples\u0022 and \u0022crackers\u0022. Otherwise, only one tag will be created.";
statictext5.alignment = ["fill","top"];
var cbSepTags = panelSepTags.add("checkbox", undefined, undefined, {name: "rbSepTags"});
cbSepTags.text = "Separate Substitutions in Keywords";
if(app.preferences.tsSeparateTags){ // initalize value
cbSepTags.value = true;
}
cbSepTags.onClick = function(){ // update values on click
app.preferences.tsSeparateTags = cbSepTags.value;
}
// PANELCUSTOMSUBS
// ===============
var panelCustomSubs = group3.add("panel", undefined, undefined, {name: "panelCustomSubs"});
panelCustomSubs.text = "Custom Substitutions";
panelCustomSubs.orientation = "column";
panelCustomSubs.alignChildren = ["left","top"];
panelCustomSubs.spacing = 10;
panelCustomSubs.margins = 10;
panelCustomSubs.alignment = ["fill","top"];
var statictext6 = panelCustomSubs.add("statictext", undefined, undefined, {name: "statictext6", multiline: true});
statictext6.text = "All .txt files beginning with \u0022ts_\u0022 in the Startup Scripts directory or /substitutions/ subdirectory will be loaded. On script run, substitutions are reloaded if changes were made to previously loaded files.";
statictext6.alignment = ["fill","top"];
var butReloadCustSubs = panelCustomSubs.add("button", undefined, undefined, {name: "butReloadCustSubs"});
butReloadCustSubs.helpTip = "This will purge all custom substitutions from memory and reload them from disk.";
butReloadCustSubs.text = "Reload Custom Substitutions";
var txtFilesLoaded = panelCustomSubs.add("statictext", undefined, undefined, {name: "txtFilesLoaded", multiline: true});
txtFilesLoaded.text = TS_LOADED_CUSTOM_FILES.length + " custom substitution files loaded.\n" + TS_NUM_CUSTOM_RULES_LOADED + " custom substitution rules loaded."
txtFilesLoaded.alignment = ["fill","top"];
butReloadCustSubs.onClick = function(){
txtFilesLoaded.text = "Loading...";
tsBuildCustomSubTables();
txtFilesLoaded.text = TS_LOADED_CUSTOM_FILES.length + " custom substitution files loaded.\n" + TS_NUM_CUSTOM_RULES_LOADED + " custom substitution rules loaded.";
}
// GROUP4
// ======
var group4 = group1.add("group", undefined, {name: "group4"});
group4.orientation = "column";
group4.alignChildren = ["left","top"];
group4.spacing = 10;
group4.margins = 0;
// PANELMAXRECURSIONS
// ==================
var panelMaxRecursions = group4.add("panel", undefined, undefined, {name: "panelMaxRecursions"});
panelMaxRecursions.text = "Max Substitutions";
panelMaxRecursions.orientation = "column";
panelMaxRecursions.alignChildren = ["left","top"];
panelMaxRecursions.spacing = 10;
panelMaxRecursions.margins = 10;
panelMaxRecursions.alignment = ["fill","top"];
var statictext7 = panelMaxRecursions.add("statictext", undefined, undefined, {name: "statictext7", multiline: true});
statictext7.text = "Maximum number of substitutions in a single metadata field before an error occurs. Prevents runaway recursion.";
var edMaxRecursions = panelMaxRecursions.add('edittext {properties: {name: "edMaxRecursions"}}');
edMaxRecursions.text = app.preferences.tsRecursionLimit;
edMaxRecursions.preferredSize.width = 40;
edMaxRecursions.onChange = function(){
if(!isNaN(parseInt(edMaxRecursions.text)))
app.preferences.tsRecursionLimit = parseInt(edMaxRecursions.text);
else{
app.preferences.tsRecursionLimit = 500
edMaxRecursions.text = 500;
}
}
// PANELPROPERTYCATEGORIES
// =======================
var panelPropertyCategories = group4.add("panel", undefined, undefined, {name: "panelPropertyCategories"});
panelPropertyCategories.text = "Fields to Analyze";
panelPropertyCategories.orientation = "column";
panelPropertyCategories.alignChildren = ["left","top"];
panelPropertyCategories.spacing = 10;
panelPropertyCategories.margins = 10;
var statictext8 = panelPropertyCategories.add("statictext", undefined, undefined, {name: "statictext8", multiline: true});
statictext8.text = "Only these fields will be checked for custom substitutions when the program is run. Reducing the number of fields analyzed will improve performance. Only fields in IPTC Core (not IPTC Extension) are analyzed.";
var cbPropCatCore = panelPropertyCategories.add("checkbox", undefined, undefined, {name: "cbPropCatCore"});
cbPropCatCore.helpTip = "Description, keywords, alt-text, extended description,\nheadline, title, sublocation, city, state, country, and country code.";
cbPropCatCore.text = "Core";
var cbPropCatCredit = panelPropertyCategories.add("checkbox", undefined, undefined, {name: "cbPropCatCredit"});
cbPropCatCredit.helpTip = "Creator contact info, credit, source,\ndescription writer, rights, and usage terms.";
cbPropCatCredit.text = "Credit";
var cbPropCatMisc = panelPropertyCategories.add("checkbox", undefined, undefined, {name: "cbPropCatMisc"});
cbPropCatMisc.helpTip = "IPTC subject & scene codes, intellectual genre,\njob identifier, and instructions.";
cbPropCatMisc.text = "Misc";
var cbPropCatFilename = panelPropertyCategories.add("checkbox", undefined, undefined, {name: "cbPropCatFilename"});
cbPropCatFilename.helpTip = "The file's filename. Use caution\nwhen changing file extensions.";
cbPropCatFilename.text = "Filename";
// initalize delimiter values
if(app.preferences.tsPropertyCategories & TS_PROPERTY_CATEGORIES.filename){
cbPropCatFilename.value = true;
}
if(app.preferences.tsPropertyCategories & TS_PROPERTY_CATEGORIES.core){
cbPropCatCore.value = true;
}
if(app.preferences.tsPropertyCategories & TS_PROPERTY_CATEGORIES.credit){
cbPropCatCredit.value = true;
}
if(app.preferences.tsPropertyCategories & TS_PROPERTY_CATEGORIES.misc){
cbPropCatMisc.value = true;
}
// update values on select
cbPropCatFilename.onClick = function(){
if(cbPropCatFilename.value) app.preferences.tsPropertyCategories |= TS_PROPERTY_CATEGORIES.filename;
else app.preferences.tsPropertyCategories ^= TS_PROPERTY_CATEGORIES.filename;
}
cbPropCatCore.onClick = function(){
if(cbPropCatCore.value) app.preferences.tsPropertyCategories |= TS_PROPERTY_CATEGORIES.core;
else app.preferences.tsPropertyCategories ^= TS_PROPERTY_CATEGORIES.core;
}
cbPropCatCredit.onClick = function(){
if(cbPropCatCredit.value) app.preferences.tsPropertyCategories |= TS_PROPERTY_CATEGORIES.credit;
else app.preferences.tsPropertyCategories ^= TS_PROPERTY_CATEGORIES.credit;
}
cbPropCatMisc.onClick = function(){
if(cbPropCatMisc.value) app.preferences.tsPropertyCategories |= TS_PROPERTY_CATEGORIES.misc;
else app.preferences.tsPropertyCategories ^= TS_PROPERTY_CATEGORIES.misc;
}
var statictext9 = panelPropertyCategories.add("statictext", undefined, undefined, {name: "statictext9"});
statictext9.text = "(hover for details)";
// TSPREFSPANELOBJECT
// ==================
var divider1 = tsPrefsPanelObject.add("panel", undefined, undefined, {name: "divider1"});
divider1.alignment = "fill";
var statictext3 = tsPrefsPanelObject.add("group", undefined , {name: "statictext3"});
statictext3.getText = function() { var t=[]; for ( var n=0; n<statictext3.children.length; n++ ) { var text = statictext3.children[n].text || ''; if ( text === '' ) text = ' '; t.push( text ); } return t.join('\n'); };
statictext3.orientation = "column";
statictext3.alignChildren = ["left","center"];
statictext3.spacing = 0;
// uses a newline or the descender in the 'g' in github will get cut off
statictext3.add("statictext", undefined, "Version " + TS_VERSION + ". Last operation on " + TS_LAST_OP_FILES + " files completed in " + TS_LAST_OP_TIMER.toFixed(2) + "s. See docs and contribute at github.com/9yz/bridge-scripts\n");
// statictext3.add("statictext", undefined, "View documentation and contribute to Text Substitutions at https://github.com/9yz/bridge-scripts");
tsPrefsPanelObject.layout.layout(true); // not really sure what this does but without it nothing works so ¯\_(ツ)_/¯
}
return { handled: false };
};
// Register the event handler
app.eventHandlers.push( { handler: tsPrefHandler } );
return true;
}
// reset prefs to defaults
// if allPrefs = true, all prefs are set (optional)
function tsSetDefaultPrefs(allPrefs){
if(!allPrefs) allPrefs = false; // set to false if not specified
if(allPrefs || app.preferences.tsPrefsVersion < 100100){
app.preferences.tsPropertyCategories = TS_PROPERTY_CATEGORIES.core;
}
if(allPrefs || app.preferences.tsPrefsVersion < 100001){
app.preferences.tsDelimiter = 1; // int representing the `delimiters` array index of the delimiter to use
app.preferences.tsDateField = 0; // 0 = EXIF, 1 = IPTC
app.preferences.tsSeparateTags = 0; // 1 = seperate tags
app.preferences.tsRecursionLimit = 500; // max number of recursions before error
}
app.preferences.tsPrefsVersion = TS_VERSION_PREFS;
app.preferences.tsPrefsSet = true;
}
// set vars based on prefs
function tsInitalizePrefs(){
// set default prefs if they havent been set
if(app.preferences.tsPrefsSet != true) tsSetDefaultPrefs(true); // first run
else if(app.preferences.tsPrefsVersion < TS_VERSION_PREFS) tsSetDefaultPrefs(); // upgrading
tsInitalizePrefsDelimiter();
}
// set delimiter prefs based on prefs
function tsInitalizePrefsDelimiter(){
switch(app.preferences.tsDelimiter) {
case 0:
TS_START_DELIM = TS_DELIMITERS[0][0];
TS_END_DELIM = TS_DELIMITERS[0][1];
TS_DELIM_SIZE = TS_DELIMITERS[0][2];
break;
case 1:
TS_START_DELIM = TS_DELIMITERS[1][0];
TS_END_DELIM = TS_DELIMITERS[1][1];
TS_DELIM_SIZE = TS_DELIMITERS[1][2];
break;
case 2:
TS_START_DELIM = TS_DELIMITERS[2][0];
TS_END_DELIM = TS_DELIMITERS[2][1];
TS_DELIM_SIZE = TS_DELIMITERS[2][2];
break;
case 3:
TS_START_DELIM = TS_DELIMITERS[3][0];
TS_END_DELIM = TS_DELIMITERS[3][1];
TS_DELIM_SIZE = TS_DELIMITERS[3][2];
break;
case 4:
TS_START_DELIM = TS_DELIMITERS[4][0];
TS_END_DELIM = TS_DELIMITERS[4][1];
TS_DELIM_SIZE = TS_DELIMITERS[4][2];
break;
case 5:
TS_START_DELIM = TS_DELIMITERS[5][0];
TS_END_DELIM = TS_DELIMITERS[5][1];
TS_DELIM_SIZE = TS_DELIMITERS[5][2];
break;
}
}
// Build builtin and custom substitution tables
function tsBuildSubstitutionTables(){
const builtinTableSize = 223; // size we want for the hashtable - should be a prime at least 2x the size of builtinCommands.
const builtinCommands = [ // map of all program-defined substitutions
// time-based substitutions
{ target: "tdate", replacement: tsTDateTaken },
{ target: "tdatep", replacement: tsTDateTakenPretty },
{ target: "tdatepretty", replacement: tsTDateTakenPretty },
{ target: "tdateps", replacement: tsTDateTakenPrettyShort },
{ target: "tdateprettyshort", replacement: tsTDateTakenPrettyShort },
{ target: "tday", replacement: tsTDateTakenDay },
{ target: "tdayp", replacement: tsTDateTakenDayPretty },
{ target: "tdaypretty", replacement: tsTDateTakenDayPretty },
{ target: "tdayofweek", replacement: tsTDateTakenDayPretty },
{ target: "tdow", replacement: tsTDateTakenDayPretty },
{ target: "tmonth", replacement: tsTDateTakenMonth },
{ target: "tmonthp", replacement: tsTDateTakenMonthPretty },
{ target: "tmonthpretty", replacement: tsTDateTakenMonthPretty },
{ target: "tmonthps", replacement: tsTDateTakenMonthPrettyShort },
{ target: "tmonthprettyshort", replacement: tsTDateTakenMonthPrettyShort },
{ target: "tyear", replacement: tsTDateTakenYear },
{ target: "tyr", replacement: tsTDateTakenYear },
{ target: "tyearshort", replacement: tsTDateTakenYearShort },
{ target: "tyrs", replacement: tsTDateTakenYearShort },
{ target: "ttime12", replacement: tsTTimeTaken12 },
{ target: "ttime24", replacement: tsTTimeTaken24 },
{ target: "ttime", replacement: tsTTimeTaken24 },
{ target: "tdayhalf", replacement: tsTDayHalf },
{ target: "tampm", replacement: tsTDayHalf },
{ target: "ttod", replacement: tsTTimeOfDay },
{ target: "ttimeofday", replacement: tsTTimeOfDay },
{ target: "thr", replacement: tsTHour },
{ target: "thour", replacement: tsTHour },
{ target: "thour12", replacement: tsTHour12 },
{ target: "tmin", replacement: tsTMinute },
{ target: "tminute", replacement: tsTMinute },
{ target: "tsec", replacement: tsTSecond },
{ target: "tsecond", replacement: tsTSecond },
{ target: "tdt", replacement: tsTDateTime },
{ target: "tdatetime", replacement: tsTDateTime },
{ target: "texiftime", replacement: tsTEXIFTime },
{ target: "tiptctime", replacement: tsTIPTCTime },
// metadata-based substitutions
{ target: "mname", replacement: tsMFileName },
{ target: "mfile", replacement: tsMFileName },
{ target: "mfilename", replacement: tsMFileName },
{ target: "mfilenameshort", replacement: tsMFileNameShort },
{ target: "mnameshort", replacement: tsMFileNameShort },
{ target: "mfileshort", replacement: tsMFileNameShort },
{ target: "mfiles", replacement: tsMFileNameShort },
{ target: "mnames", replacement: tsMFileNameShort },
{ target: "mfoldername", replacement: tsMFolderName },
{ target: "mfolder", replacement: tsMFolderName },
{ target: "mextension", replacement: tsMExtension },
{ target: "mfiletype", replacement: tsMExtension },
{ target: "mtitle", replacement: tsMTitle },
{ target: "mheadline", replacement: tsMHeadline },
{ target: "mcredit", replacement: tsMCreditLine },
{ target: "mcreditline", replacement: tsMCreditLine },
{ target: "msublocation", replacement: tsMSublocation },
{ target: "mlocation", replacement: tsMLocation },
{ target: "mcity", replacement: tsMCity },
{ target: "mstate", replacement: tsMState },
{ target: "mregion", replacement: tsMState },
{ target: "mprovince", replacement: tsMState },
{ target: "mcountry", replacement: tsMCountry },
{ target: "mrating", replacement: tsMRating },
{ target: "mratingpretty", replacement: tsMRatingPretty },
{ target: "mratingp", replacement: tsMRatingPretty },
{ target: "mlabel", replacement: tsMLabel },
{ target: "msource", replacement: tsMSource },
{ target: "mcaptionwriter", replacement: tsMCaptionWriter },
{ target: "mdescwriter", replacement: tsMCaptionWriter },
{ target: "mcopyrightnotice", replacement: tsMCopyrightNotice },
{ target: "mcopyright", replacement: tsMCopyrightNotice },
{ target: "mrightsusageterms", replacement: tsMUsageTerms },
{ target: "musageterms", replacement: tsMUsageTerms },
{ target: "mjobidentifier", replacement: tsMJobIdentifier },
{ target: "mjobident", replacement: tsMJobIdentifier },
{ target: "mjob", replacement: tsMJobIdentifier },
{ target: "minstructions", replacement: tsMInstructions },
{ target: "mcreator", replacement: tsMCreator },
{ target: "mcreatorjob", replacement: tsMCreatorJob },
{ target: "mcreatoraddress", replacement: tsMCreatorAddress },
{ target: "mcreatorcity", replacement: tsMCreatorCity },
{ target: "mcreatorregion", replacement: tsMCreatorState },
{ target: "mcreatorprovince", replacement: tsMCreatorState },
{ target: "mcreatorstate", replacement: tsMCreatorState },
{ target: "mcreatorcountry", replacement: tsMCreatorCountry },
{ target: "mcreatorzip", replacement: tsMCreatorPostalCode },
{ target: "mcreatorpostal", replacement: tsMCreatorPostalCode },
{ target: "mcreatorphone", replacement: tsMCreatorPhone },
{ target: "mcreatoremail", replacement: tsMCreatorEmail },
{ target: "mcreatorwebsite", replacement: tsMCreatorWebsite },
{ target: "mdescription", replacement: tsMDescription },
{ target: "mdesc", replacement: tsMDescription },
// camera-based substitutions
{ target: "cwidth", replacement: tsCWidth },
{ target: "cw", replacement: tsCWidth },
{ target: "cheight", replacement: tsCHeight },
{ target: "ch", replacement: tsCHeight },
{ target: "ccamera", replacement: tsCCamera },
{ target: "ccam", replacement: tsCCamera },
{ target: "cserial", replacement: tsCSerial },
{ target: "clens", replacement: tsCLens },
{ target: "cshutterspeed", replacement: tsCShutterSpeed },
{ target: "cshutter", replacement: tsCShutterSpeed },
{ target: "css", replacement: tsCShutterSpeed },
{ target: "caperture", replacement: tsCAperture },
{ target: "cf", replacement: tsCAperture },
{ target: "ciso", replacement: tsCISO },
{ target: "cfocallength", replacement: tsCFocalLength },
{ target: "czoom", replacement: tsCFocalLength },
{ target: "cfocallength35", replacement: tsCFocalLength35 },
{ target: "czoom35", replacement: tsCFocalLength35 },
{ target: "cexposurecomp", replacement: tsCExposureComp },
{ target: "cexpcomp", replacement: tsCExposureComp },
{ target: "ccomp", replacement: tsCExposureComp },
// special
{ target: "", replacement: tsBlank },
]
const builtinFunctionsTableSize = 139; // size we want for the hashtable - should be a prime at least 2x the size of the associated table.
const builtinFunctions = [ // map of built-in functions
// math ops
{ target: "fadd", replacement: tsFAdd },
{ target: "f+", replacement: tsFAdd },
{ target: "fsub", replacement: tsFSub },
{ target: "f-", replacement: tsFSub },
{ target: "fmul", replacement: tsFMul },
{ target: "f*", replacement: tsFMul },
{ target: "fdiv", replacement: tsFDiv },
{ target: "f/", replacement: tsFDiv },
{ target: "fmod", replacement: tsFMod },
{ target: "f%", replacement: tsFMod },
{ target: "ffloor", replacement: tsFFloor },
{ target: "fceil", replacement: tsFCeil },
{ target: "fround", replacement: tsFRound },
// string ops
{ target: "fprefix", replacement: tsFPrefix },
{ target: "fpfx", replacement: tsFPrefix },
{ target: "fsuffix", replacement: tsFSuffix },
{ target: "fsfx", replacement: tsFSuffix },
{ target: "fsubstring", replacement: tsFSubstring },
{ target: "fsubstr", replacement: tsFSubstring },
{ target: "flength", replacement: tsFLength },
{ target: "flen", replacement: tsFLength },
{ target: "fgetindex", replacement: tsFGetIndex },
{ target: "findex", replacement: tsFGetIndex },
{ target: "findexof", replacement: tsFGetIndex },
{ target: "fgetlastindex", replacement: tsFGetLastIndex },
{ target: "flastindex", replacement: tsFGetLastIndex },
{ target: "flastindexof", replacement: tsFGetLastIndex },
{ target: "ffindreplace", replacement: tsFFindReplace },
{ target: "freplace", replacement: tsFFindReplace },
{ target: "ftouppercase", replacement: tsFToUpperCase },
{ target: "ftoupper", replacement: tsFToUpperCase },
{ target: "ftolowercase", replacement: tsFToLowerCase },
{ target: "ftolower", replacement: tsFToLowerCase },
{ target: "ftotitlecase", replacement: tsFToTitleCase },
{ target: "ftotitle", replacement: tsFToTitleCase },
// logic
{ target: "fequals", replacement: tsFEquals },
{ target: "feq", replacement: tsFEquals },
{ target: "f=", replacement: tsFEquals },
{ target: "fanyequals", replacement: tsFAnyEquals },
{ target: "fanyeq", replacement: tsFAnyEquals },
{ target: "fnotequals", replacement: tsFNotEquals },
{ target: "fneq", replacement: tsFNotEquals },
{ target: "f!=", replacement: tsFNotEquals },
{ target: "fgreaterthan", replacement: tsFGreaterThan },
{ target: "fgt", replacement: tsFGreaterThan },
{ target: "f>", replacement: tsFGreaterThan },
{ target: "flessthan", replacement: tsFLessThan },
{ target: "flt", replacement: tsFLessThan },
{ target: "f<", replacement: tsFLessThan },
{ target: "fgreaterequal", replacement: tsFGreaterEqual },
{ target: "fgeq", replacement: tsFGreaterEqual },
{ target: "f>=", replacement: tsFGreaterEqual },
{ target: "f=>", replacement: tsFGreaterEqual },
{ target: "flessequal", replacement: tsFLessEqual },
{ target: "fleq", replacement: tsFLessEqual },
{ target: "f<=", replacement: tsFLessEqual },
{ target: "f=<", replacement: tsFLessEqual },
{ target: "for", replacement: tsFOr },
{ target: "f||", replacement: tsFOr },
{ target: "fand", replacement: tsFAnd },
{ target: "f&&", replacement: tsFAnd },
{ target: "fnot", replacement: tsFNot },
{ target: "f!", replacement: tsFNot },
// conditionals
{ target: "fbranch", replacement: tsFBranch },
{ target: "fbch", replacement: tsFBranch },
{ target: "fsubstexists", replacement: tsFSubstitutionExists },
{ target: "fsubexists", replacement: tsFSubstitutionExists },
{ target: "fexists", replacement: tsFSubstitutionExists },
{ target: "fsex", replacement: tsFSubstitutionExists },
{ target: "fsafeexecute", replacement: tsFSafeExecute },
{ target: "fsafe", replacement: tsFSafeExecute },
]
TS_SUB_TABLE_BUILTIN = tsFillSubTables(builtinCommands, builtinTableSize);
TS_SUB_TABLE_BUILTIN_FUNCTIONS = tsFillSubTables(builtinFunctions, builtinFunctionsTableSize);
tsBuildCustomSubTables();
}
// Finds custom substitution files and builds them into TS_SUB_TABLE_USER
function tsBuildCustomSubTables(){
const searchpattern = "ts_*.txt";
var files;
var dir = new Folder(TS_SCRIPTS_DIR.fsName);
var failFiles = [];
var customSubs = [];
TS_NUM_CUSTOM_RULES_LOADED = 0;
TS_LOADED_CUSTOM_FILES = [];
files = dir.getFiles(searchpattern); // returns an array of files matching the pattern
for(i in files){
if(!tsParseTSV(files[i], customSubs))
failFiles.push(files[i].name);
else{
var newfile = { path: files[i].absoluteURI, modified: files[i].modified };
TS_LOADED_CUSTOM_FILES.push(newfile);
}
}
dir.changePath("./substitutions/") // check subs folder
files = dir.getFiles(searchpattern);
for(i in files){
if(!tsParseTSV(files[i], customSubs))
failFiles.push(files[i].name);
else{
var newfile = { path: files[i].absoluteURI, modified: files[i].modified };
TS_LOADED_CUSTOM_FILES.push(newfile);
}
}
if(failFiles.length > 0) alert("Text Substitutions Warning:\nThese files were empty or could not be opened:\n\n" + failFiles);
TS_SUB_TABLE_USER = tsFillSubTables(customSubs);
}
// opens and reads from a File object, parses the tsv into object as a series of target/replacement pairs
// returns false if the file cant be opened or is empty
function tsParseTSV(inputFile, output){
const sep = "\t";
inputFile.open("r"); // r = read mode
if(inputFile.eof){ // empty file or couldnt open
inputFile.close();
return false;
}
for (var i = 1; !inputFile.eof; i++) {
var line = inputFile.readln(); // grab a line
if( line.length < 2 || (line.length >= 2 && line[0] == "/" && line[1] == "/") ){ // line is blank or commented out - skip it
continue;
}
var obj = { target: "", replacement: [], recursions: 0 };
if(line.indexOf(TS_END_DELIM) != -1) obj.recursions = 1; // might we need to recurse on this?
while(line[line.length-1] == "\\"){ // check if line ends with backslash, read a new line to append if it does
while(!inputFile.eof){
var nl = inputFile.readln();
if(nl.length < 2){
continue; // blank, discard
}
var start = 0;
while(nl[start] && nl[start] == "\t"){
start++; // skip leading tabs
}
if(!nl[start]|| (nl.length >= 2 && nl[start] == "/" && nl[start+1] == "/")){
continue; // only contains tabs or commented out, discard
}
line = line.substring(0, line.length-1); // chop off that backslash
line += nl.substring(start); // trim leading tabs, append to existing string
break;
}
if(inputFile.eof && line[line.length-1] == "\\"){ // case: the last valid line ends with a backslash - gotta make sure we trim it
line = line.substring(0, line.length-1);
}
}
line = line.split(sep); // split at tabs
if(line[0] == "" && line[1] == ""){ // blank line, ignore;
continue;
}
obj.target = line[0]; // add the first one as the target
for(var j = 1; j < line.length; j++){ // and the rest as replacements
if(line[j].length > 0) obj.replacement.push(line[j]);
}
if(obj.replacement.length == 0) obj.replacement.push(""); // if the user only specified one param they want it to be blank
TS_NUM_CUSTOM_RULES_LOADED++;
output.push(obj);
}
inputFile.close(); // gotta be polite
return true;
}
// returns a substitution table with the contents of substArray. Optionally sets substTable size to tableSize, if specified.
function tsFillSubTables(substArray, tableSize){
if(!tableSize){ // if not specified, choose one.
// prime nums, somewhat evenly spaced for use as user table size for better modulo
const primes = [53, 101, 211, 307, 401, 601, 809, 1009, 1201, 1399, 1601, 1901, 2399, 2801, 3203, 6397, 12007, 24001, 48017, 96001];
var i = 0;
// in the ungodly case someone has > 48,000 substitutions, just pick something that's maybe a prime number
if(substArray.length*2 > primes[primes.length-1]){
alert("Text Substitutions is impressed!\nIf you're seeing this, you have more than 48,000 substitutions which is way more than I ever expected anyone would use. Don't worry, I added a fallback to ensure the program still works, it will just be slightly less efficent.\n\nAlso, please leave a github issue or email me (9yz [at] 9yz.dev) so I can learn what the fuck you're doing that requires 48,000+ substitutions.");
tableSize = (substArray.length*2)-1;
}
else{
for(i in primes){
if(substArray.length*2 < primes[i]){
tableSize = primes[i];
break; // find the first prime larger than the number of custom subs *2
}
}
}
}
var substTable = new SubstitutionTable(tableSize);
for(var i in substArray){ // move items to the table
substTable.insert(substArray[i]);
}
return substTable;
}
// Checks if any of the files in TS_LOADED_CUSTOM_FILES have been modified since their stored date; calls tsBuildCustomSubTables(true) if so.
function tsHotloadCustomSubTables(){
if(!TS_LOADED_CUSTOM_FILES) return;
for(var i = 0; i < TS_LOADED_CUSTOM_FILES.length; i++){
var f = new File(TS_LOADED_CUSTOM_FILES[i].path);
if(f.modified > TS_LOADED_CUSTOM_FILES[i].modified){ // if one of the files was modified after the date we stored, we need to rebuild the sub tables.
tsBuildCustomSubTables();
/* var failFiles = [];
var customSubs = [];
TS_NUM_CUSTOM_RULES_LOADED = 0;
var newLoadedFilesList = [];
for(var j = 0; j < TS_LOADED_CUSTOM_FILES.length; j++){
var file = new File(TS_LOADED_CUSTOM_FILES[j].path);
if(!tsParseTSV(file, customSubs))
failFiles.push(file.name);
else{
var newfile = { path: file.absoluteURI, modified: file.modified };
newLoadedFilesList.push(newfile);
}
}
if(failFiles.length > 0) alert("Text Substitutions Warning:\nThese files were empty or could not be opened:\n\n" + failFiles);
TS_LOADED_CUSTOM_FILES = newLoadedFilesList;
tsFillSubTables(TS_SUB_TABLE_USER, customSubs); */
}
}
}
// Run when the script is selected. Gets user input, selects properties to edit, and passes them to tsDoSubstitutions()
function tsRun(){
const propertyList = [
{ type: "simple", category: TS_PROPERTY_CATEGORIES.core, namespace: XMPConst.NS_PHOTOSHOP, key: "City", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.core, namespace: XMPConst.NS_PHOTOSHOP, key: "State", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.core, namespace: XMPConst.NS_PHOTOSHOP, key: "Country", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.core, namespace: XMPConst.NS_IPTC_CORE, key: 'CountryCode', },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.core, namespace: XMPConst.NS_IPTC_CORE, key: "Location", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.core, namespace: XMPConst.NS_DC, key: "title", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.core, namespace: XMPConst.NS_PHOTOSHOP, key: "Headline", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.core, namespace: XMPConst.NS_IPTC_CORE, key: "AltTextAccessibility", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.core, namespace: XMPConst.NS_IPTC_CORE, key: "ExtDescrAccessibility", },
{ type: "array", category: TS_PROPERTY_CATEGORIES.core, namespace: XMPConst.NS_DC, key: "subject", }, // keywords
{ type: "simple", category: TS_PROPERTY_CATEGORIES.core, namespace: XMPConst.NS_DC, key: "description", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.credit, namespace: XMPConst.NS_PHOTOSHOP, key: "AuthorsPosition", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.credit, namespace: XMPConst.NS_PHOTOSHOP, key: "Credit", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.credit, namespace: XMPConst.NS_PHOTOSHOP, key: "Source", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.credit, namespace: XMPConst.NS_PHOTOSHOP, key: "CaptionWriter", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.credit, namespace: XMPConst.NS_DC, key: "creator", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.credit, namespace: XMPConst.NS_DC, key: "rights", },
{ type: "localized",category: TS_PROPERTY_CATEGORIES.credit, namespace: XMPConst.NS_XMP_RIGHTS, key: "UsageTerms", }, // because UsageTerms just wants to be special and refuses to behave like other tags that USE LOCALIZATION but don't FREAK THE FUCK OUT when you dont explicitly insert localized text
{ type: "complex", category: TS_PROPERTY_CATEGORIES.credit, schemaNS: XMPConst.NS_IPTC_CORE, structName: "CreatorContactInfo", fieldNS: XMPConst.NS_IPTC_CORE, fieldName: "CiEmailWork" },
{ type: "complex", category: TS_PROPERTY_CATEGORIES.credit, schemaNS: XMPConst.NS_IPTC_CORE, structName: "CreatorContactInfo", fieldNS: XMPConst.NS_IPTC_CORE, fieldName: "CiUrlWork" },
{ type: "complex", category: TS_PROPERTY_CATEGORIES.credit, schemaNS: XMPConst.NS_IPTC_CORE, structName: "CreatorContactInfo", fieldNS: XMPConst.NS_IPTC_CORE, fieldName: "CiAdrExtadr" },
{ type: "complex", category: TS_PROPERTY_CATEGORIES.credit, schemaNS: XMPConst.NS_IPTC_CORE, structName: "CreatorContactInfo", fieldNS: XMPConst.NS_IPTC_CORE, fieldName: "CiAdrCity" },
{ type: "complex", category: TS_PROPERTY_CATEGORIES.credit, schemaNS: XMPConst.NS_IPTC_CORE, structName: "CreatorContactInfo", fieldNS: XMPConst.NS_IPTC_CORE, fieldName: "CiAdrRegion" },
{ type: "complex", category: TS_PROPERTY_CATEGORIES.credit, schemaNS: XMPConst.NS_IPTC_CORE, structName: "CreatorContactInfo", fieldNS: XMPConst.NS_IPTC_CORE, fieldName: "CiAdrPcode" },
{ type: "complex", category: TS_PROPERTY_CATEGORIES.credit, schemaNS: XMPConst.NS_IPTC_CORE, structName: "CreatorContactInfo", fieldNS: XMPConst.NS_IPTC_CORE, fieldName: "CiAdrCtry" },
{ type: "complex", category: TS_PROPERTY_CATEGORIES.credit, schemaNS: XMPConst.NS_IPTC_CORE, structName: "CreatorContactInfo", fieldNS: XMPConst.NS_IPTC_CORE, fieldName: "CiTelWork" },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.misc, namespace: XMPConst.NS_IPTC_CORE, key: "SubjectCode", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.misc, namespace: XMPConst.NS_IPTC_CORE, key: "IntellectualGenre", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.misc, namespace: XMPConst.NS_IPTC_CORE, key: "Scene", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.misc, namespace: XMPConst.NS_PHOTOSHOP, key: "TransmissionReference", },
{ type: "simple", category: TS_PROPERTY_CATEGORIES.misc, namespace: XMPConst.NS_PHOTOSHOP, key: "Instructions", },
]
try{
tsHotloadCustomSubTables(); // rebuild sub tables if we need to
app.synchronousMode = true;
var errorFiles = 0;
var selection = app.document.selections; // get selected files
if(!selection.length){ // nothing selected
alert('Text Substitutions Error:\nNothing selected!');
return;
}
if(ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript'); // load the xmp scripting API
var useDialog = false;
if(selection.length > 20) useDialog = true;
if(useDialog){ // progress bar dialog
/*
Code for Import https://scriptui.joonas.me — (Triple click to select):
{"activeId":2,"items":{"item-0":{"id":0,"type":"Dialog","parentId":false,"style":{"enabled":true,"varName":"progessDialog","windowType":"Dialog","creationProps":{"su1PanelCoordinates":false,"maximizeButton":false,"minimizeButton":false,"independent":false,"closeButton":false,"borderless":false,"resizeable":false},"text":"Text Substitutions is working...","preferredSize":[300,0],"margins":16,"orientation":"column","spacing":10,"alignChildren":["center","top"]}},"item-1":{"id":1,"type":"Progressbar","parentId":0,"style":{"enabled":true,"varName":null,"preferredSize":[275,4],"alignment":null,"helpTip":null}},"item-2":{"id":2,"type":"StaticText","parentId":0,"style":{"enabled":true,"varName":"progresstext","creationProps":{"truncate":"none","multiline":false,"scrolling":false},"softWrap":false,"text":"x out of x files processed.","justify":"left","preferredSize":[0,0],"alignment":"left","helpTip":null}}},"order":[0,1,2],"settings":{"importJSON":true,"indentSize":false,"cepExport":false,"includeCSSJS":true,"showDialog":true,"functionWrapper":false,"afterEffectsDockable":false,"itemReferenceList":"None"}}
*/
// DIALOG
// ======
var progessDialog = new Window("palette", undefined, undefined, {closeButton: false, resizable: true});
progessDialog.text = "Text Substitutions is working...";
progessDialog.preferredSize.width = 325;
progessDialog.preferredSize.height = 100;
progessDialog.orientation = "column";
progessDialog.alignChildren = ["center","center"];
progessDialog.spacing = 10;
progessDialog.margins = 32;
var progressbar1 = progessDialog.add("progressbar", undefined, undefined, {name: "progressbar1"});
progressbar1.maxvalue = selection.length;
progressbar1.preferredSize.width = 300;
progressbar1.preferredSize.height = 4;
progressbar1.value = 0;
var progresstext1 = progessDialog.add("statictext", undefined, undefined, {name: "progresstext1"});
progresstext1.alignment = ["left","top"];
progresstext1.preferredSize.width = 300;
progresstext1.text = "0 out of " + selection.length + " files processed.";
var progresstext2 = progessDialog.add("statictext", undefined, undefined, {name: "progresstext2"});
progresstext2.alignment = ["left","top"];
progresstext2.preferredSize.width = 300;