forked from finale-lua/lua-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UI_switch_to_selected_part.lua
72 lines (66 loc) · 3.12 KB
/
UI_switch_to_selected_part.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
function plugindef()
finaleplugin.NoStore = true
finaleplugin.Author = "CJ Garcia"
finaleplugin.Copyright = "© 2022 CJ Garcia Music"
finaleplugin.Version = "1.3.1"
finaleplugin.Date = "February 14, 2022"
finaleplugin.CategoryTags = "UI"
return "Switch To Selected Part", "Switch To Selected Part",
"Switches to the first part of the top staff in a selected region in a score. Switches back to the score if viewing a part."
end
local library = require("library.general_library")
function ui_switch_to_selected_part()
local music_region = finenv.Region()
local selection_exists = not music_region:IsEmpty()
local ui = finenv.UI()
local top_cell = library.get_top_left_selected_or_visible_cell()
local parts = finale.FCParts()
parts:LoadAll()
local current_part = parts:GetCurrent()
if current_part:IsScore() then
local part_ID = nil
parts:SortByOrderID()
for part in each(parts) do
if (not part:IsScore()) and part:IsStaffIncluded(top_cell.Staff) then
part_ID = part:GetID()
-- stop searching if the top selected staff is visible on the system of the first selected measure
local found_staff = false
local this_part = finale.FCPart(part_ID)
this_part:SwitchTo()
local systems = finale.FCStaffSystems()
systems:LoadAll()
local system = systems:FindMeasureNumber(top_cell.Measure)
if system then
local staves = finale.FCSystemStaves()
staves:LoadAllForItem(system.ItemNo)
found_staff = staves:FindStaff(top_cell.Staff) ~= nil
end
this_part:SwitchBack()
if found_staff then break end
end
end
if part_ID ~= nil then
local part = finale.FCPart(part_ID)
part:ViewInDocument()
-- Finale does not always calculate the selected region correctly for the part, leading to an invalid selection state, so fix it when switching to parts
if selection_exists then
music_region:SetInstrumentList(0)
music_region:SetStartStaff(top_cell.Staff)
music_region:SetEndStaff(top_cell.Staff)
music_region:SetInDocument()
end
-- scroll the selected region into view, because Finale sometimes loses track of it
ui:MoveToMeasure(top_cell.Measure, music_region.StartStaff)
else
finenv.UI():AlertInfo("Hmm, this part doesn't seem to be generated.\nTry generating parts and try again", "No Part Detected")
end
else
local score_ID = parts:GetScore()
local part = finale.FCPart(score_ID:GetID())
-- Finale manages to keep the selected region displayed when switching back to score, so nothing needs to be done here
part:ViewInDocument()
-- scroll the selected region into view, because Finale sometimes loses track of it
ui:MoveToMeasure(top_cell.Measure, top_cell.Staff)
end
end
ui_switch_to_selected_part()