Extracting Style information programmatically
Posted: Mon Jan 27, 2025 4:26 pm
				
				I am struggleing to find a way to reliably extract the infromation about a style and output it to the lua console. I hoped this would be a simple solution but i keep running into walls, where either i get errored out entirely, or receive no information, or in my latest attempot which i think is close it just reports back nil or no color defined based on my pint statements.
My Lua Console Output:
My Script:
I am hoping to find the simplest most elegant solution to extract style information so i can reference it in my other scripts but so far it seems impossible no matter which approach i take. Primarily I'd like to output the Fill color and Line Color, but ideally it should be able to extract and display all the information about a style included in a MOHO style.
Thoughts Ideas, all would be appreciated!
			My Lua Console Output:
Code: Select all
Style Name: BG_Skin_Tone_DEF
No Fill Color Defined
No Line Color Defined
-----
Style Name: BG_Skin_Acct_DEF
No Fill Color Defined
No Line Color Defined
-----
Style Name: BG_Skin_ToneSec_DEF
No Fill Color Defined
No Line Color Defined
-----
Style Name: BG_Skin_Tone_1
No Fill Color Defined
No Line Color Defined
-----
Style Name: BG_Skin_Acct_1
No Fill Color Defined
No Line Color Defined
-----
Style Name: BG_Skin_ToneSec_1
No Fill Color Defined
No Line Color Defined
-----
Style extraction completed.
My Script:
Code: Select all
ScriptName = "ExtractStylesInfoFromStyles"
ExtractStylesInfoFromStyles = {}
function ExtractStylesInfoFromStyles:Name()
    return "Extract Styles Info"
end
function ExtractStylesInfoFromStyles:Version()
    return "1.2"
end
function ExtractStylesInfoFromStyles:Description()
    return "Extracts and outputs fill and line colors of all styles in the current document."
end
function ExtractStylesInfoFromStyles:UILabel()
    return "Extract Styles Info"
end
function ExtractStylesInfoFromStyles:Run(moho)
    local doc = moho.document
    -- Check if a document is loaded
    if not doc then
        print("No document is loaded.")
        return
    end
    print("Starting style extraction...")
    local styleCount = doc:CountStyles()
    print("Number of styles: " .. styleCount)
    if styleCount == 0 then
        print("No styles found in the document.")
        return
    end
    -- Iterate through all styles
    for i = 0, styleCount - 1 do
        local style = doc:StyleByID(i)
        if style then
            -- Access the style name safely
            local styleName = style.fName:Buffer()
            print("Style Name: " .. styleName)
            -- Check and print the fill color
            if style.fDefineFillCol then
                local fillColor = style.fFillCol
                print(string.format("  Fill Color (RGB): R:%d G:%d B:%d", fillColor.r, fillColor.g, fillColor.b))
                print(string.format("  Fill Color (Hex): #%06X", fillColor.value))
            else
                print("  No Fill Color Defined")
            end
            -- Check and print the line color
            if style.fDefineLineCol then
                local lineColor = style.fLineCol
                print(string.format("  Line Color (RGB): R:%d G:%d B:%d", lineColor.r, lineColor.g, lineColor.b))
                print(string.format("  Line Color (Hex): #%06X", lineColor.value))
            else
                print("  No Line Color Defined")
            end
        else
            print(string.format("Style at index %d could not be accessed.", i))
        end
        print("-----")
    end
    print("Style extraction completed.")
end
function ExtractStylesInfoFromStyles:Init(moho)
    if moho then
        moho:NewScriptMenu(self:Name(), self)
    end
end
local function Main()
    if Moho then
        ExtractStylesInfoFromStyles:Init(Moho)
    end
end
Main()
Thoughts Ideas, all would be appreciated!