Lightroom SDK: Unable to create nor return existing collection/set when case differs.

  • 3
  • Problem
  • Updated 6 years ago
So if you go to create a collection and say "return existing", and the collection already exists, except differs by case, then it won't return the existing one, nor will it create a new one. Ditto for sets.

Error message: name is not unique

These functions (and others like them) should be case insensitive, i.e. not case sensitive.
Photo of Rob Cole

Rob Cole

  • 4831 Posts
  • 372 Reply Likes

Posted 6 years ago

  • 3
Photo of Rob Cole

Rob Cole

  • 4831 Posts
  • 372 Reply Likes
Here is some example workaround code for the time being:

--- Case insensitve version of like-named lr-catalog method.
--
function Catalog:createCollectionSet( name, parent, returnExisting )
parent = parent or catalog
local children = parent:getChildCollectionSets()
local _name = LrStringUtils.lower( name )
local existing
for i, set in ipairs( children ) do
if LrStringUtils.lower( set:getName() ) == _name then
existing = set
break
end
end
if existing then
if returnExisting then
return existing
end
else
return catalog:createCollectionSet( name, parent, returnExisting )
end
end

A similar method is needed for creating collections, and smart collections, and don't forget the publish versions.
Photo of Rob Cole

Rob Cole

  • 4831 Posts
  • 372 Reply Likes
"Final" method for creating regular collection:

--- Case insensitve version of like-named lr-catalog method.
--
function Catalog:createCollection( name, parent, returnExisting )
local existing = self:getCollection( name, parent ) -- returns collection if one already exists with matching name (case insensitively).
if existing then
if not existing:isSmartCollection() then
if returnExisting then
return existing
else
return nil -- as specified in SDK api doc - this means "can't create since already created".
end
else -- exists, but not a normal collection.
-- the documentation is not clear what to do if collection exists with same name, but different type.
app:error( "Can not create regular collection with specified name (^1), since smart collection already exists with that name (^2)", name, existing:getName() )
-- I assume there is gonna be an error at some point no matter, so may as well be clear up front what the problem is.
end
else
return catalog:createCollection( name, parent, returnExisting ) -- LrCatalog method.
end
end
Photo of Rob Cole

Rob Cole

  • 4831 Posts
  • 372 Reply Likes
Note: now that I've programmed around it, it's a non-issue for me. But, fixing this simple, easy-to-fix issue will keep some errors from happening in plugins where this work-around has not been incorporated.

At a minimum, add a note to the documentation, e.g.: "will not work if collection exists with same name but different case". At least then plugin authors have been "warned".