-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNDHLoadLatLong.lua
128 lines (104 loc) · 3.82 KB
/
NDHLoadLatLong.lua
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
--[[-------------------------------------------------------------------------
NDHLoadCaptions.lua
Exploring LRSDK
---------------------------------------------------------------------------]]
-- Set the names of root keyword and root collection set - can be edited to taste
local LrApplication = import 'LrApplication'
local LrFunctionContext = import 'LrFunctionContext'
local LrBinding = import 'LrBinding'
local LrDialogs = import 'LrDialogs'
local LrView = import 'LrView'
local LrLogger = import 'LrLogger'
local LrTasks = import 'LrTasks'
-- Set up the logger
local logger = LrLogger('NDH')
logger:enable("print") -- set to "logfile" to write to ~/Documents/lrClassicLogs/NDH.log
local log = logger:quickf('info')
local function csvsplit (inputstr)
local t={}
local i = 20;
local field
while (inputstr and inputstr:len() > 0 and i > 0) do
if (inputstr:sub(1, 1) == "\"") then
field, inputstr = inputstr:match("\"([^\"]*)\",?(.*)")
table.insert(t, field)
else
field, inputstr = inputstr:match("([^,]*),?(.*)")
table.insert(t, field)
end
i = i - 1
end
return t
end
local captionsfile = "/Users/neilhunt/DriveNeil/0 Personal Folders/Lightroom/2023 CSV for Captions - Export.csv"
--[[
This is the entry point function that's called when the Lightroom menu item is selected
]]
local function main ()
log(" Starting")
-- LrDialogs.message("NDH", "Starting LoadCaptions")
local catalog = LrApplication.activeCatalog()
local photos = catalog:getTargetPhotos()
if (photos == nil) then
log("No selected photos")
LrDialogs.message("NDH", "Usage: Select one or more photos to adjust")
return
end
-- Load Captions File
local captionstable = { }
local line
local first = true
local countlines = 0
local countcaptions = 0
for line in io.lines(captionsfile) do
countlines = countlines + 1
line = line:gsub("%c$", "") -- chomp
if (first) then
if (line:find("Filename,New Caption") ~= 1) then
LrDialogs.message("NDH Set Captions", "File does not appear to have the right CSV")
return
end
first = false
else
local t = csvsplit(line)
local filename = t[1]
local newcaption = t[2]
if (newcaption and newcaption:len() > 3) then
captionstable[filename] = newcaption
countcaptions = countcaptions + 1
end
end
end
LrDialogs.message("NDH", string.format("Read %d lines %d captions", countlines, countcaptions))
-- Iterate over all selected photos...
-- NOTE: iteration seems to iterate by current sort order, nothing to do with selection order.
local countphotos = 0
local countmodified = 0
local countunchanged = 0
for j, p in ipairs(photos) do
countphotos = countphotos + 1
local filename = p:getFormattedMetadata('fileName')
local path = p:getRawMetadata('path')
if (captionstable[path]) then
local caption = captionstable[path]
-- LrDialogs.message("NDH", string.format("Found file %s caption = %s (%d)", path, caption, caption:len()))
local oldcaption = p:getFormattedMetadata("caption")
if (oldcaption and oldcaption ~= "" and oldcaption ~= caption) then
-- LrDialogs.message("NDH", string.format("For %s: BLOCK changing %s (%d) to %s (%d)", path, oldcaption, oldcaption:len(), caption, caption:len()))
countunchanged = countunchanged + 1
else
catalog:withWriteAccessDo("Create keyword", function()
p:setRawMetadata("caption", caption)
end)
-- LrDialogs.message("NDH", string.format("For %s: Added caption %s", path, caption))
countmodified = countmodified + 1
end
end
end
-- LrDialogs.resetDoNotShowFlag()
LrDialogs.message(string.format("Processed %d photos: %d added captions, %d unchanged",
countphotos, countmodified, countunchanged))
return
end
-- Run main()
LrTasks.startAsyncTask(main)