Skip to content

Instantly share code, notes, and snippets.

@dacap
Created October 16, 2021 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacap/d30f73e5d978dd42ffa34b57e466808d to your computer and use it in GitHub Desktop.
Save dacap/d30f73e5d978dd42ffa34b57e466808d to your computer and use it in GitHub Desktop.
Increments the revision of the file and saves it
-- E.g. if the filename is called "spriteR1.png", it will rename it to "spriteR2.png" and save it.
local spr = app.activeSprite
if not spr then return app.alert "No active sprite" end
local pt = app.fs.filePathAndTitle(spr.filename)
local ext = app.fs.fileExtension(spr.filename)
-- New filename replacing the revision number at the end with its
-- incremented version
local fn = string.gsub(
pt, "%d+",
function(s)
local r = string.format("%d",
tonumber(s)+1) -- Increment revisiono
-- As string.format() doesn't "%*d" we have to add the '0's
while string.len(r) < string.len(s) do
r = "0" .. r
end
return r
end) .. "." .. ext
-- Save with the new filename
if not app.fs.isFile(fn) then
app.command.SaveFileAs{ filename=fn }
else
app.alert("File " .. fn .. " already exists. We don't want to overwrite it.")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment