How can I save a sprite from another tab?

Hello, dear community! I hope you will help me with my problem.
In general, I want to make a script that saves layers with cropped alpha.
So, in my plan, the script should go through the layers → select them → move to another tab (with fit sprite size) → save the file from this new tab.

I made a script like this

local sprite = app.activeSprite
local folder_path, file_name = sprite.filename:match("^(.+[/\\])(.-).([^.]*)$")

local function iterate_layers(_input_layer)
	for i, layer in ipairs(_input_layer.layers) do
			local new_sprite = app.command.NewSpriteFromSelection()
			app.command.SaveFile {
				filename=folder_path .. "export/" .. .. ".png",

And it almost works, but every time it saves only 1 layer. Please tell me where I made a mistake

hi, again ;]
it seems like the script is incomplete, but assuming you call iterate_layers(_input_layer) later, then i think following happens:

  • if _input_layer is regular layer, nothing happens, because it doesn’t contain a stack of layers to loop through. only sprites and groups can have .layers.
  • if it is group, then it doesn’t have a cel with image (content) to create a selection from it. which shouldn’t matter in this case, however same happens if one of the layers is empty. or if one of layers is another, nested group.
  • and if there is no selection, then aseprite can’t create a file from it.
  • and finally, once aseprite didn’t create a new file, it saves the active file as png and then closes it. which probably will be the original file you actually want to keep open.

there are also other issues and possibly some more i don’t see.

anyway, i got you this: Export cropped layers
it’s nothing fancy, but it somewhat works.
the script may look long, but it really are just three blocks: setup with variables, main functionality and dialog window. the most important thing is happening again in familiar iterate_layers function, so if you want to experiment with it, start there.
hope it helps.

1 Like

Wow, thank you, Olga! I’ve gotten a lot of new information! For example, I didn’t know that I could get a selection from a cell! I’ve tried to find selection options from a level. Thank you for your answers, it helps a lot. Also, I will check your script. I want to make my own because I want to learn Lua and use it in the future. But I like to look at other people’s scripts and find interesting solutions in them

1 Like