Created
October 16, 2021 14:34
-
-
Save dacap/d30f73e5d978dd42ffa34b57e466808d to your computer and use it in GitHub Desktop.
Increments the revision of the file and saves it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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