| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- 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://example.com/help"
- }
- -- local variables
- local help_url = "https://example.com/help"
- 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", "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("box") {
- orientation = "horizontal",
- dt.new_widget("label") {
- label = "Verzeichnis: "
- },
- 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 gallery_title_widget = dt.new_widget("box") {
- orientation = "horizontal",
- dt.new_widget("label") {
- label = "Titel: "
- },
- dt.new_widget("entry") {
- text = dt.preferences.read("lightgallery_export", "gallery_title", "string"),
- tooltip = "Enter the title for the lightGallery"
- }
- }
- local gallery_uplink_widget = dt.new_widget("box") {
- orientation = "horizontal",
- dt.new_widget("label") {
- label = "Uplink: "
- },
- dt.new_widget("entry") {
- text = dt.preferences.read("lightgallery_uplink", "gallery_uplink", "string"),
- tooltip = "Uplink for the lightGallery index.html"
- }
- }
- local gallery_family_tags_widget = dt.new_widget("check_button"){
- label = "Attach photography|...|family Tag",
- value = dt.preferences.read("lightgallery_export", "gallery_family_tags", "bool"),
- tooltip = "Click to toggle the checkbox",
- clicked_callback = function(self)
- dt.preferences.write("lightgallery_export", "gallery_family_tags", "bool", self.value)
- end
- }
- -- 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)
- 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)
- if dt.preferences.read("lightgallery_export", "gallery_family_tags", "bool") then
- local tagnr = dt.tags.find('photography|published|gallery|family') --- tag must exist!
- dt.tags.attach(tagnr,i)
- end
- os.execute( "rm -f " .. tmpname )
- end
- local post_export_script = dt.preferences.read("lightgallery_export", "post_export_script", "file")
- dt.print("post_export_script: ".. post_export_script.." "..exp_path)
- os.execute( post_export_script.." "..exp_path )
- -- tell the user that everything worked
- 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: "..help_url,
- 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,
- export_path_widget,
- gallery_uplink_widget,
- gallery_family_tags_widget,
- 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
|