|
| 1 | +function plugindef() |
| 2 | + finaleplugin.RequireSelection = false |
| 3 | + finaleplugin.Author = "CJ Garcia" |
| 4 | + finaleplugin.Copyright = "2024 MuseCraft Studio" |
| 5 | + finaleplugin.Version = "1.0" |
| 6 | + finaleplugin.Date = "5/27/2024" |
| 7 | + finaleplugin.RevisionNotes = [[ |
| 8 | + May 27, 2024: Script work began with idea from Burt Goldstein |
| 9 | + May 28, 2024: Version 1.0 |
| 10 | + ]] |
| 11 | + finaleplugin.CategoryTags = "Articulation, Expression" |
| 12 | + return "Articulation and Expression Swap", "Articulation and Expression Swap", |
| 13 | + "Replaces the selected articulation with the selected expression (or vice versa) for the full document if a region is not selected." |
| 14 | +end |
| 15 | + |
| 16 | +SelectedItem = nil |
| 17 | + |
| 18 | +local function assignExpression(exp_id, entry) |
| 19 | + local exp = finale.FCExpression() |
| 20 | + exp:SetStaff(entry.Staff) |
| 21 | + exp:SetVisible(true) |
| 22 | + exp:SetMeasurePos(entry:GetMeasurePos()) |
| 23 | + exp:SetScaleWithEntry(true) |
| 24 | + exp:SetLayerAssignment(entry.LayerNumber) |
| 25 | + exp:SetID(exp_id) |
| 26 | + |
| 27 | + local note_cell = finale.FCCell(entry.Measure, entry.Staff) |
| 28 | + exp:SaveNewToCell(note_cell) |
| 29 | +end |
| 30 | + |
| 31 | +local function run(art_id, exp_id) |
| 32 | + if (art_id == nil) and (exp_id ~= nil) then |
| 33 | + finenv.UI():AlertInfo("Articulation ID must be a number. Exiting process", "Articulation entry error") |
| 34 | + return |
| 35 | + elseif (art_id ~= nil) and (exp_id == nil) then |
| 36 | + finenv.UI():AlertInfo("Expression ID must be a number. Exiting process", "Expression entry error") |
| 37 | + return |
| 38 | + elseif (art_id == nil) and (exp_id == nil) then |
| 39 | + finenv.UI():AlertInfo("The articulation ID and expression ID must both be a number. Exiting process", |
| 40 | + "Entry error") |
| 41 | + return |
| 42 | + end |
| 43 | + |
| 44 | + local art_defs = finale.FCArticulationDefs() |
| 45 | + art_defs:LoadAll() |
| 46 | + local has_art = false |
| 47 | + for art in each(art_defs) do |
| 48 | + if art:GetItemNo() == art_id then |
| 49 | + has_art = true |
| 50 | + break |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + local exp_defs = finale.FCTextExpressionDefs() |
| 55 | + exp_defs:LoadAll() |
| 56 | + local has_exp = false |
| 57 | + for ted in each(exp_defs) do |
| 58 | + if ted:GetItemNo() == exp_id then |
| 59 | + has_exp = true |
| 60 | + break |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + if (has_art ~= true) and (has_exp == true) then |
| 65 | + finenv.UI():AlertInfo("The articulation with ID " .. art_id .. " could not be found. Exiting process", |
| 66 | + "Unable to find articulation") |
| 67 | + return |
| 68 | + elseif (has_art == true) and (has_exp ~= true) then |
| 69 | + finenv.UI():AlertInfo("The expression with ID " .. exp_id .. " could not be found. Exiting process", |
| 70 | + "Unable to find expression") |
| 71 | + return |
| 72 | + elseif (has_art ~= true) and (has_exp ~= true) then |
| 73 | + finenv.UI():AlertInfo( |
| 74 | + "Neither the articulation with ID " .. |
| 75 | + art_id .. " nor the expression with ID " .. exp_id .. " could not be found. Exiting process", |
| 76 | + "Unable to find items") |
| 77 | + return |
| 78 | + end |
| 79 | + |
| 80 | + local music_region = finenv.Region() |
| 81 | + if music_region:IsEmpty() == true then |
| 82 | + music_region = finale.FCMusicRegion() |
| 83 | + music_region:SetFullDocument() |
| 84 | + end |
| 85 | + |
| 86 | + local count = 0 |
| 87 | + |
| 88 | + if SelectedItem == 0 then |
| 89 | + -- replace articulation with expression |
| 90 | + for noteentry in eachentrysaved(music_region) do |
| 91 | + local arts = noteentry:CreateArticulations() |
| 92 | + for a in eachbackwards(arts) do |
| 93 | + if a:GetID() == art_id then |
| 94 | + count = count + 1 |
| 95 | + a:DeleteData() |
| 96 | + assignExpression(exp_id, noteentry) |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + if count ~= 0 then |
| 101 | + if count > 1 then |
| 102 | + finenv.UI():AlertInfo("Replaced " .. count .. " occurances of articulation with the ID of " .. art_id, |
| 103 | + "Success") |
| 104 | + else |
| 105 | + finenv.UI():AlertInfo("Replaced " .. count .. " occurance of articulation with the ID of " .. art_id, |
| 106 | + "Success") |
| 107 | + end |
| 108 | + else |
| 109 | + finenv.UI():AlertInfo("No occurances of articulation with the ID of " .. art_id .. " was found.", |
| 110 | + "Nothing found") |
| 111 | + end |
| 112 | + else |
| 113 | + -- replace expression with articulation |
| 114 | + for noteentry in eachentrysaved(music_region) do |
| 115 | + local pin_point = finale.FCMusicRegion() |
| 116 | + pin_point:SetStartStaff(noteentry:GetStaff()) |
| 117 | + pin_point:SetEndStaff(noteentry:GetStaff()) |
| 118 | + pin_point:SetStartMeasure(noteentry:GetMeasure()) |
| 119 | + pin_point:SetEndMeasure(noteentry:GetMeasure()) |
| 120 | + pin_point:SetStartMeasurePos(noteentry:GetMeasurePos()) |
| 121 | + pin_point:SetEndMeasurePos(noteentry:GetMeasurePos()) |
| 122 | + |
| 123 | + local expressions = finale.FCExpressions() |
| 124 | + expressions:LoadAllForRegion(pin_point) |
| 125 | + for exp in eachbackwards(expressions) do |
| 126 | + local ted = exp:CreateTextExpressionDef() |
| 127 | + if ted:GetItemNo() == exp_id then |
| 128 | + count = count + 1 |
| 129 | + exp:DeleteData() |
| 130 | + local art = finale.FCArticulation() |
| 131 | + art:SetNoteEntry(noteentry) |
| 132 | + art:SetID(art_id) |
| 133 | + art:SaveNew() |
| 134 | + end |
| 135 | + end |
| 136 | + end |
| 137 | + if count ~= 0 then |
| 138 | + if count > 1 then |
| 139 | + finenv.UI():AlertInfo("Replaced " .. count .. " occurances of expression with the ID of " .. exp_id, |
| 140 | + "Success") |
| 141 | + else |
| 142 | + finenv.UI():AlertInfo("Replaced " .. count .. " occurance of expression with the ID of " .. exp_id, |
| 143 | + "Success") |
| 144 | + end |
| 145 | + else |
| 146 | + finenv.UI():AlertInfo("No occurances of expression with the ID of " .. exp_id .. " was found.", |
| 147 | + "Nothing found") |
| 148 | + end |
| 149 | + end |
| 150 | +end |
| 151 | + |
| 152 | +local str = finale.FCString() |
| 153 | +str.LuaString = "Articulation Replacement" |
| 154 | +local dialog = finale.FCCustomLuaWindow() |
| 155 | +dialog:SetTitle(str) |
| 156 | + |
| 157 | +local art_button = dialog:CreateButton(0, 45) |
| 158 | +str.LuaString = "Select..." |
| 159 | +art_button:SetText(str) |
| 160 | + |
| 161 | +local exp_button = dialog:CreateButton(150, 45) |
| 162 | +str.LuaString = "Select..." |
| 163 | +exp_button:SetText(str) |
| 164 | + |
| 165 | +local art_text = dialog:CreateStatic(0, 0) |
| 166 | +str.LuaString = "Articulation ID" |
| 167 | +art_text:SetText(str) |
| 168 | +local art_box = dialog:CreateEdit(0, 16) |
| 169 | +local art_str = finale.FCString() |
| 170 | +art_str.LuaString = "" |
| 171 | + |
| 172 | +local exp_text = dialog:CreateStatic(150, 0) |
| 173 | +str.LuaString = "Expression ID" |
| 174 | +exp_text:SetText(str) |
| 175 | +local exp_box = dialog:CreateEdit(150, 16) |
| 176 | +local exp_str = finale.FCString() |
| 177 | +exp_str.LuaString = "" |
| 178 | + |
| 179 | +local radio_group = dialog:CreateRadioButtonGroup(0, 100, 2) |
| 180 | +local strs = finale.FCStrings() |
| 181 | +strs:AddCopy(finale.FCString("Find articulation, replace with expression.")) |
| 182 | +strs:AddCopy(finale.FCString("Find expression, replace with articulation.")) |
| 183 | +radio_group:SetText(strs) |
| 184 | +radio_group:SetWidth(225) |
| 185 | + |
| 186 | +dialog:CreateOkButton() |
| 187 | + |
| 188 | +dialog:CreateCancelButton() |
| 189 | + |
| 190 | +local function getUserSelection(controller) |
| 191 | + if controller:GetControlID() == art_button:GetControlID() then |
| 192 | + local art_select = finenv.UI():DisplayArticulationDialog(0) |
| 193 | + art_box:SetText(finale.FCString(tostring(art_select))) |
| 194 | + elseif controller:GetControlID() == exp_button:GetControlID() then |
| 195 | + local art_select = finenv.UI():DisplayExpressionDialog(0, false) |
| 196 | + exp_box:SetText(finale.FCString(tostring(art_select))) |
| 197 | + end |
| 198 | +end |
| 199 | + |
| 200 | +dialog:RegisterHandleCommand(getUserSelection) |
| 201 | + |
| 202 | +if dialog:ExecuteModal(nil) == finale.EXECMODAL_OK then |
| 203 | + art_box:GetText(art_str) |
| 204 | + exp_box:GetText(exp_str) |
| 205 | + SelectedItem = radio_group:GetSelectedItem() |
| 206 | + run(tonumber(art_str.LuaString), tonumber(exp_str.LuaString)) |
| 207 | +end |
0 commit comments