|
53 | 53 |
|
54 | 54 | local script_data = {}
|
55 | 55 |
|
| 56 | +local have_not_printed_config_message = true |
| 57 | + |
56 | 58 | script_data.metadata = {
|
57 | 59 | name = _("auto style"),
|
58 | 60 | purpose = _("automatically apply a style based on image EXIF tag"),
|
@@ -101,56 +103,62 @@ end
|
101 | 103 | local function autostyle_apply_one_image (image)
|
102 | 104 |
|
103 | 105 | local pref = darktable.preferences.read("autostyle", "exif_tag", "string")
|
104 |
| - -- We need the tag, the value and the style_name provided from the configuration string |
105 |
| - local tag, value, style_name = string.match(pref, "(%g+)%s*=%s*(%g+)%s*=>%s*(%g+)") |
106 | 106 |
|
107 |
| - -- check they all exist (correct syntax) |
108 |
| - if (not tag) then |
109 |
| - darktable.print(string.format(_("EXIF tag not found in %s"), pref)) |
110 |
| - return 0 |
111 |
| - end |
112 |
| - if (not value) then |
113 |
| - darktable.print(string.format(_("value to match not found in %s"), pref)) |
114 |
| - return 0 |
115 |
| - end |
116 |
| - if (not style_name) then |
117 |
| - darktable.print(string.format(_("style name not found in %s"), pref)) |
118 |
| - return 0 |
119 |
| - end |
120 |
| - if not filelib.check_if_bin_exists("exiftool") then |
121 |
| - darktable.print(_("can't find exiftool")) |
122 |
| - return 0 |
123 |
| - end |
124 |
| - |
125 |
| - |
126 |
| - -- First find the style (we have its name) |
127 |
| - local styles = darktable.styles |
128 |
| - local style |
129 |
| - for _, s in ipairs(styles) do |
130 |
| - if s.name == style_name then |
131 |
| - style = s |
132 |
| - end |
133 |
| - end |
134 |
| - if (not style) then |
135 |
| - darktable.print(string.format(_("style not found for autostyle: %s"), style_name)) |
136 |
| - return 0 |
137 |
| - end |
138 |
| - |
139 |
| - -- Apply the style to image, if it is tagged |
140 |
| - local ok, auto_dr_attr = pcall(exiftool_attribute, image.path .. '/' .. image.filename,tag) |
141 |
| - --darktable.print_error("dr_attr:" .. auto_dr_attr) |
142 |
| - -- If the lookup fails, stop here |
143 |
| - if (not ok) then |
144 |
| - darktable.print(string.format(_("couldn't get attribute %s from exiftool's output"), auto_dr_attr)) |
145 |
| - return 0 |
146 |
| - end |
147 |
| - if auto_dr_attr == value then |
148 |
| - darktable.print_log("Image " .. image.filename .. ": autostyle automatically applied " .. pref) |
149 |
| - darktable.styles.apply(style,image) |
150 |
| - return 1 |
151 |
| - else |
152 |
| - darktable.print_log("Image " .. image.filename .. ": autostyle not applied, exif tag " .. pref .. " not matched: " .. auto_dr_attr) |
153 |
| - return 0 |
| 107 | + if pref and string.len(pref) >= 6 then |
| 108 | + -- We need the tag, the value and the style_name provided from the configuration string |
| 109 | + local tag, value, style_name = string.match(pref, "(%g+)%s*=%s*(%g+)%s*=>%s*(%g+)") |
| 110 | + |
| 111 | + -- check they all exist (correct syntax) |
| 112 | + if (not tag) then |
| 113 | + darktable.print(string.format(_("EXIF tag not found in %s"), pref)) |
| 114 | + return 0 |
| 115 | + end |
| 116 | + if (not value) then |
| 117 | + darktable.print(string.format(_("value to match not found in %s"), pref)) |
| 118 | + return 0 |
| 119 | + end |
| 120 | + if (not style_name) then |
| 121 | + darktable.print(string.format(_("style name not found in %s"), pref)) |
| 122 | + return 0 |
| 123 | + end |
| 124 | + if not filelib.check_if_bin_exists("exiftool") then |
| 125 | + darktable.print(_("can't find exiftool")) |
| 126 | + return 0 |
| 127 | + end |
| 128 | + |
| 129 | + |
| 130 | + -- First find the style (we have its name) |
| 131 | + local styles = darktable.styles |
| 132 | + local style |
| 133 | + for _, s in ipairs(styles) do |
| 134 | + if s.name == style_name then |
| 135 | + style = s |
| 136 | + end |
| 137 | + end |
| 138 | + if (not style) then |
| 139 | + darktable.print(string.format(_("style not found for autostyle: %s"), style_name)) |
| 140 | + return 0 |
| 141 | + end |
| 142 | + |
| 143 | + -- Apply the style to image, if it is tagged |
| 144 | + local ok, auto_dr_attr = pcall(exiftool_attribute, image.path .. '/' .. image.filename,tag) |
| 145 | + --darktable.print_error("dr_attr:" .. auto_dr_attr) |
| 146 | + -- If the lookup fails, stop here |
| 147 | + if (not ok) then |
| 148 | + darktable.print(string.format(_("couldn't get attribute %s from exiftool's output"), auto_dr_attr)) |
| 149 | + return 0 |
| 150 | + end |
| 151 | + if auto_dr_attr == value then |
| 152 | + darktable.print_log("Image " .. image.filename .. ": autostyle automatically applied " .. pref) |
| 153 | + darktable.styles.apply(style,image) |
| 154 | + return 1 |
| 155 | + else |
| 156 | + darktable.print_log("Image " .. image.filename .. ": autostyle not applied, exif tag " .. pref .. " not matched: " .. auto_dr_attr) |
| 157 | + return 0 |
| 158 | + end |
| 159 | + elseif have_not_printed_config_message then |
| 160 | + have_not_printed_config_message = false |
| 161 | + darktable.print(string.format(_("%s is not configured, please configure the preference in Lua options"), script_data.metadata.name)) |
154 | 162 | end
|
155 | 163 | end
|
156 | 164 |
|
|
180 | 188 | darktable.register_event("autostyle", "shortcut", autostyle_apply,
|
181 | 189 | _("apply your chosen style from exiftool tags"))
|
182 | 190 |
|
183 |
| -darktable.preferences.register("autostyle", "exif_tag", "string", "Autostyle: EXIF_tag=value=>style", _("apply a style automatically if an EXIF tag matches value, find the tag with exiftool"), "") |
| 191 | +darktable.preferences.register("autostyle", "exif_tag", "string", |
| 192 | + string.format("%s: EXIF_tag=value=>style", script_data.metadata.name), |
| 193 | + _("apply a style automatically if an EXIF tag matches value, find the tag with exiftool"), "") |
184 | 194 |
|
185 | 195 | darktable.register_event("autostyle", "post-import-image",
|
186 | 196 | autostyle_apply_one_image_event)
|
|
0 commit comments