|
| 1 | +--[[ |
| 2 | +Rename tags |
| 3 | +
|
| 4 | +AUTHOR |
| 5 | +Sebastian Witt ([email protected]) |
| 6 | +
|
| 7 | +INSTALLATION |
| 8 | +* copy this file in $CONFIGDIR/lua/ where CONFIGDIR |
| 9 | +is your darktable configuration directory |
| 10 | +* add the following line in the file $CONFIGDIR/luarc |
| 11 | + require "rename-tags" |
| 12 | +
|
| 13 | +USAGE |
| 14 | +* In lighttable there is a new entry: 'rename tag' |
| 15 | +* Enter old tag (this one gets deleted!) |
| 16 | +* Enter new tag name |
| 17 | +
|
| 18 | +LICENSE |
| 19 | +GPLv2 |
| 20 | +
|
| 21 | +]] |
| 22 | + |
| 23 | +local darktable = require "darktable" |
| 24 | +local debug = require "darktable.debug" |
| 25 | + |
| 26 | +-- GUI entries |
| 27 | +local old_tag = darktable.new_widget("entry") { tooltip = "Enter old tag" } |
| 28 | +local new_tag = darktable.new_widget("entry") { tooltip = "Enter new tag" } |
| 29 | + |
| 30 | +-- This function does the renaming |
| 31 | +local function rename_tags(widget) |
| 32 | + -- If entries are empty, return |
| 33 | + if old_tag.text == '' then |
| 34 | + darktable.print ("Old tag can't be empty") |
| 35 | + return |
| 36 | + end |
| 37 | + if new_tag.text == '' then |
| 38 | + darktable.print ("New tag can't be empty") |
| 39 | + return |
| 40 | + end |
| 41 | + |
| 42 | + local Count = 0 |
| 43 | + |
| 44 | + -- Check if old tag exists |
| 45 | + local ot = darktable.tags.find (old_tag.text) |
| 46 | + |
| 47 | + if not ot then |
| 48 | + darktable.print ("Old tag does not exist") |
| 49 | + return |
| 50 | + end |
| 51 | + |
| 52 | + -- Show job |
| 53 | + local job = darktable.gui.create_job ("Renaming tag", true) |
| 54 | + |
| 55 | + old_tag.editable = false |
| 56 | + new_tag.editable = false |
| 57 | + |
| 58 | + -- Create if it does not exists |
| 59 | + local nt = darktable.tags.create (new_tag.text) |
| 60 | + |
| 61 | + -- Search images for old tag |
| 62 | + dbcount = #darktable.database |
| 63 | + for i,image in ipairs(darktable.database) do |
| 64 | + -- Update progress bar |
| 65 | + job.percent = i / dbcount |
| 66 | + |
| 67 | + local tags = image:get_tags () |
| 68 | + for _,t in ipairs (tags) do |
| 69 | + if t.name == old_tag.text then |
| 70 | + -- Found it, attach new tag |
| 71 | + image:attach_tag (nt) |
| 72 | + Count = Count + 1 |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + -- Delete old tag, this removes it from all images |
| 78 | + darktable.tags.delete (ot) |
| 79 | + |
| 80 | + job.valid = false |
| 81 | + darktable.print ("Renamed tags for " .. Count .. " images") |
| 82 | + old_tag.editable = true |
| 83 | + new_tag.editable = true |
| 84 | +end |
| 85 | + |
| 86 | +-- GUI |
| 87 | +local old_widget = darktable.new_widget ("box") { |
| 88 | + orientation = "horizontal", |
| 89 | + darktable.new_widget("label") { label = "Old tag" }, |
| 90 | + old_tag |
| 91 | +} |
| 92 | + |
| 93 | +local new_widget = darktable.new_widget ("box") { |
| 94 | + orientation = "horizontal", |
| 95 | + darktable.new_widget("label") { label = "New tag" }, |
| 96 | + new_tag |
| 97 | +} |
| 98 | + |
| 99 | +local function rename_reset(widget) |
| 100 | + old_tag.text = '' |
| 101 | + new_tag.text = '' |
| 102 | +end |
| 103 | + |
| 104 | +local rename_widget = darktable.new_widget ("box") { |
| 105 | + orientation = "vertical", |
| 106 | + reset_callback = rename_reset, |
| 107 | + old_widget, |
| 108 | + new_widget, |
| 109 | + darktable.new_widget("button") { label = "Go", clicked_callback = rename_tags } |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +darktable.register_lib ("rename_tags", "rename tag", true, true, {[darktable.gui.views.lighttable] = {"DT_UI_CONTAINER_PANEL_RIGHT_CENTER", 20},}, rename_widget, nil, nil) |
| 114 | + |
0 commit comments