Can my script save the state of variables between invokations?

I’ve created a script to help me generate trim sheets. It pop up a dialog that lets the user set a few parameters before it runs. Is there a way to save these values so that it remembers the last value used (so the user doesn’t have to keep entering the same numbers each time)?

...

local dlg = Dialog("Trim Sheet Builder")
dlg:number{id="tileWidth", label="Tile Width", text="32"}
dlg:number{id="tileHeight", label="Tile Height", text="32"}
dlg:number{id="marginWidth", label="Margin Width", text="8"}
dlg:number{id="marginHeight", label="Margin Height", text="8"}
dlg:button{id="ok", text="Ok"}
dlg:button{id="cancel", text="Cancel"}
dlg:show()

local data = dlg.data
if data.ok then
    createTrimSheetImage(data.tileWidth, data.tileHeight, data.marginWidth, data.marginHeight)
end

yes. there are two ways.
script as an extension plugin can save presets as plugin.preferences.
sadly, i don’t know anything plugins. @thkwznk could probably tell you more as his sprite analyzer does save presets.
the other possibility is to use lua functionality. aseprite supports lua almost completely, which means you can read and save files. the simplest way would be to store parameters as separate lines in a text file (so you’d have just a single preset) like i have in sessions script and then load the default values from the table.
or you can google and use parser for json or ini files. here’s one for ini: inifile.lua.
to save the settings automatically you’d have to add save function to dialog’s onclose event.

2 Likes

Exactly as @Olga_Galvanova mentioned, packaging your script into a plugin and using plugin.preferences is your best choice in this case.

I’d advise against implementing your own system for saving settings to a file, as I can tell you from experience that it’s very likely not worth the hassle. On top of that, in my opinion, asking a user to give file access to a script is a lot, especially when you don’t really need to do that.

plugin.preferences is very handy to use as long as you stick to basic types - numbers, strings, tables. Any Aseprite-specific types like Point, Rectangle or Color will not be correctly serialized and deserialized and you’ll just lose this data.

plugin.preferences is persisted between sessions which means it will be kept after you reopen Aseprite, if you’d like to just keep these values in memory for a single session instead you can save data in a Lua table instead.

function init(plugin)
    local data = {
        tileWidth = 32,
        tileHeight = 32,
        marginWidth = 8,
        marginHeight = 8
    }

    plugin:newCommand{
        id = "TrimSheetBuilder",
        title = "Trim Sheet Builder",
        group = "cel_popup_properties",
        onclick = function()
            local dlg = Dialog("Trim Sheet Builder")
            dlg:number{
                id = "tileWidth",
                label = "Tile Width",
                text = tostring(data.tileWidth),
                onchange = function()
                    data.tileWidth = dlg.data.tileWidth
                end
            }
            dlg:number{
                id = "tileHeight",
                label = "Tile Height",
                text = tostring(data.tileHeight),
                onchange = function()
                    data.tileHeight = dlg.data.tileHeight
                end
            }
            dlg:number{
                id = "marginWidth",
                label = "Margin Width",
                text = tostring(data.marginWidth),
                onchange = function()
                    data.marginWidth = dlg.data.marginWidth
                end
            }
            dlg:number{
                id = "marginHeight",
                label = "Margin Height",
                text = tostring(data.marginHeight),
                onchange = function()
                    data.marginHeight = dlg.data.marginHeight
                end
            }
            dlg:button{id = "ok", text = "Ok"}
            dlg:button{id = "cancel", text = "Cancel"}
            dlg:show()
        end
    }
end

function exit(plugin)
    -- You don't really need to do anything specific here
end

4 Likes