-- @description Script that provides incremental saving capability. Every time the script is called, the project will be saved with a new (unused) file name that counts up. E.g. my_project_0001.ocs, my_project_0002.ocs, etc. -- @author me :) -- @shortcut alt + s -- @version 0.1 local function showError(text) octane.gui.showDialog{ type = octane.gui.dialogType.BUTTON_DIALOG, title = "Error", text = text } end -- currently we don't support incremental saving for packages if octane.project.loadedFromPackage() then showError("Incremental saving for packages is currently not supported.") return end -- get current project file name local projectPath = octane.project.getCurrentProject() -- if we don't have a file name yet, the project hasn't been saved yet: if not projectPath or (projectPath == "") then -- let user choose a file name for the project local newPath = octane.gui.showDialog{ type = octane.gui.dialogType.FILE_DIALOG, browseDirectory = false, path = octane.project.getPreferences():getAttribute(octane.A_LAST_PROJECT_DIRECTORY), save = true, title = "Please specify the project file name", wildcards = "*.ocs" } projectPath = newPath.result -- if no path is returned, the user cancelled the dialog -> stop script if projectPath == "" then return end end -- split path into directory, file name without exension local projectDir = octane.file.getParentDirectory(projectPath) local projectFile = octane.file.getFileNameWithoutExtension(projectPath) -- if the directory of the file path doesn't exist, create it if not octane.file.isDirectory(projectDir) then if not octane.file.createDirectory(projectDir) then showError(string.format("Can't save project, because directory '%s' could not be created.")) return end end -- if the file name doesn't end with a number, append "_0001" if not projectFile:find("%d+$") then projectFile = projectFile .. "_0001" end -- loop until we find an unused file name while true do -- if we found an unused file name, save the project and return projectPath = octane.file.join(projectDir, projectFile .. ".ocs") if not octane.file.exists(projectPath) then if not octane.project.saveAs(projectPath) then showError(string.format("Found unused file name '%s', but can't save project into it.")) end return end -- ok increment the number in the file name and then try again. local ix = projectFile:len() while true do -- if the current character index is 0, the file name consists only of digits and we have -- the maximum possible number. add a "1" in front of it and break. if ix < 1 then projectFile = "1" .. projectFile break end -- get digit at character position ix local digit = tonumber(projectFile:sub(ix, ix)) -- if digit is not a digit, insert a "1" and break if not digit then projectFile = projectFile:sub(1, ix) .. "1" .. projectFile:sub(ix + 1) break end -- increment the digit and if it's still less than 10, just replace the old digit with the -- incremented one digit = digit + 1 if digit < 10 then projectFile = projectFile:sub(1, ix-1) .. tostring(digit) .. projectFile:sub(ix + 1) break -- if we have an overflow, replace the old digit with "0" and continue with next left digit else projectFile = projectFile:sub(1, ix-1) .. "0" .. projectFile:sub(ix + 1) end ix = ix - 1 end end