Skip to main content
Karsten.G
Known Participant
September 23, 2018
Answered

Checking whether collection is smart

  • September 23, 2018
  • 2 replies
  • 775 views

First of all - I'm just starting with Lua and LR SDK so please be indulgent with me 😉

My problem:

I am unable to determine whether a LrCollection object is a simple or smart collection.

LrCollection API provides a method collection:isSmartCollection() but I can't get it to work.

Hope you can help me to figure out what I am doing wrong.

My starting point:

A plugin that calls an external program to process the file(s) being handed over.

Upon return to LR, the resulting image file is added to the catalog.

I want to enhance the plugin so that the newly added file is also added to the collection from which the plugin was called, in case it was a collection.

The code I am using is as follows:

activeSourceIsCollection = nil
                                                                                 
-- determine the source of the selected photo(s)                                  
local activeSources = exportSession.catalog:getActiveSources()                    
                                                                                 
-- Can there be more than 1 entry in this table??                                 
-- I have no clue. But at least there is one, and this is the one we'll work with 
activeSource = activeSources[1]                                             
                                                                               
-- Check if it's a collection or a folder -> type must be 'table'                 
if (type(activeSource) == "table") and (activeSource:type() == "LrCollection") then
-- source is collection or smart collection                                     
  LrTasks.startAsyncTask (function()                                            
    activeSourceIsCollection = not activeSource:IsSmartCollection()             
  end)                                                                          
end                                                                             

...

if activeSourceIsCollection then
-- add to collection as well
            exportSession.catalog:withWriteAccessDo( LOC "$$$/SNSHDR/Export/Import=Import tonemapped file",
                function()
                    activeSource:addPhotos( { reimportedPhoto } )   
                end )
        end

When the second if-stmt is encountered (which is several seconds after the AysncTask has been started, because in the meantime the external program was running) the activeSourceIsCollection is still untouched, meaning 'nil",

What am I doing wrong??

Thanks a lot for any hints and help.

Karsten

This topic has been closed for replies.
Correct answer johnrellis

You've misspelled the method isCollection() (it's lower-case "i"):

activeSource:IsSmartCollection()

You didn't see any error raised because this is executed inside of a task other than the main task -- when an error occurs in other than the main task, LR simply terminates the task without raising an error or logging.

I recommend you use my Debugging Toolkit or search this forum for how to connect a Lua IDE's debugger to Lightroom. It will take an hour or two to get up to speed with either, but it will quickly pay off. Trying to develop plugins without a robust debugger and just print statements is a great way to waste your weekends...

2 replies

johnrellis
Legend
September 24, 2018
Can there be more than 1 entry in this table??

Yes. The documentation says that catalog:getActiveSources() returns an "Array of the currently viewed objects". The user can select more than one source in the left column of the Library module (e.g. multiple folders and/or collections), and this method returns all of them in the array.

johnrellis
johnrellisCorrect answer
Legend
September 24, 2018

You've misspelled the method isCollection() (it's lower-case "i"):

activeSource:IsSmartCollection()

You didn't see any error raised because this is executed inside of a task other than the main task -- when an error occurs in other than the main task, LR simply terminates the task without raising an error or logging.

I recommend you use my Debugging Toolkit or search this forum for how to connect a Lua IDE's debugger to Lightroom. It will take an hour or two to get up to speed with either, but it will quickly pay off. Trying to develop plugins without a robust debugger and just print statements is a great way to waste your weekends...

Karsten.G
Karsten.GAuthor
Known Participant
September 24, 2018

John, you're my hero !! :-)

What a difference a wrong caps can make... said the guy who grown up with programming languages that have strong semantic checks ;-)  Now my code works as intended.

I will also look into a proper handling of catalog:getActiveSources() result to make the plugin more robust. I was not aware of the use case you mentioned since I never work this way.

As for debugging: so far I have been using ZeroBraneStudio which is really convenient since it provides interactive source code debugging. But I was planning to look into your debugger toolkit anyway.

Many thanks !!

johnrellis
Legend
September 25, 2018

I think the primary downside of ZeroBrane is that it doesn't know about the peculiarities of LR's error reporting as it interacts with tasks other than the main task. Any error raised on a non-main task is silently ignored, and I don't think ZeroBrane will catch these "ignored" errors.  The same applies for many (all?) call-back functions that are passed into the API, e.g. callbacks provide to LrView objects.  My debugging toolkit provides a recipe for handling those situations, so you never miss an error.

I might be mistaken about ZeroBrane, since I've only read about it, not used it myself.