-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdocument_options_as_json.lua
1245 lines (1117 loc) · 43.6 KB
/
document_options_as_json.lua
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
function plugindef()
finaleplugin.RequireScore = false
finaleplugin.RequireSelection = false
finaleplugin.RequireDocument = true
finaleplugin.Author = "Aaron Sherber"
finaleplugin.AuthorURL = "https://aaron.sherber.com"
finaleplugin.Copyright = "CC0 https://creativecommons.org/publicdomain/zero/1.0/"
finaleplugin.Version = "2.1.4"
finaleplugin.MinJWLuaVersion = 0.67
finaleplugin.Date = "2024-02-05"
finaleplugin.CategoryTags = "Report"
finaleplugin.Id = "9c05a4c4-9508-4608-bb1b-2819cba96101"
finaleplugin.AdditionalMenuOptions = [[
Import Document Options from JSON...
]]
finaleplugin.AdditionalPrefixes = [[
action = "import"
]]
finaleplugin.RevisionNotes = [[
v2.1.4 Switch to cjson
v2.1.3 Luacheck hinting
v2.1.2 Resync expression definitions
v2.1.1 Add music spacing allotments (requires RGPLua v0.66)
v2.0.1 Add ability to import
v1.2.1 Add Grid/Guide snap-tos; better organization of SmartShapes
v1.1.2 First public release
]]
finaleplugin.Notes = [[
While other plugins exist that let you copy document options directly from one document to another,
this script saves the options from the current document in an organized human-readable form, as a
JSON file. You can then use a diff program to compare the JSON files generated from
two Finale documents, or you can keep track of how the settings in a document have changed
over time. The script will also let you import settings from a full or partial JSON file.
Please see https://url.sherber.com/finalelua/options-as-json for more information.
The focus is on document-specific settings, rather than program-wide ones, and in particular on
the ones that affect the look of a document. Most of these come from the Document Options dialog
in Finale, although some come from the Category Designer, the Page Format dialog, and the
SmartShape menu.
All physical measurements are given in EVPUs, except for a couple of values that Finale always
displays as spaces. (1 EVPU is 1/288 of an inch, 1/24 of a space, or 1/4 of a point.) So if your
measurement units are set to EVPUs, the values given here should match what you see in Finale.
]]
return "Save Document Options as JSON...", "", "Saves all current document options to a JSON file"
end
--[[
BASIC WORKFLOW FOR SAVING PREFERENCES
- Load all preferences with prefs object and use dumpproperties() to turn them
into one big table, using custom handlers if needed (RAW PREFS and HANDLERS sections)
- NORMALIZE units (mainly to EVPU, but also to standard percent values, etc.)
- TRANSFORM the raw prefs table into a table grouped the way a user would see things
- Serialize to alpha order JSON
Loading preferences essentially follows this workflow in reverse.
]]
action = action or "export"
local debug = {
raw_categories = false,
raw_units = false
}
local mixin = require('library.mixin')
local json = require("cjson.safe")
local expr = require("library.expression")
-- region DIALOGS
local fcstr = function(str)
return mixin.FCMString():SetLuaString(str)
end
function simplify_finale_version(ver)
if type(ver) == "number" then
return ver < 10000 and ver or (ver - 10000)
else
return nil
end
end
function get_file_options_tag()
local temp_table = {}
if debug.raw_categories then table.insert(temp_table, "raw prefs") end
if debug.raw_units then table.insert(temp_table, "raw units") end
local result = table.concat(temp_table, ", ")
if #result > 0 then result = " - " .. result end
return result
end
function get_path_and_file(document)
local file_name = mixin.FCMString()
local path_name = mixin.FCMString()
local file_path = mixin.FCMString()
document:GetPath(file_path)
file_path:SplitToPathAndFile(path_name, file_name)
return path_name, file_name
end
function do_file_open_dialog(document)
local text_extension = ".json"
local filter_text = "JSON files"
local path_name, _ = get_path_and_file(document)
local open_dialog = mixin.FCMFileOpenDialog(finenv.UI())
:SetWindowTitle(fcstr("Open JSON Settings"))
:SetInitFolder(path_name)
:AddFilter(fcstr("*" .. text_extension), fcstr(filter_text))
if not open_dialog:Execute() then
return nil
end
local selected_file_name = finale.FCString()
open_dialog:GetFileName(selected_file_name)
return selected_file_name.LuaString
end
function confirm_file_import(meta)
-- If no meta section then don't confirm
if meta == nil then return true end
local col_1_width = 85
local width_factor = 6
local row_height = 17
local max_string_length = 0
local dialog = mixin.FCMCustomWindow()
:SetTitle("Confirm Import")
local t = {
{ "Import these settings?" },
{},
{ "Music File", meta.File or meta.MusicFile},
{ "Date", meta.Date },
{ "Finale Version", simplify_finale_version(meta.FinaleVersion) },
{ "Description", meta.Description }
}
for row, labels in ipairs(t) do
for col, label in ipairs(labels) do
max_string_length = math.max(max_string_length, string.len(label or ""))
dialog:CreateStatic((col - 1) * col_1_width, (row - 1) * row_height)
:SetText(label)
end
end
for ctrl in each(dialog) do
ctrl:SetWidth(max_string_length * width_factor)
end
dialog:CreateOkButton()
dialog:CreateCancelButton()
return dialog:ExecuteModal(nil) == finale.EXECMODAL_OK
end
function do_save_as_dialog(document)
local text_extension = ".json"
local filter_text = "JSON files"
local path_name, file_name = get_path_and_file(document)
local extension = mixin.FCMString()
:SetLuaString(file_name.LuaString)
:ExtractFileExtension()
if extension.Length > 0 then
file_name:TruncateAt(file_name:FindLast("." .. extension.LuaString))
end
file_name:AppendLuaString(" settings")
:AppendLuaString(get_file_options_tag())
:AppendLuaString(text_extension)
local save_dialog = mixin.FCMFileSaveAsDialog(finenv.UI())
:SetWindowTitle(fcstr("Save As"))
:AddFilter(fcstr("*" .. text_extension), fcstr(filter_text))
:SetInitFolder(path_name)
:SetFileName(file_name)
if not save_dialog:Execute() then
return nil
end
save_dialog:AssureFileExtension(text_extension)
local selected_file_name = finale.FCString()
save_dialog:GetFileName(selected_file_name)
return selected_file_name.LuaString
end
function get_description()
local dialog = mixin.FCMCustomWindow():SetTitle("Save As")
dialog:CreateStatic(0, 0):SetText("Settings Description"):SetWidth(120)
dialog:CreateEdit(0, 17, "input"):SetWidth(360)
dialog:CreateOkButton()
if dialog:ExecuteModal(nil) == finale.EXECMODAL_OK then
return dialog:GetControl("input"):GetText()
else
return ""
end
end
-- endregion
-- region HANDLERS
--[[
Functions to assist with moving raw preferences
between tables and PDK objects.
]]
function getter_name(...) return "Get" .. table.concat{...} end
function setter_name(...) return "Set" .. table.concat{...} end
function delete_from_table(prefs_table, exclusions)
if not prefs_table or not exclusions then return end
for _, e in pairs(exclusions) do prefs_table[e] = nil end
end
function add_props_to_table(prefs_table, prefs_obj, tag, exclusions)
if tag then
prefs_table[tag] = {}
prefs_table = prefs_table[tag]
end
for k, v in pairs(dumpproperties(prefs_obj)) do
prefs_table[k] = v
end
delete_from_table(prefs_table, exclusions)
end
function set_props_from_table(prefs_table, prefs_obj, exclusions)
prefs_table = prefs_table or {}
delete_from_table(prefs_table, exclusions)
for k, v in pairs(prefs_table) do
if type(v) ~= "table" then
local setter = prefs_obj[setter_name(k)]
if setter then setter(prefs_obj, v) end
end
end
end
function handle_page_format_prefs(prefs_obj, prefs_table, load)
local SCORE, PARTS = "Score", "Parts"
if load then
prefs_obj:LoadScore()
add_props_to_table(prefs_table, prefs_obj, SCORE)
prefs_obj:LoadParts()
add_props_to_table(prefs_table, prefs_obj, PARTS)
else
prefs_obj:LoadScore()
set_props_from_table(prefs_table[SCORE], prefs_obj)
prefs_obj:Save()
prefs_obj:LoadParts()
set_props_from_table(prefs_table[PARTS], prefs_obj)
prefs_obj:Save()
end
end
function handle_name_position_prefs(prefs_obj, prefs_table, load)
local FULL, ABBREVIATED = "Full", "Abbreviated"
if load then
prefs_obj:LoadFull()
add_props_to_table(prefs_table, prefs_obj, FULL)
prefs_obj:LoadAbbreviated()
add_props_to_table(prefs_table, prefs_obj, ABBREVIATED)
else
prefs_obj:LoadFull()
set_props_from_table(prefs_table[FULL], prefs_obj)
prefs_obj:Save()
prefs_obj:LoadAbbreviated()
set_props_from_table(prefs_table[ABBREVIATED], prefs_obj)
prefs_obj:Save()
end
end
function handle_layer_prefs(prefs_obj, prefs_table, load)
for i = 0, 3 do
prefs_obj:Load(i)
local layer_name = "Layer" .. i + 1
if load then
add_props_to_table(prefs_table, prefs_obj, layer_name)
else
set_props_from_table(prefs_table[layer_name], prefs_obj)
prefs_obj:Save()
end
end
end
local FONT_EXCLUSIONS = { "EnigmaStyles", "IsSMuFLFont", "Size" }
function handle_font_prefs(prefs_obj, prefs_table, load)
local font_pref_types = {
[finale.FONTPREF_MUSIC] = 'Music',
[finale.FONTPREF_KEYSIG] = 'KeySignatures',
[finale.FONTPREF_CLEF] = 'Clefs',
[finale.FONTPREF_TIMESIG] = 'TimeSignatureScore',
[finale.FONTPREF_CHORDSYMBOL] = 'ChordSymbols',
[finale.FONTPREF_CHORDALTERATION] = 'ChordAlterations',
[finale.FONTPREF_ENDING] = 'EndingRepeats',
[finale.FONTPREF_TUPLET] = 'Tuplets',
[finale.FONTPREF_TEXTBLOCK] = 'TextBlocks',
[finale.FONTPREF_LYRICSVERSE] = 'LyricVerses',
[finale.FONTPREF_LYRICSCHORUS] = 'LyricChoruses',
[finale.FONTPREF_LYRICSSECTION] = 'LyricSections',
[finale.FONTPREF_MULTIMEASUREREST] = 'MultimeasureRests',
[finale.FONTPREF_CHORDSUFFIX] = 'ChordSuffixes',
[finale.FONTPREF_EXPRESSION] = 'Expressions',
[finale.FONTPREF_REPEAT] = 'TextRepeats',
[finale.FONTPREF_CHORDFRETBOARD] = 'ChordFretboards',
[finale.FONTPREF_FLAG] = 'Flags',
[finale.FONTPREF_ACCIDENTAL] = 'Accidentals',
[finale.FONTPREF_ALTERNATESLASH] = 'AlternateNotation',
[finale.FONTPREF_ALTERNATENUMBER] = 'AlternateNotationNumbers',
[finale.FONTPREF_REST] = 'Rests',
[finale.FONTPREF_REPEATDOT] = 'RepeatDots',
[finale.FONTPREF_NOTEHEAD] = 'Noteheads',
[finale.FONTPREF_AUGMENTATIONDOT] = 'AugmentationDots',
[finale.FONTPREF_TIMESIGPLUS] = 'TimeSignaturePlusScore',
[finale.FONTPREF_ARTICULATION] = 'Articulations',
[finale.FONTPREF_DEFTABLATURE] = 'Tablature',
[finale.FONTPREF_PERCUSSION] = 'PercussionNoteheads',
[finale.FONTPREF_8VA] = 'SmartShape8va',
[finale.FONTPREF_MEASURENUMBER] = 'MeasureNumbers',
[finale.FONTPREF_STAFFNAME] = 'StaffNames',
[finale.FONTPREF_ABRVSTAFFNAME] = 'StaffNamesAbbreviated',
[finale.FONTPREF_GROUPNAME] = 'GroupNames',
[finale.FONTPREF_8VB] = 'SmartShape8vb',
[finale.FONTPREF_15MA] = 'SmartShape15ma',
[finale.FONTPREF_15MB] = 'SmartShape15mb',
[finale.FONTPREF_TR] = 'SmartShapeTrill',
[finale.FONTPREF_WIGGLE] = 'SmartShapeWiggle',
[finale.FONTPREF_ABRVGROUPNAME] = 'GroupNamesAbbreviated',
[finale.FONTPREF_GUITARBENDFULL] = 'GuitarBendFull',
[finale.FONTPREF_GUITARBENDNUMBER] = 'GuitarBendNumber',
[finale.FONTPREF_GUITARBENDFRACTION] = 'GuitarBendFraction',
[finale.FONTPREF_TIMESIG_PARTS] = 'TimeSignatureParts',
[finale.FONTPREF_TIMESIGPLUS_PARTS] = 'TimeSignaturePlusParts',
}
for pref_type, tag in pairs(font_pref_types) do
if load then
prefs_obj:LoadFontPrefs(pref_type)
add_props_to_table(prefs_table, prefs_obj, tag, FONT_EXCLUSIONS)
else
set_props_from_table(prefs_table[tag], prefs_obj, FONT_EXCLUSIONS)
prefs_obj:SaveFontPrefs(pref_type)
end
end
end
function handle_tie_placement_prefs(prefs_obj, prefs_table, load)
local tie_placement_types = {
[finale.TIEPLACE_OVERINNER] = "Over/Inner",
[finale.TIEPLACE_UNDERINNER] = "Under/Inner",
[finale.TIEPLACE_OVEROUTERNOTE] = "Over/Outer/Note",
[finale.TIEPLACE_UNDEROUTERNOTE] = "Under/Outer/Note",
[finale.TIEPLACE_OVEROUTERSTEM] = "Over/Outer/Stem",
[finale.TIEPLACE_UNDEROUTERSTEM] = "Under/Outer/Stem"
}
local prop_names = {
"HorizontalStart",
"VerticalStart",
"HorizontalEnd",
"VerticalEnd"
}
for placement_type, tag in pairs(tie_placement_types) do
prefs_obj:Load(placement_type)
if load then
local t = {}
for _, name in pairs(prop_names) do
t[name] = prefs_obj[getter_name(name)](prefs_obj, placement_type)
end
prefs_table[tag] = t
else
local t = prefs_table[tag]
for _, name in pairs(prop_names) do
prefs_obj[setter_name(name)](prefs_obj, placement_type, t[name])
end
prefs_obj:Save()
end
end
end
function handle_tie_contour_prefs(prefs_obj, prefs_table, load)
local tie_contour_types = {
[finale.TCONTOURIDX_SHORT] = "Short",
[finale.TCONTOURIDX_MEDIUM] = "Medium",
[finale.TCONTOURIDX_LONG] = "Long",
[finale.TCONTOURIDX_TIEENDS] = "TieEnds"
}
local prop_names = {
"Span",
"LeftRelativeInset",
"LeftRawRelativeInset",
"LeftHeight",
"LeftFixedInset",
"RightRelativeInset",
"RightRawRelativeInset",
"RightHeight",
"RightFixedInset",
}
for contour_type, tag in pairs(tie_contour_types) do
prefs_obj:Load(contour_type)
if load then
local t = {}
for _, name in pairs(prop_names) do
t[name] = prefs_obj[getter_name(name)](prefs_obj, contour_type)
end
prefs_table[tag] = t
else
local t = prefs_table[tag]
for _, name in pairs(prop_names) do
prefs_obj[setter_name(name)](prefs_obj, contour_type, t[name])
end
prefs_obj:Save()
end
end
end
function handle_base_prefs(prefs_obj, prefs_table, load, exclusions)
exclusions = exclusions or {}
prefs_obj:Load(1)
if load then
add_props_to_table(prefs_table, prefs_obj, nil, exclusions)
else
set_props_from_table(prefs_table, prefs_obj, exclusions)
prefs_obj:Save()
end
end
function handle_tie_prefs(prefs_obj, prefs_table, load)
handle_base_prefs(prefs_obj, prefs_table, load)
local TIE_CONTOURS, TIE_PLACEMENT = "TieContours", "TiePlacement"
if load then
local function load_sub_prefs(sub_prefs, handler, tag)
local t = {}
handler(sub_prefs, t, true)
prefs_table[tag] = t
end
load_sub_prefs(prefs_obj:CreateTieContourPrefs(), handle_tie_contour_prefs, TIE_CONTOURS)
load_sub_prefs(prefs_obj:CreateTiePlacementPrefs(), handle_tie_placement_prefs, TIE_PLACEMENT)
else
handle_tie_contour_prefs(prefs_obj:CreateTieContourPrefs(), prefs_table[TIE_CONTOURS], false)
handle_tie_placement_prefs(prefs_obj:CreateTiePlacementPrefs(), prefs_table[TIE_PLACEMENT], false)
end
end
function handle_category_prefs(prefs_obj, prefs_table, load)
local font = finale.FCFontInfo()
local font_types = { "Text", "Music", "Number" }
local FONTS, FONT_INFO, TYPE = "Fonts", "FontInfo", "Type"
local EXCLUSIONS = { "ID" }
local function get_cat_tag(cat) return cat:CreateName().LuaString:gsub(" ", "") end
local function humanize(tag) return string.gsub(tag, "(%l)(%u)", "%1 %2") end
prefs_obj:LoadAll()
if load then
for raw_cat in each(prefs_obj) do
if raw_cat:IsDefaultMiscellaneous() then
goto cat_continue
end
local raw_cat_tag = get_cat_tag(raw_cat)
add_props_to_table(prefs_table, raw_cat, raw_cat_tag, EXCLUSIONS)
local font_table = {}
prefs_table[raw_cat_tag][FONTS] = font_table
for _, font_type in pairs(font_types) do
if raw_cat[getter_name(font_type, FONT_INFO)](raw_cat, font) then
add_props_to_table(font_table, font, font_type, FONT_EXCLUSIONS)
end
end
::cat_continue::
end
else
local function populate_raw_cat(cat_values, raw_cat)
set_props_from_table(cat_values, raw_cat, EXCLUSIONS)
for _, font_type in pairs(font_types) do
if raw_cat[getter_name(font_type, FONT_INFO)](raw_cat, font) then
set_props_from_table(cat_values[FONTS][font_type], font, FONT_EXCLUSIONS)
raw_cat[setter_name(font_type, FONT_INFO)](raw_cat, font)
end
end
end
for cat_tag, cat_values in pairs(prefs_table) do
local this_cat = nil
for raw_cat in each(prefs_obj) do
if get_cat_tag(raw_cat) == cat_tag then
this_cat = raw_cat
break
end
end
if this_cat then
populate_raw_cat(cat_values, this_cat)
this_cat:Save()
else
local new_cat = finale.FCCategoryDef()
local cat_type = cat_values[TYPE]
new_cat:Load(cat_type)
new_cat:SetName(mixin.FCMString():SetLuaString(humanize(cat_tag)))
populate_raw_cat(cat_values, new_cat)
new_cat:SaveNewWithType(cat_type)
end
end
end
end
function handle_smart_shape_prefs(prefs_obj, prefs_table, load)
handle_base_prefs(prefs_obj, prefs_table, load)
local contour_prefs = prefs_obj:CreateSlurContourPrefs()
local span_types = { 'Short', 'Medium', 'Long', 'ExtraLong' }
local prop_names = { 'Span', 'Inset', 'Height' }
local SLUR_CONTOURS = "SlurContours"
if load then
local contour_table = {}
prefs_table[SLUR_CONTOURS] = contour_table
for _, type in pairs(span_types) do
local t = {}
for _, name in pairs(prop_names) do
t[name] = contour_prefs[getter_name(type, name)](contour_prefs)
end
contour_table[type] = t
end
else
for _, type in pairs(span_types) do
for _, name in pairs(prop_names) do
local contour_table = prefs_table[SLUR_CONTOURS]
if contour_table and contour_table[type] and contour_table[type][name] then
contour_prefs[setter_name(type, name)](contour_prefs, contour_table[type][name])
end
end
end
contour_prefs:Save()
end
end
function handle_grid_prefs(prefs_obj, prefs_table, load)
local snap_items = {
[finale.SNAPITEM_BRACKETS ] = "Brackets",
[finale.SNAPITEM_CHORDS ] = "Chords",
[finale.SNAPITEM_EXPRESSIONS ] = "Expressions",
[finale.SNAPITEM_FRETBOARDS ] = "Fretboards",
[finale.SNAPITEM_GRAPHICSMOVE ] = "GraphicsMove",
[finale.SNAPITEM_GRAPHICSSIZING ] = "GraphicsSizing",
[finale.SNAPITEM_MEASURENUMBERS ] = "MeasureNumbers",
[finale.SNAPITEM_REPEATS ] = "Repeats",
[finale.SNAPITEM_SPECIALTOOLS ] = "SpecialTools",
[finale.SNAPITEM_STAFFNAMES ] = "StaffNames",
[finale.SNAPITEM_STAVES ] = "Staves",
[finale.SNAPITEM_TEXTBLOCKMOVE ] = "TextBlockMove",
[finale.SNAPITEM_TEXTBLOCKSIZING ] = "TextBlockSizing",
}
local SNAP_TO_GRID, SNAP_TO_GUIDE = "SnapToGrid", "SnapToGuide"
handle_base_prefs(prefs_obj, prefs_table, load, { "HorizontalGuideCount", "VerticalGuideCount" })
if load then
prefs_table[SNAP_TO_GRID] = {}
prefs_table[SNAP_TO_GUIDE] = {}
for item, name in pairs(snap_items) do
prefs_table[SNAP_TO_GRID][name] = prefs_obj:GetGridSnapToItem(item)
prefs_table[SNAP_TO_GUIDE][name] = prefs_obj:GetGuideSnapToItem(item)
end
else
for item, name in pairs(snap_items) do
prefs_obj:SetGridSnapToItem(item, prefs_table[SNAP_TO_GRID][name])
prefs_obj:SetGuideSnapToItem(item, prefs_table[SNAP_TO_GUIDE][name])
end
prefs_obj:Save()
end
end
function handle_music_spacing_prefs(prefs_obj, prefs_table, load)
handle_base_prefs(prefs_obj, prefs_table, load, { "ScalingValue" })
end
function handle_allotment_prefs(prefs_obj, prefs_table, load)
if load then
for a in loadall(prefs_obj) do
table.insert(prefs_table, { Duration = a.ItemNo, Width = a.Width })
end
else
-- treat these as a complete set
for a in loadall(prefs_obj) do
a:DeleteData()
end
for _, a in ipairs(prefs_table) do
local new_allotment = finale.FCAllotment()
new_allotment.Width = a.Width
new_allotment:SaveAs(a.Duration)
end
end
end
-- endregion
-- region RAW PREFS
--[[
List of PDK objects to load, along with a handler for ones
that don't follow the common pattern.
]]
local raw_pref_definitions = {
{ prefs = finale.FCAllotments, handler = handle_allotment_prefs },
{ prefs = finale.FCCategoryDefs, handler = handle_category_prefs },
{ prefs = finale.FCChordPrefs },
{ prefs = finale.FCDistancePrefs },
{ prefs = finale.FCFontInfo, handler = handle_font_prefs },
{ prefs = finale.FCGridsGuidesPrefs, handler = handle_grid_prefs },
{ prefs = finale.FCGroupNamePositionPrefs, handler = handle_name_position_prefs },
{ prefs = finale.FCLayerPrefs, handler = handle_layer_prefs },
{ prefs = finale.FCLyricsPrefs },
{ prefs = finale.FCMiscDocPrefs },
{ prefs = finale.FCMultiMeasureRestPrefs },
{ prefs = finale.FCMusicCharacterPrefs },
{ prefs = finale.FCMusicSpacingPrefs, handler = handle_music_spacing_prefs },
{ prefs = finale.FCPageFormatPrefs, handler = handle_page_format_prefs },
{ prefs = finale.FCPianoBracePrefs },
{ prefs = finale.FCRepeatPrefs },
{ prefs = finale.FCSizePrefs },
{ prefs = finale.FCSmartShapePrefs, handler = handle_smart_shape_prefs },
{ prefs = finale.FCStaffNamePositionPrefs, handler = handle_name_position_prefs },
{ prefs = finale.FCTiePrefs, handler = handle_tie_prefs },
{ prefs = finale.FCTupletPrefs },
}
local function instantiate_prefs()
for _, obj in pairs(raw_pref_definitions) do
if type(obj.prefs) == "table" then
local ok, prefs = pcall(function() return obj.prefs() end)
obj.prefs = ok and prefs or nil
end
end
end
function load_all_raw_prefs()
instantiate_prefs()
local result = {}
for _, obj in pairs(raw_pref_definitions) do
if obj.prefs then
local tag = obj.prefs:ClassName()
if obj.handler == nil then
obj.prefs:Load(1)
add_props_to_table(result, obj.prefs, tag)
else
result[tag] = {}
obj.handler(obj.prefs, result[tag], true)
end
if not debug.raw_units then
normalize_units_for_raw_section(result[tag], tag)
end
end
end
return result
end
function save_all_raw_prefs(prefs_table)
instantiate_prefs()
for _, obj in pairs(raw_pref_definitions) do
if obj.prefs then
local tag = obj.prefs:ClassName()
denormalize_units_for_raw_section(prefs_table[tag], tag)
if obj.handler == nil then
obj.prefs:Load(1)
set_props_from_table(prefs_table[tag], obj.prefs)
obj.prefs:Save()
else
obj.handler(obj.prefs, prefs_table[tag], false)
end
end
end
for cat in loadall(finale.FCCategoryDefs()) do
expr.resync_expressions_for_category(cat.ID)
end
end
-- endregion
-- region TRANSFORM
-- Functions for transforming between raw and friendly pref tables
--[[
The keys in this table are the names of sections in "transformed"
preferences -- the sections in Document Options or other Finale
menus where a user would find the preferences.
For each key, there is then a table of where in the raw prefs the
preferences come from. The keys here are the names of the PDK classes;
the values are an array of locators, which can be:
- a literal string (to match a preference name exactly)
- a Lua pattern (to match a number of preference names)
- a negated Lua pattern, beginning with "!" (for all preferences except the ones matching the pattern)
If the key for the sub-table includes a ">", it indicates a sub-table in the
transformed table. So "FCFontInfo>Fonts" (under "SmartShapes") means to take the
preferences from FCFontInfo and put them into Fonts under SmartShapes.
If the key for the sub-table includes a "/", it indicates a sub-table in the
source table. So "FCFontInfo/Music" (under "DefaultMusicFont") means that the
preferences are drawn from Music table under FCFontInfo.
]]
local transform_definitions = {
Accidentals = {
FCDistancePrefs = { "^Accidental" },
FCMusicSpacingPrefs = { "AccidentalsGutter" },
FCMusicCharacterPrefs = {
"SymbolNatural", "SymbolFlat", "SymbolSharp", "SymbolDoubleFlat",
"SymbolDoubleSharp", "SymbolPar."
},
},
AlternateNotation = {
FCDistancePrefs = { "^Alternate" },
FCMusicCharacterPrefs = {
"VerticalTwoMeasureRepeatOffset", ".Slash",
"SymbolOneBarRepeat", "SymbolTwoBarRepeat",
},
},
AugmentationDots = {
FCMiscDocPrefs = { "AdjustDotForMultiVoices" },
FCMusicCharacterPrefs = { "SymbolAugmentationDot" },
FCDistancePrefs = { "^AugmentationDot" }
},
Barlines = {
FCDistancePrefs = { "^Barline" },
FCSizePrefs = { "Barline." },
FCMiscDocPrefs = { ".Barline" }
},
Beams = {
FCDistancePrefs = { "Beam." },
FCSizePrefs = { "Beam." },
FCMiscDocPrefs = { "Beam.", "IncludeRestsInFour", "AllowFloatingRests" }
},
Chords = {
FCChordPrefs = { "." },
FCMusicCharacterPrefs = { "Chord." },
FCMiscDocPrefs = { "Chord.", "Fretboard." }
},
Clefs = {
FCMiscDocPrefs = { "ClefResize", ".Clef" },
FCDistancePrefs = { "^Clef" }
},
Flags = {
FCMusicCharacterPrefs = { ".Flag", "VerticalSecondaryGroupAdjust" }
},
Fonts = {
FCFontInfo = {
"^Lyric", "^Text", "^Time", ".Names", "Noteheads$", "^Chord",
"^Alternate", "Dots$", "EndingRepeats", "MeasureNumbers",
"Tablature", "Accidentals", "Flags", "Rests", "Clefs",
"KeySignatures", "MultimeasureRests",
"Tuplets", "Articulations", "Expressions"
}
},
GraceNotes = {
FCSizePrefs = { "Grace." },
FCDistancePrefs = { "GraceNoteSpacing" },
FCMiscDocPrefs = { "Grace." },
},
GridsAndGuides = {
FCGridsGuidesPrefs = { "." }
},
KeySignatures = {
FCDistancePrefs = { "^Key" },
FCMusicCharacterPrefs = { "^SymbolKey" },
FCMiscDocPrefs = { "^Key", "CourtesyKeySigAtSystemEnd" }
},
Layers = {
FCLayerPrefs = { "." },
FCMiscDocPrefs = { "ConsolidateRestsAcrossLayers" }
},
LinesAndCurves = {
FCSizePrefs = { "^Ledger", "EnclosureThickness", "StaffLineThickness", "ShapeSlurTipWidth" },
FCMiscDocPrefs = { "CurveResolution" }
},
Lyrics = {
FCLyricsPrefs = { "." }
},
MultimeasureRests = {
FCMultiMeasureRestPrefs = { "." }
},
MusicSpacing = {
FCMusicSpacingPrefs = { "!Gutter$" },
FCMiscDocPrefs = { "ScaleManualNotePositioning" },
["FCAllotments>Allotments"] = { "." }
},
NotesAndRests = {
FCMiscDocPrefs = { "UseNoteShapes", "CrossStaffNotesInOriginal" },
FCDistancePrefs = { "^Space" },
FCMusicCharacterPrefs = { "^Symbol.*Rest$", "^Symbol.*Notehead$", "^Vertical.*Rest$"}
},
PianoBracesAndBrackets = {
FCPianoBracePrefs = { "." },
FCDistancePrefs = { "GroupBracketDefaultDistance" }
},
Repeats = {
FCRepeatPrefs = { "." },
FCMusicCharacterPrefs = { "RepeatDot$" }
},
Stems = {
FCSizePrefs = { "Stem." },
FCDistancePrefs = { "StemVerticalNoteheadOffset" },
FCMiscDocPrefs = { "UseStemConnections", "DisplayReverseStemming" }
},
Text = {
FCMiscDocPrefs = { "DateFormat", "SecondsInTimeStamp", "TextTabCharacters" },
},
Ties = {
FCTiePrefs = { "." }
},
TimeSignatures = {
FCMiscDocPrefs = { ".TimeSig", "TimeSigCompositeDecimals" },
FCMusicCharacterPrefs = { ".TimeSig" },
FCDistancePrefs = { "^TimeSig" },
},
Tuplets = {
FCTupletPrefs = { "." }
},
Categories = {
FCCategoryDefs = { "." }
},
PageFormat = {
FCPageFormatPrefs = { "." }
},
SmartShapes = {
FCSmartShapePrefs = { "^Symbol", "^Hairpin", "^Line", "HookLength", "OctavesAsText", "ID$" },
FCMusicCharacterPrefs = { ".Octave", "SymbolTrill", "SymbolWiggle" },
["FCFontInfo>Fonts"] = { "^SmartShape" }, -- incorporate a top-level menu from the source as a submenu
["FCSmartShapePrefs>SmartSlur"] = { "Slur." },
["FCSmartShapePrefs>GuitarBend"] = { "GuitarBend[^D]" },
["FCFontInfo>GuitarBend>Fonts"] = { "^Guitar" }
},
NamePositions = {
["FCGroupNamePositionPrefs>GroupNames"] = { "." },
["FCStaffNamePositionPrefs>StaffNames"] = { "." },
},
DefaultMusicFont = {
["FCFontInfo/Music"] = { "." }
}
}
function is_pattern(s) return string.find(s, "[^%a%d]") end
function matches_negatable_pattern(s, pattern)
local negate = pattern:sub(1, 1) == '!'
if negate then pattern = pattern:sub(2) end
local found = string.find(s, pattern)
return (found ~= nil) ~= negate
end
function transform_to_friendly(all_raw_prefs)
local function copy_items(source_table, dest_table, all_defs)
local target
local function copy_matching(pattern, category)
local source = source_table[category];
if string.find(category, "/") then
local main, sub = string.match(category, "([%a%d]+)/([%a%d]+)")
source = source_table[main][sub]
end
if not source then return end
for k, v in pairs(source) do
if matches_negatable_pattern(k, pattern) then target[k] = v end
end
end
for category, locators in pairs(all_defs) do
target = dest_table
if string.find(category, ">") then
for dest_menu in string.gmatch(category, ">(%a+)") do
if target[dest_menu] == nil then target[dest_menu] = {} end
target = target[dest_menu]
end
category = string.match(category, "^%a+")
end
for _, locator in pairs(locators) do
if is_pattern(locator) then
copy_matching(locator, category)
else
target[locator] = source_table[category][locator]
end
end
end
end
local result = {}
for transformed_category, all_defs in pairs(transform_definitions) do
result[transformed_category] = {}
copy_items(all_raw_prefs, result[transformed_category], all_defs)
end
return result
end
function transform_to_raw(prefs_to_import)
local function copy_matching(import_items, raw_items, pattern)
if not import_items then return end
if raw_items[1] and import_items[1] then
-- treat an array as a complete set
while #raw_items > 0 do
table.remove(raw_items)
end
for k, v in ipairs(import_items) do
raw_items[k] = v
end
else
for k, _ in pairs(raw_items) do
if matches_negatable_pattern(k, pattern) then
if type(raw_items[k]) == "table" then
copy_matching(import_items[k], raw_items[k], ".")
elseif import_items[k] ~= nil then
raw_items[k] = import_items[k]
end
end
end
end
end
local function copy_section(import_items, raw_items, locators)
if raw_items then
for _, locator in pairs(locators) do
if not is_pattern(locator) then
locator = "^" .. locator .. "$"
end
copy_matching(import_items, raw_items, locator)
end
end
end
local raw_prefs = load_all_raw_prefs()
for import_cat, import_values in pairs(prefs_to_import) do
local transform_defs = transform_definitions[import_cat]
if transform_defs then
for raw_cat, locators in pairs(transform_defs) do
local source = import_values
local dest = raw_prefs[raw_cat]
if string.find(raw_cat, ">") then
local first = true
for segment in string.gmatch(raw_cat, "[%a%d]+") do
if first then
dest = raw_prefs[segment]
first = false
else
source = source and source[segment]
end
end
elseif string.find(raw_cat, "/") then
for segment in string.gmatch(raw_cat, "[%a%d]+") do
dest = raw_prefs[segment] or dest[segment]
end
end
copy_section(source, dest, locators)
end
end
end
-- Copy new expression categories
local cat_defs_to_import = prefs_to_import["Categories"]
if cat_defs_to_import then
local raw_cat_defs = raw_prefs["FCCategoryDefs"]
for k, v in pairs(cat_defs_to_import) do
if not raw_cat_defs[k] then raw_cat_defs[k] = v end
end
end
return raw_prefs
end
-- endregion
-- region NORMALIZE
--[[
Functions for normalizing/denormalizing between PDK native values and
user-friendly values. Currently, "user-friendly" = EVPU for measurements;
normalizing to a selectable unit may come later.
The keys in the selector table are the names of PDK classes; the values are
arrays whose keys are either property names or Lua patterns matching multiple
property names and whose values are string values indicating the normalizing
function. The first character in this string indicates whether we need to
"d"ivide or "m"ultiply, and the rest of the string is the operand to be used.
So "d64" means that we need to divide by 64 to normalize to EVPU (i.e., PDK
units are EFIX).
An optional parameter passed into modify_units_for_section indicates whether