2 Replies Latest reply on Aug 22, 2015 11:32 AM by johnrellis

    Calling getChildCollections()

    kimaldis Level 1

      I'm trying to get a list of all collections in a catalog starting with catalog:getChildCollections() as in the code below. By way of testing I have two calls to this method. The first one returns with an array of 5 empty arrays - I have 5 top level collections. The second seems to be just failing. I see no real error reporting but while the first call to message brings up a dialog, the second and third don't. Does anyone have any thoughts on this, I've exhausted the extent of my knowledge.

       

      As a secondary, I'm actually trying to get to setActiveSources() by collection name. I'm assuming that I need to search through all the collections and pick out a collection object that matches the name?

       

       

       

      LrTasks.startAsyncTask( function( context ) LrFunctionContext.callWithContext( 'dialogExample', function( context ) local collections = catalog:getChildCollections()

      local f = LrView.osFactory() --obtain a view factory local properties = LrBinding.makePropertyTable( context ) -- make a table properties.buttonEnabled = true local contents = f:row { -- create UI elements width = 500, spacing = f:label_spacing(), bind_to_object = properties, -- default bound table is the one we made f:push_button { title = "Collection", enabled = true, action = function( button ) LrDialogs.message( "yo " ) local collections = catalog:getChildCollections() LrDialogs.message( "yo " ) local i = catalog:setActiveSources( { collections[0] } ) LrDialogs.message( "yo " ) end, }, } local result = LrDialogs.presentFloatingDialog( _PLUGIN, -- invoke a dialog box { title = "Go to a URL", contents = contents, -- with the UI elements } ) end ) end )

        • 1. Re: Calling getChildCollections()
          kimaldis Level 1

          Sorry, that failed miserably. Here's a screen grab of the code. Ignore Logging.log, it's just a log to console function.

           

           

          Screen Shot 2015-08-22 at 12.49.39.png

          • 2. Re: Calling getChildCollections()
            johnrellis Most Valuable Participant

            There are a number of issues with you sample.  Here is my version, with line numbers:

             

            local Require = require 'Require'.path ("../common").reload (false)
            local Debug = require 'Debug'.init ()
            require 'strict'
            
            local LrApplication = import 'LrApplication'
            local LrBinding = import 'LrBinding'
            local LrDialogs = import 'LrDialogs'
            local LrFunctionContext = import 'LrFunctionContext'
            local LrTasks = import 'LrTasks'
            local LrView = import 'LrView'
            
            local catalog = LrApplication.activeCatalog ()
            local showErrors = Debug.showErrors
            
            LrTasks.startAsyncTask( function( context ) 
                LrFunctionContext.callWithContext( 'dialogExample', function( context ) 
            
                    local collections = catalog:getChildCollections()
            
                    local f = LrView.osFactory() --obtain a view factory 
                    local properties = LrBinding.makePropertyTable( context ) -- make a table 
                    properties.buttonEnabled = true 
            
                    local contents = f:row { -- create UI elements 
                        width = 500, 
                        spacing = f:label_spacing(), 
                        bind_to_object = properties, -- default bound table is the one we made 
            
                        f:push_button { 
                            title = "Collection", 
                            enabled = true, 
                            action = showErrors (function( button ) 
                                LrTasks.startAsyncTask (function () 
                                    LrDialogs.message( "yo 1" ) 
                                    local collections = catalog:getChildCollections() 
                                    LrDialogs.message( "yo 2" ) 
                                    local i = catalog:setActiveSources( { collections[1] } ) 
                                    LrDialogs.message( "yo 3" )
                                end)
                            end), 
                        }, 
                    } 
                    local result = LrDialogs.presentFloatingDialog( _PLUGIN, -- invoke a dialog box 
                        { 
                            title = "Go to a URL", 
                            contents = contents, -- with the UI elements 
                        } 
                    ) 
                end ) 
            end ) 
            

             

            (Use the Advanced Editor to insert code fragments in posts.)

             

            1. I strongly recommend you use a debugger, either my Debugging Toolkit, which is pretty lightweight and minimal, or a full-fledged IDE (search this forum for a post about that). My toolkit has a pretty-printer that can print nicely formatted any Lua value, including nested and circular tables.  Expect to spend an hour or two with either approach, but that investment will pay off very quickly.


            2. You weren't getting any error reports because, in certain contexts, LR doesn't enable its default error trapping and reporting -- errors are silently ignored. I can never remember all of those situations, but one is in the body of a function passed as a callback to a control created by LrView.   In general, I wrap every function passed to the SDK API with Debug.showErrors(), which enables error trapping for the function.


            3. The callback function on line 32 was failing (and being silently ignored) because catalog:getChildCollections() and catalog:setActiveSources() need to be called within async tasks. LrView callbacks are invoked from LR's main task (even if you launched the dialog from within an async task).   Note that I wrapped the function with Debug.showErrors() (to trap and report errors) and wrapped the body of the function in an async task.


            4. Line 37 referred to collections[0], but arrays in Lua generally start with 1. 

            1 person found this helpful