From 9b30f6f5e3e4ffad8cd52e39f744443e11c4b807 Mon Sep 17 00:00:00 2001 From: Yannic Meyer Date: Mon, 31 Aug 2026 15:32:50 +0200 Subject: [PATCH 1/3] rename images - always append extension --- contrib/rename_images.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/contrib/rename_images.lua b/contrib/rename_images.lua index 8aeb8b82..0fe52d7a 100644 --- a/contrib/rename_images.lua +++ b/contrib/rename_images.lua @@ -96,6 +96,18 @@ local function stop_job(job) job.valid = false end +-- make sure filename ends with the image's original extension +local function ensure_extension(filename, image) + local original_extension = df.get_filetype(image.filename) + if original_extension ~= "" then + local suffix = "." .. original_extension + if string.lower(string.sub(filename, -string.len(suffix))) ~= string.lower(suffix) then + filename = filename .. suffix + end + end + return filename +end + local function install_module() if not rename.module_installed then dt.register_lib( @@ -154,7 +166,7 @@ local function do_rename(images) if string.len(path) == 0 then path = image.path end - local filename = df.get_filename(new_name) + local filename = ensure_extension(df.get_filename(new_name), image) local filmname = image.path if path ~= image.path then if not df.check_if_file_exists(df.sanitize_filename(path)) then From 1166e234633f493609b759a17d0ca2b7c984a0a1 Mon Sep 17 00:00:00 2001 From: Yannic Meyer Date: Mon, 31 Aug 2026 15:34:13 +0200 Subject: [PATCH 2/3] remove erraneous comment --- contrib/rename_images.lua | 4 ---- 1 file changed, 4 deletions(-) diff --git a/contrib/rename_images.lua b/contrib/rename_images.lua index 0fe52d7a..a9c10cc3 100644 --- a/contrib/rename_images.lua +++ b/contrib/rename_images.lua @@ -20,10 +20,6 @@ --[[ rename - rename an image file or files - This shortcut resets the GPS information to that contained within - the image file. If no GPS info is in the image file, the GPS data - is cleared. - USAGE * require this script from your luarc file or start it from script_manager * select an image or images From 9a689ccc0f2a6e55ed5f2edd60c4d662d27a5bec Mon Sep 17 00:00:00 2001 From: Yannic Meyer Date: Mon, 31 Aug 2026 16:10:13 +0200 Subject: [PATCH 3/3] rename-images: move variable subst. into helper function, add preview button --- contrib/rename_images.lua | 50 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/contrib/rename_images.lua b/contrib/rename_images.lua index a9c10cc3..9f005f75 100644 --- a/contrib/rename_images.lua +++ b/contrib/rename_images.lua @@ -115,6 +115,8 @@ local function install_module() dt.new_widget("box"){ orientation = "vertical", rename.widgets.pattern, + rename.widgets.preview_button, + rename.widgets.preview_label, rename.widgets.button, }, nil, @@ -136,6 +138,16 @@ end -- M A I N -- - - - - - - - - - - - - - - - - - - - - - - - +-- runs variable substitution for one image and returns the result, +-- or -1 if the substitution failed (see ds.substitute_list) +local function substitute_pattern(image, sequence, pattern) + ds.build_substitute_list(image, sequence, pattern, USER, PICTURES, HOME, DESKTOP) + local new_name = ds.substitute_list(pattern) + ds.clear_substitute_list() + return new_name +end + +-- renaming local function do_rename(images) if #images > 0 then local first_image = images[1] @@ -149,14 +161,12 @@ local function do_rename(images) for i, image in ipairs(images) do if job.valid then job.percent = i / #images - ds.build_substitute_list(image, i, pattern, USER, PICTURES, HOME, DESKTOP) - local new_name = ds.substitute_list(pattern) + local new_name = substitute_pattern(image, i, pattern) if new_name == -1 then dt.print(_("unable to do variable substitution, exiting...")) stop_job(job) return end - ds.clear_substitute_list() local args = {} local path = string.sub(df.get_path(new_name), 1, -2) if string.len(path) == 0 then @@ -194,6 +204,28 @@ local function do_rename(images) end +-- preview +local function do_preview(images) + if #images > 0 then + local image = images[1] + local pattern = rename.widgets.pattern.text + if string.len(pattern) > 0 then + local new_name = substitute_pattern(image, 1, pattern) + if new_name == -1 then + rename.widgets.preview_label.label = _("unable to do variable substitution") + else + local filename = ensure_extension(df.get_filename(new_name), image) + rename.widgets.preview_label.label = filename + end + else -- pattern length + rename.widgets.preview_label.label = _("please enter the new name or pattern") + end + else -- image count + rename.widgets.preview_label.label = _("please select an image first") + end +end + + -- - - - - - - - - - - - - - - - - - - - - - - - -- W I D G E T S -- - - - - - - - - - - - - - - - - - - - - - - - @@ -213,6 +245,18 @@ if pattern_pref then rename.widgets.pattern.text = pattern_pref end +rename.widgets.preview_button = dt.new_widget("button"){ + label = _("preview filename"), + clicked_callback = function(this) + do_preview(dt.gui.action_images) + end +} + +rename.widgets.preview_label = dt.new_widget("label"){ + label = "", + selectable = true, +} + rename.widgets.button = dt.new_widget("button"){ label = _("rename"), clicked_callback = function(this)