[JW Lua] Insert Staff Or Reorder Staves

Martin Marris mmarris at notecraft.com
Thu Dec 17 14:42:07 CET 2020


OK, after a lot of head-scratching I figured it out. The script below appends a new staff to the bottom of a score, then moves the new staff to the top of the score. It is just a proof of concept and only processes the staff order in Scroll View. I am now working on Page View which should be similar except that (as I understand it) all the System Staves haves to be processed, using a loop. The index starts at 1 (0 is Scroll View).

 

At one point I needed to store the absolute value of an element in a Lua table, and then break the pointer to the table because the table was going to change later and I needed the original value preserved. The only way I could find was to load that table value into a variable, and then … destroy the table. Silly, because I needed that table, so I had to create it again immediately after extracting the “unchangeable value” of the element.

 

There must be a much better way, although of course this is a Lua language question and has nothing to do with JW Lua per se. I have read the chapter about tables in manual (“Programming in Lua”) but did not obtain enlightenment.

 

Anyway, this code works, and that’s what matters.

 

==============

 

--append a new staff to the bottom of the score

local staffID = finale.FCStaves.Append()

local staff = finale.FCStaff()

staff:Load(staffID)

staff.InstrumentUUID = finale.FFUUID_BLANKSTAFF

local strName =finale.FCString()

strName.LuaString="NEW STAFF"

staff:SaveNewFullNameString (strName)

local strAbrev =finale.FCString()

strAbrev.LuaString="N. STF."

staff:SaveNewAbbreviatedNameString (strAbrev)

staff:Save()

 

--store the value of the top staff prior to re-ordering the staves

local sysstaff_table = {}

local sysstaves= finale.FCSystemStaves()

sysstaves:LoadScrollView()

for sysstaff in each(sysstaves) do

    table.insert(sysstaff_table, sysstaff)

end

bottom_staff = sysstaff_table[#sysstaff_table]

sysstaff_table = nil    -- the only way I can find to break the pointer to the table is to destroy the table!

 

--which means the same table must now be laboriously re-created anew....

local sysstaff_table = {}

local sysstaves= finale.FCSystemStaves()

sysstaves:LoadScrollView()

for sysstaff in each(sysstaves) do

    table.insert(sysstaff_table, sysstaff)

end

 

--move each staff down by one staff

for i = #sysstaff_table, 1, -1 do

    sysstaff = sysstaff_table[i]

    next_sysstaff = sysstaff_table[i-1]

    if next_sysstaff then

        sysstaff.Staff = next_sysstaff.Staff        --process all staves except the top one.

    else

        sysstaff.Staff = bottom_staff.Staff         --replace value of top staff with value of the appended staff

    end

end

 

sysstaves:SaveAll()

 

=======

Martin

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://jwmusic.nu/pipermail/jwlua_jwmusic.nu/attachments/20201217/3373aad7/attachment.html>


More information about the JWLua mailing list