-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteMetadataToXMP.lua
More file actions
196 lines (156 loc) · 6.48 KB
/
WriteMetadataToXMP.lua
File metadata and controls
196 lines (156 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
--[[============================================================================
WriteMetadataToXMP.lua
Library → Plugin Extras → "Write Custom Metadata to XMP"
For RAW → sidecar .xmp | For JPG/TIFF → embedded in the file
============================================================================--]]
local LrApplication = import "LrApplication"
local LrDialogs = import "LrDialogs"
local LrFunctionContext = import "LrFunctionContext"
local LrPathUtils = import "LrPathUtils"
local LrProgressScope = import "LrProgressScope"
local LrStringUtils = import "LrStringUtils"
local LrTasks = import "LrTasks"
local LrFileUtils = import "LrFileUtils"
local SC = require "SharedConfig"
local Log = require "PluginLogger"
local logger = Log.getLogger("WriteXMP")
--------------------------------------------------------------------------
-- Argfile
--------------------------------------------------------------------------
local function writeArgFile(tmpDir, suffix, photo, targetPath)
local argPath = LrPathUtils.child(tmpDir, "custmeta_w_" .. suffix .. ".txt")
local lines = {}
if WIN_ENV then
table.insert(lines, "-charset")
table.insert(lines, "filename=utf8")
end
local hasData = false
for _, field in ipairs(SC.FIELD_MAP) do
local value = photo:getPropertyForPlugin(SC.PLUGIN_ID, field.id)
if value then
local trimmed = LrStringUtils.trimWhitespace(tostring(value))
if trimmed ~= "" then
hasData = true
table.insert(lines, string.format(
"-XMP-%s:%s=%s", SC.XMP_PREFIX, field.xmpTag, trimmed
))
end
end
end
if not hasData then return nil end
table.insert(lines, "-overwrite_original")
table.insert(lines, targetPath)
local f = io.open(argPath, "wb")
if not f then return nil end
f:write(table.concat(lines, "\n") .. "\n")
f:close()
return argPath
end
--------------------------------------------------------------------------
-- ExifTool
--------------------------------------------------------------------------
local function runExifTool(configPath, argPath, tmpDir, suffix)
local outPath = LrPathUtils.child(tmpDir, "custmeta_out_" .. suffix .. ".txt")
local cmd = string.format(
'exiftool -config "%s" -@ "%s" > "%s" 2>&1',
configPath, argPath, outPath
)
if Log.isEnabled("trace") then
logger:trace("CMD: " .. cmd)
end
local exitCode = LrTasks.execute(cmd)
if Log.isEnabled("trace") then
logger:trace("Exit: " .. tostring(exitCode))
if exitCode ~= 0 then
logger:trace("Output: " .. SC.readFileContents(outPath))
end
end
SC.safeDelete(outPath)
return exitCode == 0
end
--------------------------------------------------------------------------
-- Main logic
--------------------------------------------------------------------------
LrTasks.startAsyncTask(function()
LrFunctionContext.callWithContext("WriteCustomXMP", function(context)
context:addFailureHandler(function(_, msg)
logger:error("FAILURE: " .. tostring(msg))
LrDialogs.showError("Plugin error:\n\n" .. tostring(msg))
end)
local catalog = LrApplication.activeCatalog()
local photos = catalog:getTargetPhotos()
if #photos == 0 then
LrDialogs.message("No photos selected",
"Select photos to which you want to write XMP metadata.", "info")
return
end
local result = LrDialogs.confirm(
"Write custom metadata to XMP?",
string.format(
"This operation applies to %d photos.\n\n"
.. "RAW → sidecar .xmp\nJPG/TIFF → embedded in the file\n\n"
.. "Requires: ExifTool (exiftool.org)", #photos),
"Write", "Cancel"
)
if result ~= "ok" then return end
-- Check ExifTool
local checkCmd = WIN_ENV and "where exiftool >NUL 2>&1"
or "which exiftool >/dev/null 2>&1"
if LrTasks.execute(checkCmd) ~= 0 then
LrDialogs.showError("ExifTool not found in PATH.\n"
.. "Install from https://exiftool.org")
return
end
local tmpDir = LrPathUtils.getStandardFilePath("temp")
local configPath = SC.writeExifToolConfig(tmpDir, "custmeta_w.config")
if not configPath then
LrDialogs.showError("Cannot create configuration file.")
return
end
local progress = LrProgressScope {
title = "Writing XMP metadata...", functionContext = context,
}
local ok, skip, fail = 0, 0, 0
local sidecarN = 0
local failNames, cleanUp = {}, {}
for i, photo in ipairs(photos) do
if progress:isCanceled() then break end
progress:setPortionComplete(i - 1, #photos)
progress:setCaption(string.format("File %d / %d", i, #photos))
local filePath = photo:getRawMetadata("path")
if not filePath or not LrFileUtils.exists(filePath) then
skip = skip + 1
else
local targetPath, isSidecar = SC.getTargetPath(filePath)
if isSidecar then sidecarN = sidecarN + 1 end
local argPath = writeArgFile(tmpDir, tostring(i), photo, targetPath)
if argPath then
table.insert(cleanUp, argPath)
if runExifTool(configPath, argPath, tmpDir, tostring(i)) then
ok = ok + 1
else
fail = fail + 1
table.insert(failNames, LrPathUtils.leafName(filePath))
end
else
skip = skip + 1
end
end
end
progress:done()
for _, p in ipairs(cleanUp) do SC.safeDelete(p) end
SC.safeDelete(configPath)
-- Summary
local msg = string.format(
"Written: %d (sidecar .xmp: %d)\nSkipped: %d\nErrors: %d",
ok, sidecarN, skip, fail)
if #failNames > 0 then
msg = msg .. "\n\nFiles with errors:\n"
for idx = 1, math.min(#failNames, 10) do
msg = msg .. " - " .. failNames[idx] .. "\n"
end
end
LrDialogs.message("XMP write complete", msg,
fail > 0 and "warning" or "info")
end)
end)