Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


SDK Best/Quickest way to get a keyword by name

Participant ,
Jun 24, 2021 Jun 24, 2021

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.

TOPICS
SDK

Views

113

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 24, 2021 Jun 24, 2021

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 24, 2021 Jun 24, 2021

Copy link to clipboard

Copied

LATEST

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines