Skip to content

Commit 4e9d0d6

Browse files
committed
add more tests to process_all_documents
1 parent b6f67b0 commit 4e9d0d6

File tree

1 file changed

+71
-9
lines changed

1 file changed

+71
-9
lines changed

samples/process_all_documents.lua

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
function plugindef()
2+
finaleplugin.RequireDocument = false
23
finaleplugin.MinJWLuaVersion = 0.74
3-
finaleplugin.NoStore = false -- With RGP Lua 0.74 and higher, setting this to true causes all changes to rollback.
4-
finaleplugin.HandlesUndo = true -- A true value here does not suppress the automatic Undo logic in FCDocument.
4+
-- A true value of NoStore causes all changes to be rolled back and prevent FCDocument instances from being saved.
5+
finaleplugin.NoStore = false
6+
-- A true value of HandlesUndo does not prevent the automatic Undo logic in FCDocument/FCLuaIterator from saving changes,
7+
-- but it prevents other changes that are not protected by an explicit call to StartNewUndoBlock.
8+
finaleplugin.HandlesUndo = true
59
finaleplugin.Notes = [[
6-
This script demonstrates how to process all open documents. The SwitchTo/SwitchBack functions automatically
7-
manage the Undo blocks per document, based on the NoStore setting.
10+
This script demonstrates how to process all open documents or batch process a list of files with an iterator.
11+
The SwitchTo/SwitchBack functions automatically manage the Undo blocks per document, based on the NoStore
12+
and HandlesUndo settings. For versions of RGP Lua before 0.74, the Undo handling is not as consistent, and neither
13+
FCLuaIterator nor FCDocument know about the NoStore or HandlesUndo settings.
814
]]
915
return "0--process_all_documents.lua"
1016
end
1117

1218
local shift_amount = 144
19+
local use_iterator = true
20+
local use_files = false
21+
local files = (function()
22+
local retval = finale.FCStrings()
23+
local homepath = finale.FCString()
24+
homepath:SetUserPath()
25+
homepath:AssureEndingPathDelimiter()
26+
for _, filename in ipairs({ "Desktop/1.musx", "Desktop/2.musx" }) do
27+
local result = finale.FCString(homepath.LuaString .. filename)
28+
retval:AddCopy(result)
29+
end
30+
return retval
31+
end)()
1332

1433
local function file_name()
1534
local fpath = finale.FCString()
@@ -19,10 +38,7 @@ local function file_name()
1938
return fname.LuaString
2039
end
2140

22-
local docs = finale.FCDocuments()
23-
docs:LoadAll()
24-
for doc in each(docs) do
25-
doc:SwitchTo(finale.FCString(file_name() .. " " .. doc.ID), false)
41+
local function process_document_with_name(doc, filepath)
2642
local region = finale.FCMusicRegion()
2743
region:SetFullDocument()
2844
for entry in eachentrysaved(region) do
@@ -31,5 +47,51 @@ for doc in each(docs) do
3147
if not finaleplugin.NoStore then
3248
region:Redraw() -- do not call Redraw when using NoStore, or you can get confusing visible artifacts.
3349
end
34-
doc:SwitchBack(true) -- true: changes successful (will be saved unless NoStore is true)
50+
local filename = finale.FCString()
51+
filepath:SplitToPathAndFile(nil, filename)
52+
print("processed " .. filename.LuaString .. " [" .. doc.ID .. "]")
53+
end
54+
55+
local function process_document(doc)
56+
local filepath = finale.FCString()
57+
doc:GetPath(filepath)
58+
process_document_with_name(doc, filepath)
3559
end
60+
61+
local function move_expression_baseline()
62+
local baselines = finale.FCBaselines()
63+
baselines:LoadAllForPiece(finale.BASELINEMODE_EXPRESSIONABOVE)
64+
local baseline = baselines:AssureSavedStaffForPiece(finale.BASELINEMODE_EXPRESSIONABOVE, 1)
65+
if baseline then
66+
baseline.VerticalOffset = baseline.VerticalOffset + 24
67+
baseline:Save()
68+
end
69+
end
70+
71+
finenv.StartNewUndoBlock("Explict changes", false)
72+
73+
-- this change is suppressed if either NoStore or HandlesUndo is true
74+
-- otherwise, it creates a separate undo entry
75+
move_expression_baseline()
76+
77+
if use_iterator then
78+
local iterator = finale.FCLuaIterator()
79+
if use_files then
80+
iterator:ForEachFileSaved(files, process_document_with_name)
81+
else
82+
iterator:ForEachDocument(process_document)
83+
end
84+
else
85+
local docs = finale.FCDocuments()
86+
local count = docs:LoadAll()
87+
print("got " .. count .. " documents")
88+
for doc in each(docs) do
89+
doc:SwitchTo(finale.FCString(file_name() .. " " .. doc.ID), true) -- true: save current changes
90+
process_document(doc)
91+
doc:SwitchBack(true) -- true: changes successful (will be saved unless NoStore is true)
92+
end
93+
end
94+
95+
-- this change is suppressed if either NoStore or HandlesUndo is true
96+
-- otherwise, it creates a separate undo entry
97+
move_expression_baseline()

0 commit comments

Comments
 (0)