local dt = require "darktable" local du = require "lib/dtutils" du.check_min_api_version("7.0.0", "lightgallery_export_module") local gettext = dt.gettext.gettext local function _(msgid) return gettext(msgid) end local script_data = {} script_data.metadata = { name = "lightgallery_export_module", purpose = _("Export selected files into a directory and call a python script to create a lightGallery"), author = "Markus Spring", help = "https://gogs.hermes.markus-spring.info/springm/darktable_to_lightgallery" } -- local variables local help_url = "https://gogs.hermes.markus-spring.info/springm/darktable_to_lightgallery" local du = require "lib/dtutils" local df = require "lib/dtutils.file" local dtsys = require "lib/dtutils.system" local debug = require "darktable.debug" local image_max_xy = 2500 local jpg_quality_str = '92' local PS = dt.configuration.running_os == "windows" and "\\" or "/" script_data.destroy = nil -- function to destory the script script_data.destroy_method = nil -- set to hide for libs since we can't destroy them commpletely yet, otherwise leave as nil script_data.restart = nil -- how to restart the (lib) script after it's been hidden - i.e. make it visible again -- Register preferences dt.preferences.register("lightgallery_export", "export_path", "string", "LightGallery Export Path", "Path to export lightGallery", "") dt.preferences.register("lightgallery_export", "gallery_title", "string", "LightGallery Title", "Title for the lightGallery", "--..--") dt.preferences.register("lightgallery_export", "gallery_uplink", "string", "LightGallery Uplink", "Uplink for lightGallery index.html", "..") -- dt.preferences.register("lightgallery_export", "gallery_family_tags", "bool", "LightGallery attach published..family tag", "attach published..family tag", false) dt.preferences.register("lightgallery_export", "gallery_tag", "string", "LightGallery attach published|gallery| tag", "attach published|gallery| tag", "family") dt.preferences.register("lightgallery_export", "exiftoolbinary", "file", _("lightGallery: executable for exiftool"), _("select executable for exiftool command line version") , "") dt.preferences.register("lightgallery_export", "post_export_script", "file", _("lightGallery: select script to run after image export"), _("select script to run after image export") , "") function mws_basename(str) local name = string.gsub(str, "(.*)%..*", "%1") return name end function exists(name) if type(name)~="string" then return false end return os.rename(name,name) and true or false end function isFile(name) if type(name)~="string" then return false end if not exists(name) then return false end local f = io.open(name) if f then f:close() return true end return false end function isDir(name) return (exists(name) and not isFile(name)) end local export_path_widget = dt.new_widget("file_chooser_button") { title = "Select export path", is_directory = true, tooltip = "Choose the directory where to export the images for the lightGallery", value = dt.preferences.read("lightgallery_export", "export_path", "string") } local export_path_widget_box = dt.new_widget("box") { orientation = "horizontal", dt.new_widget("label") { label = "Verzeichnis: " }, export_path_widget } local gallery_title_widget = dt.new_widget("entry") { text = dt.preferences.read("lightgallery_export", "gallery_title", "string"), tooltip = "Enter the title for the lightGallery" } local gallery_title_widget_box = dt.new_widget("box") { orientation = "horizontal", dt.new_widget("label") { label = "Titel: " }, gallery_title_widget } local gallery_uplink_widget = dt.new_widget("entry") { text = dt.preferences.read("lightgallery_uplink", "gallery_uplink", "string"), tooltip = "Uplink for the lightGallery index.html" } local gallery_uplink_widget_box = dt.new_widget("box") { orientation = "horizontal", dt.new_widget("label") { label = "Uplink: " }, gallery_uplink_widget } local gallery_tag_widget = dt.new_widget("entry") { text = dt.preferences.read("lightgallery_export", "gallery_tag", "string") } local gallery_tag_widget_box = dt.new_widget("box") { orientation = "horizontal", dt.new_widget("label") { label = "photography|published|gallery Tag: " }, gallery_tag_widget } -- Function to save preferences local function save_preferences() dt.preferences.write("lightgallery_export", "export_path", "string", export_path_widget.value) dt.preferences.write("lightgallery_export", "gallery_title", "string", gallery_title_widget.text) dt.preferences.write("lightgallery_export", "lightgallery_uplink", "string", gallery_uplink_widget.text) dt.preferences.write("lightgallery_export", "gallery_tag", "string", gallery_tag_widget.text) end -- Function to check if a string is not empty local function is_not_empty(s) return s ~= "" and s ~= nil end local function run_post_export_script(exp_path) title = dt.preferences.read("lightgallery_export", "gallery_title", "string") if title == "" then title = "n.n" end uplink = dt.preferences.read("lightgallery_export", "lightgallery_uplink", "string") if uplink == "" then uplink = ".." end local post_export_script = dt.preferences.read("lightgallery_export", "post_export_script", "file") dt.print_log( "post_export_script: ".. post_export_script.." -t '"..title.."' -u '"..uplink.."' "..exp_path ) os.execute( post_export_script.." -t '"..title.."' -u '"..uplink.."' "..exp_path ) end -- Function to export images to Light Gallery local function export_to_lightgallery() local exp_path = export_path_widget.value local gallery_title = gallery_title_widget.text local exiftool = dt.preferences.read("lightgallery_export", "exiftoolbinary", "file") -- Save preferences save_preferences() -- Create the export directory if it doesn't exist os.execute("mkdir -p " .. exp_path) local jpeg_exporter = dt.new_format("jpeg") jpeg_exporter.quality = jpg_quality_str jpeg_exporter.max_height = image_max_xy jpeg_exporter.max_width = image_max_xy -- Loop over the selected images local images = dt.gui.selection() for _, i in ipairs(images) do local tmpname = os.tmpname() local tmp_exported = tmpname..".jpg" if dt.configuration.running_os == "windows" then tmp_exported = dt.configuration.tmp_dir .. tmp_exported -- windows os.tmpname() defaults to root directory end if i.rating < 2 then i.rating = 2 end dt.print("Exporting "..i.filename) jpeg_exporter:write_image(i, tmp_exported, false) run_cmd = exiftool..' -TagsFromFile '..df.sanitize_filename(i.sidecar)..' -xmp:all -exif:all --subifd:all -overwrite_original '..df.sanitize_filename(tmp_exported) dt.print_log(string.format("Running: %s", run_cmd)) resp = dtsys.external_command(run_cmd) targetfile = df.sanitize_filename(string.gsub(exp_path, "'", "")..PS..mws_basename(i.filename)..'.jpg') dt.print('mv ' .. tmp_exported .. ' ' .. targetfile) os.execute('mv ' .. tmp_exported .. ' ' .. targetfile) tag = dt.preferences.read("lightgallery_export", "gallery_tag", "string") if is_not_empty(tag) then tag = 'photography|published|gallery|'..tag tnr = dt.tags.find(tag) if not tnr then tnr = dt.tags.create(tag) end dt.tags.attach(tnr,i) end os.execute( "rm -f " .. tmpname ) end run_post_export_script(exp_path) dt.print("Exported " .. #images .. " images to lightGallery") end -- Create export button local gallery_export_button = dt.new_widget("button"){ label = "Run Export", clicked_callback = export_to_lightgallery } -- Create a button widget with a help link local help_button = dt.new_widget("button"){ label = "Help/Documentation", clicked_callback = function() -- Open the help URL in the default web browser dt.control.execute("xdg-open "..help_url) end } -- Create the module widget local module_widget = dt.new_widget("box"){ orientation = "vertical", gallery_title_widget_box, export_path_widget_box, gallery_uplink_widget_box, gallery_tag_widget_box, gallery_export_button, help_button } -- Register the module dt.register_lib( "lightgallery_export_module", "lightGallery Export", true, false, { [dt.gui.views.lighttable] = {"DT_UI_CONTAINER_PANEL_RIGHT_CENTER", 100}, }, module_widget ) script_data.destroy = nil script_data.destroy_method = nil return script_data