Copy link to clipboard
Copied
I'm trying to write a plugin to manage some of my keywords and I am struggling a little bit with the SDK.
I have a photo (LRPhoto) to which I want to assign a keyword, I have the full name of the keyword in the hierarchy. For example the keyword might be "$WORKLIST > 4-Selection" or "$WHERE > Europe > UK > England > Greater Manchester > Stockport > Heaton Mersey"
I can get LRKeywords via a call to catalog:getKeywords()
Where I am struggling is with how to quickly traverse the hierarchy to the correct key so that I can assign it using photo:addKeyword()
Does anyone have any example code I can look at that will do this? I am pretty sure due to the nested keywords that I need to do something that is recursive, but I am getting pretty rusty on my programming (It's 18 years since I switch from working in IT to become a photographer!).
Thanks
Ian.
Copy link to clipboard
Copied
Here's a function that will look up a keyword in the hierarchy, returning nil if there is no keyword with "name":
local function findKeyword (name)
local function find (keywords)
for _, keyword in ipairs (keywords) do
if name == keyword:getName () then return keyword end
local descendantKeyword = find (keyword:getChildren ())
if descendantKeyword then return descendantKeyword end
end
return nil
end
return find (catalog:getKeywords ())
end
Note that if there is more than one keyword with "name", this returns the first one it finds.
Also, if your code will be doing this dozens or hundreds of times, and you have a very large hierarchy with thousands of keywords, this function could be sluggish. In that case, you could traverse the hierarchy once and cache the results in a table.
Copy link to clipboard
Copied
Thank you, John, that looks like exactly what I need!
As it's 23:48 here in the UK - I will try it out tomorrow when I am awake!
Thanks
Ian.