[JW Lua] Chord as string

Chris marcel.denio at gmail.com
Mon Aug 25 07:52:55 CEST 2014


Hello Bart

I made this script (see attachment) to translate the chords into French.
Maybe this will help (missing a library and a expression category)

Best regards
-------------- next part --------------
function plugindef()
   -- This function and the 'finaleplugin' namespace
   -- are both reserved for the plug-in definition.
   finaleplugin.CategoryTags = "Chord"
   return "- Accords en français", "Accords en français", ""
end

local textExp = require "textExpressions"

function decodeNote(scaleNumber,alteration,key)
    local notes = {"LA","SI","DO","RE","MI","FA","SOL"}
    local root = key:CalcScaleRootIndex()
    local indNote = root + scaleNumber
    if indNote > 6 then indNote = indNote - 7 end
  

    local alter = alteration
    if key:IsSharpNote(indNote) then alter = alter + 1 end
    if key:IsFlatNote(indNote) then alter = alter -1 end

    indNote = indNote + 1 -- car 1er index de string.sub -> 1
    return notes[indNote] .. decodeAlter(alter)
end

function decodeAlter(alter)

    if alter > 0 then -- diese
        return string.sub("##", 1,alter)
    elseif alter < 0 then  -- bemol
         return string.sub("bb", 1,-alter)
    end

    return ""
end

function creerSuffixe(chord)
    local str = finale.FCString()
    local suffixe=chord:CreateChordSuffixElements()
    if suffixe == nil then return str end
    
    for character in each(suffixe) do   -- ChordSuffixElement

        if character.PrefixFlat then str:AppendLuaString("b")
        elseif character.PrefixSharp then str:AppendLuaString("#")
        elseif character.PrefixPlus then str:AppendLuaString("+")
        elseif character.PrefixMinus then str:AppendLuaString("-")  end

        if character.NumberRepresentation then
           str:AppendInteger(character.Symbol)
        else
            str:AppendCharacter(character.Symbol)
        end

    end

    return str
end

function main()
    if textExp.FindTextExpressionCategory("Accords francais") == -1 then
        finenv.UI():AlertInfo("Il manque la categorie : 'Accords francais'","Accords francais")
        return
    end

    musicregion= finenv.Region()
    if musicregion:IsEmpty() then  musicregion:SetFullDocument() end

    for m, s in eachcell(musicregion) do
        -- 'm' is the measure number, 's' is the staff number/ID
        local cell = finale.FCCell(m, s)
        local chords = finale.FCChords()
        chords:LoadAllInCell(cell)
        
        local key=cell:GetKeySignature()
 
        for c in each(chords) do
            local  textvalue = decodeNote(c.ScaleNumber,c.Alteration,key) 
                                    .. creerSuffixe(c).LuaString

            local expID = textExp.MakeTextExpression(textvalue,"Accords francais")
            textExp.AttachExpression(c.Staff, c.Measure, c.MeasurePos, expID)          
        end
    end
end

function fontCategory(catdef)
    local font =finale.FCFontInfo()
    catdef:GetTextFontInfo(font)
    local text = font:CreateEnigmaString(nil)
    text:Replace("font","fontTxt")
    return  text.LuaString
end

main()


More information about the JWLua mailing list