forked from finale-lua/lua-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoteheads_change_by_layer.lua
299 lines (280 loc) · 12.1 KB
/
noteheads_change_by_layer.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
function plugindef()
finaleplugin.RequireSelection = true
finaleplugin.Author = "Carl Vine"
finaleplugin.AuthorURL = "https://carlvine.com/lua/"
finaleplugin.Copyright = "https://creativecommons.org/licenses/by/4.0/"
finaleplugin.Version = "0.34"
finaleplugin.Date = "2024/05/20"
finaleplugin.AdditionalMenuOptions = [[
Noteheads Change Repeat
]]
finaleplugin.AdditionalUndoText = [[
Noteheads Change Repeat
]]
finaleplugin.AdditionalDescriptions = [[
Change notehead shapes on a specific layer of the current selection (no dialog)
]]
finaleplugin.AdditionalPrefixes = [[
no_dialog = true
]]
finaleplugin.MinJWLuaVersion = 0.70
finaleplugin.ScriptGroupName = "Noteheads Change by Layer"
finaleplugin.ScriptGroupDescription = "Change notehead shapes on a specific layer of the current selection"
finaleplugin.Notes = [[
Change __notehead shapes__ on a specific layer of the current
selection to one of these options:
> Circled | Default | Diamond | Guitar Diamond
> Hidden | Number | Round | Slash | Square
> Strikethrough | Triangle | Wedge | X
This script produces an ordered list of notehead types,
each line beginning with a configurable _hotkey_.
Call the script, type the _hotkey_ and hit [Return].
In __SMuFL__ fonts like _Finale Maestro_, shapes can vary according
to duration values. Most duration-dependent shapes are not available
in Finale's old (non-SMuFL) _Maestro_ and _Engraver_ fonts.
_Diamond (Guitar)_ is like _Diamond_ except quarter notes and shorter use filled diamonds.
_Number_ lets you specify any font character as a number including
__SMuFL__ (Unicode) numbers in the form __0xe0e1__ or __0xE0E1__.
To repeat the same action as last time without a confirmation dialog either select the
__Noteheads Change Repeat__ menu item or hold down [Shift] when opening the script.
]]
return "Noteheads Change by Layer...", "Noteheads Change by Layer",
"Change notehead shapes on a specific layer of the current selection"
end
no_dialog = no_dialog or false
local notehead = require("library.notehead")
local mixin = require("library.mixin")
local configuration = require("library.configuration")
local utils = require("library.utils")
local library = require("library.general_library")
local layer = require("library.layer")
local diamond = { smufl = 0xE0E1, non_smufl = 79 }
local script_name = library.calc_script_name()
local refocus_document = false
local dialog_options = { -- notehead name (and key), HOTKEY
{ "Circled", "C" },
{ "Default", "A" },
{ "Diamond", "D" },
{ "Diamond_Guitar", "G" },
{ "Hidden", "H" },
{ "Number", "N" },
{ "Round", "R" },
{ "Slash", "S" },
{ "Square", "Q" },
{ "Strikethrough", "E" },
{ "Triangle", "T" },
{ "Wedge", "W" },
{ "X", "Z" }
}
local config = {
layer_num = 1,
ignore_duplicates = 0,
shape = "default",
glyph = "0xe0e1",
window_pos_x = false,
window_pos_y = false
}
for _, v in ipairs(dialog_options) do -- add HOTKEYS to CONFIG
config[v[1]] = v[2] -- map name (key) onto HOTKEY
end
local function dialog_set_position(dialog)
if config.window_pos_x and config.window_pos_y then
dialog:StorePosition()
dialog:SetRestorePositionOnlyData(config.window_pos_x, config.window_pos_y)
dialog:RestorePosition()
end
end
local function dialog_save_position(dialog)
dialog:StorePosition()
config.window_pos_x = dialog.StoredX
config.window_pos_y = dialog.StoredY
configuration.save_user_settings(script_name, config)
end
local function user_chooses_glyph()
local x = 230
local y_diff = finenv.UI():IsOnMac() and 3 or 0 -- extra y-offset for Mac text box
local base_glyph = tonumber(config.glyph) or diamond.smufl
local msg
if library.is_font_smufl_font() then
if base_glyph < 0xE000 then base_glyph = diamond.smufl end -- < 57344
config.glyph = string.format("0x%X", base_glyph)
msg = "... as a plain integer, or hex value like \"0xE0E1\". "
.. "The Default Music Font is SMuFL compliant so glyph numbers "
.. "should be higher than 57344 (\"0xE000\")."
else
if base_glyph >= 0x1000 then base_glyph = diamond.non_smufl end
config.glyph = tostring(base_glyph)
msg = "The Default Music Font is not SMuFL compliant so glyph numbers "
.. "should be lower than 4096 (\"0x1000\") "
.. "and might contain no characters higher than 255"
end
local dialog = mixin.FCXCustomLuaWindow():SetTitle(finaleplugin.ScriptGroupName)
dialog:CreateStatic(0, y_diff):SetWidth(x + 70)
:SetText("Enter required character (glyph) number:")
dialog:CreateStatic(0, y_diff + 25):SetWidth(x + 100):SetHeight(50)
:SetText(msg)
local glyph = dialog:CreateEdit(x, 0):SetText(config.glyph)
dialog:CreateOkButton()
dialog:CreateCancelButton()
dialog_set_position(dialog)
dialog:RegisterInitWindow(function() glyph:SetKeyboardFocus() end)
dialog:RegisterCloseWindow(function(self) dialog_save_position(self) end)
dialog:RegisterHandleOkButtonPressed(function() config.glyph = glyph:GetText() end)
return (dialog:ExecuteModal() == finale.EXECMODAL_OK)
end
local function reassign_keystrokes(parent, index)
local y_step, x_wide = 17, 180
local offset = finenv.UI():IsOnMac() and 3 or 0
local dialog = mixin.FCXCustomLuaWindow():SetTitle("Noteheads: Reassign Keys")
local is_duplicate, errors = false, {}
local y = 0
for _, v in ipairs(dialog_options) do -- add all options with keycodes
dialog:CreateEdit(0, y - offset, v[1]):SetText(config[v[1]]):SetWidth(20)
:AddHandleCommand(function(self)
local str = self:GetText():sub(-1):upper()
self:SetText(str):SetKeyboardFocus()
end)
dialog:CreateStatic(25, y):SetText(v[1]):SetWidth(x_wide)
y = y + y_step
end
y = y + 7
local ignore = dialog:CreateCheckbox(0, y):SetWidth(x_wide)
:SetText("Ignore duplicate assignments"):SetCheck(config.ignore_duplicates or 0)
dialog:CreateOkButton()
dialog:CreateCancelButton()
dialog:RegisterInitWindow(function(self)
self:GetControl(dialog_options[index][1]):SetKeyboardFocus()
end)
dialog_set_position(dialog)
dialog:RegisterHandleOkButtonPressed(function(self)
local assigned = {}
for _, v in ipairs(dialog_options) do
local key = self:GetControl(v[1]):GetText()
if key == "" then key = "?" end
config[v[1]] = key -- save for another possible run-through
config.ignore_duplicates = ignore:GetCheck()
if config.ignore_duplicates == 0 then -- DON'T ignore duplicates
if assigned[key] then -- previously assigned
is_duplicate = true
if not errors[key] then errors[key] = { assigned[key] } end
table.insert(errors[key], v[1])
else
assigned[key] = v[1] -- flag key assigned
end
end
end
if is_duplicate then -- list reassignment duplications
local msg = ""
for i, v in pairs(errors) do
if msg ~= "" then msg = msg .. "\n\n" end
msg = msg .. "Key \"" .. i .. "\" is assigned to: "
for j, w in ipairs(v) do
if j > 1 then msg = msg .. " and " end
msg = msg .. "\"" .. w .. "\""
end
end
dialog:CreateChildUI():AlertError(msg, "Duplicate Key Assignment")
end
end)
local ok = (dialog:ExecuteModal(parent) == finale.EXECMODAL_OK)
return ok, is_duplicate
end
local function user_chooses_shape()
local x_offset = 140
local y_step = 18
local box_high = #dialog_options * 17 + 5
local dialog = mixin.FCXCustomLuaWindow():SetTitle(finaleplugin.ScriptGroupName)
dialog:CreateStatic(0, 0):SetText("Select note shape:"):SetWidth(150)
local shape_list = dialog:CreateListBox(0, y_step):SetWidth(x_offset - 10):SetHeight(box_high)
local function fill_shape_list()
local join = finenv.UI():IsOnMac() and "\t" or ": "
shape_list:Clear()
for i, v in ipairs(dialog_options) do
local item = (i ~= 4) and v[1] or "Guitar Diamond"
shape_list:AddString(config[v[1]] .. join .. item)
if v[1]:lower() == config.shape then
shape_list:SetSelectedItem(i - 1)
end
end
end
fill_shape_list()
local function show_info()
utils.show_notes_dialog(dialog, "About " .. finaleplugin.ScriptGroupName, 420, 310)
refocus_document = true
end
local function reassign_keys()
local ok, is_duplicate = true, true
while ok and is_duplicate do -- wait for valid choice in reassign_keystrokes()
ok, is_duplicate = reassign_keystrokes(dialog, shape_list:GetSelectedItem() + 1)
refocus_document = true
end
if ok then
fill_shape_list()
else -- reinstall hotkeys from user config
configuration.get_user_settings(script_name, config)
end
end
local y = y_step * 3
local max = layer.max_layers()
dialog:CreateStatic(x_offset, y):SetText("Layer number (1-" .. max .. "):"):SetWidth(110)
y = y + y_step-- + 2
local mac_offset = finenv.UI():IsOnMac() and 3 or 0 -- + vertical offset for Mac edit boxes
local save_layer = config.layer_num
local layer_num = dialog:CreateEdit(x_offset + 38, y - mac_offset):SetWidth(20)
:SetText(save_layer)
:AddHandleCommand(function(self)
local val = self:GetText():lower()
if val:find("[^0-" .. max .. "]") then
if val:find("r") then reassign_keys()
elseif val:find("[?q]") then show_info()
end
elseif val ~= "" then
save_layer = val:sub(-1) -- layer number has one char
end
self:SetText(save_layer):SetKeyboardFocus()
end)
y = y + y_step-- + 2
dialog:CreateStatic(x_offset + 12, y):SetText("(0 = all layers)"):SetWidth(105)
y = y + y_step-- + 2
dialog:CreateButton(x_offset + 38, y, "q"):SetText("?"):SetWidth(20)
:AddHandleCommand(function() show_info() end)
y = y_step * 3 + y
dialog:CreateButton(x_offset, y)
:SetText("Change Hotkeys"):SetWidth(100)
:AddHandleCommand(function() reassign_keys() end)
dialog:CreateOkButton()
dialog:CreateCancelButton()
dialog_set_position(dialog)
dialog:RegisterInitWindow(function()
local q = dialog:GetControl("q")
q:SetFont(q:CreateFontInfo():SetBold(true))
shape_list:SetKeyboardFocus()
end)
dialog:RegisterHandleOkButtonPressed(function()
local item = shape_list:GetSelectedItem() + 1
config.shape = dialog_options[item][1]:lower()
config.layer_num = layer_num:GetInteger()
end)
dialog:RegisterCloseWindow(function(self) dialog_save_position(self) end)
return (dialog:ExecuteModal() == finale.EXECMODAL_OK)
end
local function change_noteheads()
configuration.get_user_settings(script_name, config, true)
local qim = finenv.QueryInvokedModifierKeys
local mod_key = qim and (qim(finale.CMDMODKEY_ALT) or qim(finale.CMDMODKEY_SHIFT))
if no_dialog or mod_key or user_chooses_shape() then
if config.shape == "number" then
if not user_chooses_glyph() then return end -- user cancelled
config.shape = tonumber(config.glyph) -- glyph -> shape NUMBER
end
for entry in eachentrysaved(finenv.Region(), config.layer_num) do
if entry:IsNote() then
for note in each(entry) do
notehead.change_shape(note, config.shape)
end
end
end
end
if refocus_document then finenv.UI():ActivateDocumentWindow() end
end
change_noteheads()