I have a suggestion for the Lua API.
I use Lua scripts a lot, but I find it a bit inconvenient.
Call from the Script menu.
If I make the directory structure easy to see in Aseprite, it is troublesome to manage the download source.
If the directory structure is easy for the download source to manage, the provided functions and menu structure in Aseprite conflict.
(And, one script is one menu, the number of files tends to increase.)
Call from the shortcut key.
Unnecessarily, the number of shortcut keys increases.I forget the scripts only used occasionally.
Therefore, I want to support Lua Script Extension and adding menu Lua API.
What does everyone think?
Hi @msk-k, actually I had some plans to register actions in different menu items (even items in the main menu bar but different locations, not only File > Extensions). I’ve found my incomplete patch.
Some notes about your changes:
As in issue 1949, we have plans to register several things from extension scripts (commands, file formats, inks, etc.), so maybe app.register() is too much, or we should need app.registerMenu() or something like app.register{ menu="..." } (my original API use names like app.newCommand and app.newMenu).
There should be a way to unload a extension/script (remove all registered items)
I would prefer other word for "context" (to avoid confusion with keyboard shortcuts “context”), but still not sure.
I think some design work is still needed for the exact API that we want for extension scripts. Something like this might be a possibility:
app.plugin{
load=function() ... end,
unload=function() ... end
}
Or other example:
function init() -- or load() or activate()
end
function exit() -- or unload() or deactivate()
end
I was working on this issue and making some progress with the final API:
This is an example:
function init(plugin)
print('foo init')
if plugin.preferences.count == nil then
plugin.preferences.count = 0
end
plugin:newCommand{
id="fooFirstCommand",
title="Foo First Command",
group="cel_popup",
onclick=function()
plugin.preferences.count = plugin.preferences.count+1
end
}
end
function exit(plugin)
print("foo exit - count=" .. plugin.preferences.count)
end
Both functions init() and exit() will be optional. The "group" attribute should indicate the exact menu where to put the new command (anyway if no group is specified, the command will be available for keyboard shortcuts anyway). (All commands are deleted/unregistered automatically when the extension is disabled/uninstalled or at exit, so generally there will no need to define an exit() function).
you can create a .zip with a package.json file similar to other extensions like palettes but instead of "palettes" section you have to include a scripts section, for example:
The my-script.lua can be something like the given example above. After that you compress both files (package.json and my-script.lua) in a .zip, and rename the .zip to .aseprite-extension. To install the extension just double-clicking just be enough on Windows or macOS.
@msk-k is the parameter the id of the xml property from the gui?
@dacap any chance you could clear up the steps to get something listed under a menu?
In the example it uses “cel_popup” which I don’t see directly listed in gui.xml instead I see things like “cel_popup_edit” would the group for mine be one be “cel_popup_myfirstcommand”?
Another example, if I wanted to put this under the menu bar like under “view” or “edit” how would I do that?
I have the plugin running as I’m seeing the print(“foo init”) in the example but I can’t find my command anywhere in the menus. right now mine looks something like this:
function init(plugin)
print("foo init")
plugin:newCommand{
id="dothething",
text="Do A Thing",
group="help",
onclick=DoTheThing()
}
end
function DoTheThing()
print("Doing a thing!")
end
Finally Even if I can’t find it in the menu, I also don’t see it listed under the keyboard shortcuts, do I need to define something specific for shortcuts?
Anyway thanks for any help anyone can provide! I’m excited to get this working!
You would need to specify the “group” of the menu that exists in gui.xml as the “group” argument to newCommand.
Then your menu will be added underneath the menu you specified.
My code is below.
------------------------------------------------------------------------------
-- Top Menu
------------------------------------------------------------------------------
plugin:newCommand{
id="toLoopExtending",
title="Loop Extending",
group="sprite_crop",
onclick=LoopExtending
}
------------------------------------------------------------------------------
-- Dialogs
------------------------------------------------------------------------------
plugin:newCommand{
id="toEditLayerMetaDataDialogShow",
title="Mask Options",
group="layer_popup_new",
onclick=EditLayerMetaDataDialogShow
}
plugin:newCommand{
id="toEditCelMetaDataDialogShow",
title="Mask Options (Cel)",
group="cel_popup_new",
onclick=EditCelMetaDataDialogShow
}
------------------------------------------------------------------------------
-- Frame Menu
------------------------------------------------------------------------------
plugin:newCommand{
id="toFrameCopyout",
title="new Sprite From frames",
group="frame_popup_reverse",
onclick=FrameCopyout
}
------------------------------------------------------------------------------
-- Layer Menu
------------------------------------------------------------------------------
-- Mask and Link(Src/Dst) Cel Menu
plugin:newCommand{
id="toUpdateAll",
title="Update (Copy & Paste)",
group="layer_popup_new",
onclick=UpdateAll
}
plugin:newCommand{
id="toStoreAll",
title="Store Offset",
group="layer_popup_new",
onclick=StoreAll
}
And one thing that struck me is the way you specified the “onclick” argument.
It looks like the result of the DoTheThing run is specified as “onclick”.
I think the correct way is as follows
function DoTheThing()
print("Doing a thing!")
end
function init(plugin)
print("foo init")
plugin:newCommand{
id="dothething",
text="Do A Thing",
group="help",
onclick=DoTheThing
}
end