6 Replies Latest reply on Dec 12, 2012 11:08 PM by dhmc05

    Add selected photos to new collection

    dhmc05 Level 1

      Probably a newby question, but how can I add selected photos to a new collection.

       

      Reading the SDK doc:

      1. catalog:getTargetPhotos()
        Return value
        (array of LrPhoto) The photos.
      2. collection:addPhotos( photos ):
        Parameters
        photos (array of LrPhoto) The photo objects.

       

      To me catalog:getTargetPhotos() returns a array of LrPhoto and collection:addPhotos askes for an array of LrPhoto.

       

      Yet I always get "An internal error has occurred: assertion failed".

       

      Where do I go wrong?

       

      I'm running the code below on an Windows XP SP3 machine with Lightroom 3.6

       

      local LrTasks = import 'LrTasks'

      local catalog = import "LrApplication".activeCatalog()

      local LrFunctionContext = import 'LrFunctionContext'

      local LrProgressScope = import 'LrProgressScope'

      local LrDialogs = import 'LrDialogs'

       

      local myLogger = import 'LrLogger'( 'AddImages' )

      myLogger:enable( "logfile" )

       

      function AddImagesToCollection()

       

          LrTasks.startAsyncTask(function()

       

              catalog:withWriteAccessDo( "Test", function()

       

      local test1Collection = catalog:createCollection( "Test_1 Collection", nil, true )

       

      local cat_photos = catalog.getTargetPhotos()

      local nCountSelected = #cat_photos

      myLogger:trace( 'number selected photos:', #cat_photos)

       

      test1Collection:addPhotos{ cat_photos }

              end)

       

          end)

      end

       

      AddImagesToCollection()

       

        • 1. Re: Add selected photos to new collection
          areohbee Level 5

          Like many catalog functions, one thing can't be done until the other has been completely done -

           

          you need to create the collection, and add photos to it, in separate calls to with-write-access-do.

          • 2. Re: Add selected photos to new collection
            dhmc05 Level 1

            Changed the code by adding a with-write-access-do, but still get the same error: "An internal error has occurred: assertion failed".

             

            @Rob Cole, send you a e-mail ...

             

             

            function AddImagesToCollection()

             

                LrTasks.startAsyncTask(function()

             

            local test1Collection

            local cat_photos

             

                    catalog:withWriteAccessDo( "Test", function()

             

            test1Collection = catalog:createCollection( "Test_1 Collection", nil, true )

                    end)

             

                    catalog:withWriteAccessDo( "Test1", function()

             

            cat_photos = catalog:getTargetPhotos()

            local nCountSelected = #cat_photos

            myLogger:trace( 'number selected photos:', #cat_photos)

             

            test1Collection:addPhotos{ cat_photos }

                    end)

             

                end)

             

            • 3. Re: Add selected photos to new collection
              johnrellis Most Valuable Participant

              In your original code that you posted, you have a dot rather than a colon: catalog.getTargetPhotos(). That's probably the most common SDK error, and it has such a cryptic error message.

              • 4. Re: Add selected photos to new collection
                dhmc05 Level 1

                Hi John,

                 

                I noticed the point and changed it, yet I still get the same error

                 

                Anyone any idea why this little piece of code is not working?

                 

                Any advice how to solve it?

                • 5. Re: Add selected photos to new collection
                  johnrellis Most Valuable Participant

                  The problem is with this line:

                   

                  test1Collection:addPhotos{ cat_photos }
                  

                   

                  This is equivalent to:

                   

                  test1Collection:addPhotos( {cat_photos} )
                  

                   

                  The expression {cat_photos} makes an array with one element, the value of cat_photos (itself an array of LrPhoto).  But collection:addPhotos() wants an array of LrPhoto, not an array of an array.  Changing it to this:

                   

                  test1Collection:addPhotos( cat_photos )
                  

                   

                  will allow it to execute correctly.

                   

                  I suggest you use the Debugging Toolkit, which will make it much quicker to identify your bugs. It will take some time to hook it up the first time, but it will pay off pretty quickly.

                  • 6. Re: Add selected photos to new collection
                    dhmc05 Level 1

                    A little late reply, but THANK YOU!

                     

                    I see the problem and now it works.