export_to_lightgallery.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. local dt = require "darktable"
  2. local du = require "lib/dtutils"
  3. du.check_min_api_version("7.0.0", "lightgallery_export_module")
  4. local gettext = dt.gettext.gettext
  5. local function _(msgid)
  6. return gettext(msgid)
  7. end
  8. local script_data = {}
  9. script_data.metadata = {
  10. name = "lightgallery_export_module",
  11. purpose = _("Export selected files into a directory and call a python script to create a lightGallery"),
  12. author = "Markus Spring",
  13. help = "https://example.com/help"
  14. }
  15. -- local variables
  16. local help_url = "https://example.com/help"
  17. local du = require "lib/dtutils"
  18. local df = require "lib/dtutils.file"
  19. local dtsys = require "lib/dtutils.system"
  20. local debug = require "darktable.debug"
  21. local image_max_xy = 2500
  22. local jpg_quality_str = '92'
  23. local PS = dt.configuration.running_os == "windows" and "\\" or "/"
  24. script_data.destroy = nil -- function to destory the script
  25. script_data.destroy_method = nil -- set to hide for libs since we can't destroy them commpletely yet, otherwise leave as nil
  26. script_data.restart = nil -- how to restart the (lib) script after it's been hidden - i.e. make it visible again
  27. -- Register preferences
  28. dt.preferences.register("lightgallery_export", "export_path", "string", "LightGallery Export Path", "Path to export lightGallery", "")
  29. dt.preferences.register("lightgallery_export", "gallery_title", "string", "LightGallery Title", "Title for the lightGallery", "--..--")
  30. dt.preferences.register("lightgallery_export", "gallery_uplink", "string", "LightGallery Uplink", "Uplink for lightGallery index.html", "..")
  31. dt.preferences.register("lightgallery_export", "gallery_family_tags", "bool", "LightGallery attach published..family tag", "attach published..family tag", false)
  32. dt.preferences.register("lightgallery_export", "exiftoolbinary", "file",
  33. _("lightGallery: executable for exiftool"),
  34. _("select executable for exiftool command line version") , "")
  35. dt.preferences.register("lightgallery_export", "post_export_script", "file",
  36. _("lightGallery: select script to run after image export"),
  37. _("select script to run after image export") , "")
  38. function mws_basename(str)
  39. local name = string.gsub(str, "(.*)%..*", "%1")
  40. return name
  41. end
  42. function exists(name)
  43. if type(name)~="string" then return false end
  44. return os.rename(name,name) and true or false
  45. end
  46. function isFile(name)
  47. if type(name)~="string" then return false end
  48. if not exists(name) then return false end
  49. local f = io.open(name)
  50. if f then
  51. f:close()
  52. return true
  53. end
  54. return false
  55. end
  56. function isDir(name)
  57. return (exists(name) and not isFile(name))
  58. end
  59. local export_path_widget = dt.new_widget("box") {
  60. orientation = "horizontal",
  61. dt.new_widget("label") {
  62. label = "Verzeichnis: "
  63. },
  64. dt.new_widget("file_chooser_button") {
  65. title = "Select export path",
  66. is_directory = true,
  67. tooltip = "Choose the directory where to export the images for the lightGallery",
  68. value = dt.preferences.read("lightgallery_export", "export_path", "string")
  69. }
  70. }
  71. local gallery_title_widget = dt.new_widget("box") {
  72. orientation = "horizontal",
  73. dt.new_widget("label") {
  74. label = "Titel: "
  75. },
  76. dt.new_widget("entry") {
  77. text = dt.preferences.read("lightgallery_export", "gallery_title", "string"),
  78. tooltip = "Enter the title for the lightGallery"
  79. }
  80. }
  81. local gallery_uplink_widget = dt.new_widget("box") {
  82. orientation = "horizontal",
  83. dt.new_widget("label") {
  84. label = "Uplink: "
  85. },
  86. dt.new_widget("entry") {
  87. text = dt.preferences.read("lightgallery_uplink", "gallery_uplink", "string"),
  88. tooltip = "Uplink for the lightGallery index.html"
  89. }
  90. }
  91. local gallery_family_tags_widget = dt.new_widget("check_button"){
  92. label = "Attach photography|...|family Tag",
  93. value = dt.preferences.read("lightgallery_export", "gallery_family_tags", "bool"),
  94. tooltip = "Click to toggle the checkbox",
  95. clicked_callback = function(self)
  96. dt.preferences.write("lightgallery_export", "gallery_family_tags", "bool", self.value)
  97. end
  98. }
  99. -- Function to save preferences
  100. local function save_preferences()
  101. dt.preferences.write("lightgallery_export", "export_path", "string", export_path_widget.value)
  102. dt.preferences.write("lightgallery_export", "gallery_title", "string", gallery_title_widget.text)
  103. dt.preferences.write("lightgallery_export", "lightgallery_uplink", "string", gallery_uplink_widget.text)
  104. end
  105. -- Function to export images to Light Gallery
  106. local function export_to_lightgallery()
  107. local exp_path = export_path_widget.value
  108. local gallery_title = gallery_title_widget.text
  109. local exiftool = dt.preferences.read("lightgallery_export", "exiftoolbinary", "file")
  110. -- Save preferences
  111. save_preferences()
  112. -- Create the export directory if it doesn't exist
  113. os.execute("mkdir -p " .. exp_path)
  114. local jpeg_exporter = dt.new_format("jpeg")
  115. jpeg_exporter.quality = jpg_quality_str
  116. jpeg_exporter.max_height = image_max_xy
  117. jpeg_exporter.max_width = image_max_xy
  118. -- Loop over the selected images
  119. local images = dt.gui.selection()
  120. for _, i in ipairs(images) do
  121. local tmpname = os.tmpname()
  122. local tmp_exported = tmpname..".jpg"
  123. if dt.configuration.running_os == "windows" then
  124. tmp_exported = dt.configuration.tmp_dir .. tmp_exported -- windows os.tmpname() defaults to root directory
  125. end
  126. if i.rating < 2 then
  127. i.rating = 2
  128. end
  129. dt.print("Exporting "..i.filename)
  130. jpeg_exporter:write_image(i, tmp_exported, false)
  131. run_cmd = exiftool..' -TagsFromFile '..df.sanitize_filename(i.sidecar)..' -xmp:all -exif:all --subifd:all -overwrite_original '..df.sanitize_filename(tmp_exported)
  132. dt.print_log(string.format("Running: %s", run_cmd))
  133. resp = dtsys.external_command(run_cmd)
  134. targetfile = df.sanitize_filename(string.gsub(exp_path, "'", "")..PS..mws_basename(i.filename)..'.jpg')
  135. dt.print('mv ' .. tmp_exported .. ' ' .. targetfile)
  136. os.execute('mv ' .. tmp_exported .. ' ' .. targetfile)
  137. if dt.preferences.read("lightgallery_export", "gallery_family_tags", "bool") then
  138. local tagnr = dt.tags.find('photography|published|gallery|family') --- tag must exist!
  139. dt.tags.attach(tagnr,i)
  140. end
  141. os.execute( "rm -f " .. tmpname )
  142. end
  143. local post_export_script = dt.preferences.read("lightgallery_export", "post_export_script", "file")
  144. dt.print("post_export_script: ".. post_export_script.." "..exp_path)
  145. os.execute( post_export_script.." "..exp_path )
  146. -- tell the user that everything worked
  147. dt.print("Exported " .. #images .. " images to lightGallery")
  148. end
  149. -- Create export button
  150. local gallery_export_button = dt.new_widget("button"){
  151. label = "Run Export",
  152. clicked_callback = export_to_lightgallery
  153. }
  154. -- Create a button widget with a help link
  155. local help_button = dt.new_widget("button"){
  156. label = "Help: "..help_url,
  157. clicked_callback = function()
  158. -- Open the help URL in the default web browser
  159. dt.control.execute("xdg-open "..help_url)
  160. end
  161. }
  162. -- Create the module widget
  163. local module_widget = dt.new_widget("box"){
  164. orientation = "vertical",
  165. gallery_title_widget,
  166. export_path_widget,
  167. gallery_uplink_widget,
  168. gallery_family_tags_widget,
  169. gallery_export_button,
  170. help_button
  171. }
  172. -- Register the module
  173. dt.register_lib(
  174. "lightgallery_export_module",
  175. "lightGallery Export",
  176. true,
  177. false,
  178. {
  179. [dt.gui.views.lighttable] = {"DT_UI_CONTAINER_PANEL_RIGHT_CENTER", 100},
  180. },
  181. module_widget
  182. )
  183. script_data.destroy = nil
  184. script_data.destroy_method = nil
  185. return script_data