Debuging a plugin for lightroom 6.4
local LrDialogs = import 'LrDialogs'
local LrTasks = import 'LrTasks'
local LrApplication = import 'LrApplication'
local function getCollectionPath(collection)
local path = {}
local current = collection
while current do
table.insert(path, 1, current:getName())
current = current:getParent()
end
return path
end
local function createKeywordFromPath(path)
if #path==0 then
return ""
elseif #path==1 then
return path[1] -- just collection name
else
local keyword = ""
for i = 1, #path do
if i == 1 then
keyword = path[i]
else
keyword = keyword .. "|" .. path[i]
end
end
return keyword
end
end
local function processCollections(collections, progressScope)
for _, collection in ipairs(collections) do
if progressScope:isCanceled() then
return
end
if collection:type() == "LrCollectionSet" then
local childCollections = collection:getChildCollections()
local childCollectionSets = collection:getChildCollectionSets()
processCollections(childCollections, progressScope)
processCollections(childCollectionSets, progressScope)
else
progressScope:setCaption("Processing: " .. collection:getName())
if not collection:isSmartCollection() then
local photos = collection:getPhotos()
local collectionPath = getCollectionPath(collection)
local keyword = createKeywordFromPath(collectionPath)
if keyword ~= "" and #photos > 0 then
progressScope:setCaption("Adding keyword'" .. keyword .. "' to " .. #photos .. "photos")
for _, photo in ipairs(photos) do
if progressScope:isCanceled() then
return
end
local currentKeywords = photo:getFormattedMetadata('keywordTags') or ""
local keywords = {}
if currentKeywords ~= "" then
for kw in string.gmatch(currentKeywords, "[^,]+") do
local trimmed = string.match(kw, "%s*(.-)%s*$")
if trimmed ~= "" then
table.insert(keywords, trimmed)
end
end
end
local keywordExists = false
for _, existingKeyword in ipairs (keywords) do
if existingKeyword == keyword then
keywordExists = true
break
end
end
if not keywordExists then
table.insert(keywords, keyword)
photo:setPropertyForPlugin(_PLUGIN, "tempKeywords", table.concat(keywords, ", "))
local success, errorMessage = pcall(function()
photo:setRawMetadata("keywordTags", table.concat(keywords, ", "))
end)
if not success then
local catalog = LrApplication.activeCatalog()
catalog:setKeywordStringForPhoto(photo, table.concat(keywords, ", "))
end
end
end
end
end
end
end
end
local function collectionToKeyword()
LrTasks.startAsyncTask(function()
local catalog = LrApplication.activeCatalog()
local collections = catalog:getChildCollections()
local collectionSets = catalog:getChildCollectionSets()
local allCollections = {}
for _, collection in ipairs (collections) do
table.insert(allCollections, collection)
end
for _, collectionSet in ipairs (collectionSets) do
table.insert(allCollections, collectionSet)
end
if #allCollections == 0 then
LrDialogs.message ("no collections Found", "no collections in the catalog", "info")
return
end
local result = LrDialogs.confirm(
"Add Collection Keywords",
"This will add collection hierarchy as keywordss to all photos in non -smart collections.\n\n do you want to continue",
"Continue",
"Cancel"
)
if result == "Cancel" then
return
end
local progressScope = LrDialogs.showModalProgressDialog({
title = "Adding Collection Keywords",
functionContext = function(progressScope)
local catalog = LrApplication.activeCatalog()
catalog:withWriteAccessDo("Add collection Keyword", function()
processCollections(allCollections, progressScope)
end)
if not progressScope:isCanceled() then
LrDialogs.message("Complete", "Collection to keywords done successfully!", "info")
end
end
})
end)
end
collectionToKeyword()my plugin is functional, when I launch it I have the confirmation message (LrDialogs.confirm) but then I get an error ?:0: attempt to index a function value
stuck here.
Goal of my plugin is to auto add keywords to all photos in my collections (couldn’t find any other plugin to do it, i have enough collections so that it’s worth trying to get the plugin working...)
I use Lightroom 6.4 on windows 10, yes I now, old one… classic indeed
Thanks for any help
