Question
Trying to write a plugin
I'm trying to write a plugin that will take the Geolocation information (city, state, country) and write those as keywords.
The plugin is below. When I ran the plugin i got an error : "An internal error has occurred: Unknown key: "City" Thoughts, ideas?
--------------------------------------------------------
local LrDialogs = import 'LrDialogs'
local LrTasks = import 'LrTasks'
local LrApplication = import 'LrApplication'
local LrProgressScope = import 'LrProgressScope'
LrTasks.startAsyncTask(function()
local catalog = LrApplication.activeCatalog()
local photos = catalog:getTargetPhotos()
if #photos == 0 then
LrDialogs.message("No photos selected", "Please select one or more photos.", "info")
return
end
local progress = LrProgressScope {
title = "Copying location metadata to keywords...",
}
catalog:withWriteAccessDo("Add Geo Keywords", function(context)
for i, photo in ipairs(photos) do
progress:setPortionComplete(i, #photos)
progress:setCaption("Processing photo " .. i .. " of " .. #photos)
local city = photo:getRawMetadata("City") or ""
local state = photo:getRawMetadata("State") or ""
local country = photo:getRawMetadata("Country") or ""
local newKeywords = {}
for _, value in ipairs({ City, State, Country }) do
value = value:match("^%s*(.-)%s*$") -- trim whitespace
if value ~= "" then
table.insert(newKeywords, value)
end
end
if #newKeywords > 0 then
local existingKeywords = photo:getRawMetadata("keywords") or {}
local keywordSet = {}
for _, k in ipairs(existingKeywords) do
keywordSet[k:getName()] = true
end
for _, kw in ipairs(newKeywords) do
if not keywordSet[kw] then
local keyword = catalog:createKeyword(kw, {}, true, nil)
photo:addKeyword(keyword)
end
end
end
if progress:isCanceled() then break end
end
end)
progress:done()
LrDialogs.message("Done", "Location data copied to keywords.", "info")
end)