14 Replies Latest reply on Dec 9, 2015 12:59 AM by jeffreyw24183857

    LrShutdownApp example

    jeffreyw24183857

      Hi,

       

      I am looking for an short example how to use LrShutdownApp in a lua script.

      Google can not find so I was wondering if somebody might have a short example.

      I want to use the LrShell.openFilesInCommandLineProcess function in this script.

       

      regards,

       

      Jeffrey

       

      This is a repost from:

      https://forums.adobe.com/inbox?objectType=2&objectID=8230629

        • 1. Re: LrShutdownApp example
          jeffreyw24183857 Level 1

          adding LrShutdownApp = { LrShutdownFunction = 'ShutDownApp.lua' }

          to the info.lua seems to be working better.

          anyone got an idea?

          • 2. Re: LrShutdownApp example
            johnrellis Most Valuable Participant

            Have you read page 29 of the Lightroom SDK 6 Programmers Guide?  I haven't used Lr ShutdownApp, but page 29 seems pretty clear about how to use it.  

            • 3. Re: LrShutdownApp example
              jeffreyw24183857 Level 1

              Dear John,

               

              Thank you for your reply.

              Yes I have read these pages, that is how I know that the lrShutdownApp function exists.

              Googling on it gives me only SDKs. (and my question on this form)

               

              I have tried and search the sdk for examples how to configure the called upon lua file.

              the search keys "parameters" / "two" etc came up with no other function in the sdk ,I could use to see how I have to use this.

               

              I am in the process of understanding lua, so an example would clear it up a lot for me.

              Would you be so kind to write down a couple of example lines of how I need to configure the info.lua and the ShutDownApp.lua in my case?

              Reference to one of the SDK example files and function to look at works for me as well.

               

              Thank you in advance.

              • 4. Re: LrShutdownApp example
                jeffreyw24183857 Level 1

                Can someone verify why this is not working?


                --insert in Info.lua

                LrShutdownApp = 'shutdown.lua'

                 

                -- In shutdown.lua

                return {

                    LrShutdownFunction = function(doneFunction, progressFunction)

                        doneFunction = function()

                            --insert code here when LR is done?

                        end

                       

                        progressFunction = function(percent, description)

                           --insert code here during shut done?

                           --can you do this: percent = 1 ? then it finished ok?

                        end

                    end

                  }

                • 5. Re: LrShutdownApp example
                  johnrellis Most Valuable Participant

                  I think this is the outline of how to write "shutdown.lua":

                   

                  local function shutdownFunction (doneFunction, progressFunction)
                      local percentComplete = 0
                      while ... do
                              -- Do some work shutting down the plugin and then report progress
                          ...
                          percentComplete = percentComplete + ...
                          if progressFunction (percentComplete, "Shutting down MyPlugin") then
                                  -- User clicked cancel
                              break
                              end
                          end
                  
                      doneFunction ()
                      end
                  
                  return {LrShutdownFunction = shutdownFunction}
                  
                  

                   

                  (The description on page 29 is poorly written -- too much passive voice that doesn't it make it clear who calls what.) But I think when LR shuts down, the sequence goes like this:

                   

                  1. LR invokes "shutdown.lua".

                   

                  2. "shutdown.lua" returns a table containing "shutdownFunction".

                   

                  3. LR invokes shutdownFunction (doneFunction, progressFunction).

                   

                  4. shutdownFunction() does the work of shutting down the plugin.  It calls progressFunction() periodically as it does its work, and when it's done it calls doneFunction().

                  • 6. Re: LrShutdownApp example
                    jeffreyw24183857 Level 1

                    Thank you for your reply.

                    With the code I understand the API better.

                     

                    I tried:

                     

                    local function shutdownFunction (doneFunction, progressFunction)
                        local percentComplete = 1
                        if progressFunction (percentComplete, "Thank you for using Plug in") then
                                -- User clicked cancel
                            break
                        end
                    
                        doneFunction ()
                    
                    end
                    
                    return {LrShutdownFunction = shutdownFunction}
                    

                     

                    But I still get that the plug in it terminated because it is not responding.

                    With this code I would expect that the plug in would terminate immediately.

                     

                    Any suggestions?

                     

                    Currently using LR 6.3.

                    • 7. Re: LrShutdownApp example
                      johnrellis Most Valuable Participant

                      Have you verified with breakpoints or logging that shutdownFunction() is being invoked (i.e. that line 2 is being executed) and that the call to doneFunction() on line 8 is being executed?

                      • 8. Re: LrShutdownApp example
                        jeffreyw24183857 Level 1

                        question:

                        1] how do you debug this? its not like its like "old school", c / c++ / vb languages

                        it's "clean", like java was when it first came out. (no correct, java had messageboxes)

                        and the SDK says : The LrDialogs namespace is not available in the environment of the shutdown task.


                        Googling on how to debug give 0 hits you can use.

                        • 9. Re: LrShutdownApp example
                          johnrellis Most Valuable Participant
                          how do you debug this?

                          Page 186 of the Lightroom SDK 6 Programmers Guide explains how to use basic logging.

                           

                          I use my Debugging Toolkit, which provides quite a bit more flexibility (pretty printing of arbitrary values, interactive evaluation and debugging).  It's minimal and lightweight, takes an hour or so to get up to speed, but it will pay off immediately in much higher productivity.

                           

                          There's a message somewhere in these forums about how to connect a particular IDE to LR, but I've never tried that.  I know one person posted in the last couple of months they couldn't get it to work (yet).

                          • 10. Re: LrShutdownApp example
                            jeffreyw24183857 Level 1

                            Dear John,

                             

                            Sorry for my late reply but it seems that the functions are not loaded.

                             

                            How do I know?

                            my debugging tool:

                            local function outputToLog( message )

                                local out = assert(io.open("c:/MIDI2LR.lrplugin/debug.log", "a"))   

                                out:write(message, "\n")   

                                assert(out:close())

                            end

                             

                            writes the data to the log during start up, so the function works correctly.

                            But when used in the shutdownFunction, there is nothing written in the log. (function is declared locally)

                            (thanks to the file close I am pretty sure that everything is written in the log file)

                             

                            Any suggestions?

                            • 11. Re: LrShutdownApp example
                              johnrellis Most Valuable Participant

                              Try putting outputToLog ("Here") at the very top of shutdown.lua (not inside a function).  This will test if shutdown.lua itself is getting loaded by LR at some point.

                              • 12. Re: LrShutdownApp example
                                jeffreyw24183857 Level 1

                                John,

                                 

                                I got it working the way I want it.(Not what I expected by he it works)

                                 

                                My shut down script lua:

                                 

                                function sleep(s)

                                  local ntime = os.time() + s

                                  repeat until os.time() > ntime

                                end

                                 

                                return {

                                        LrShutdownFunction = function(doneFunction, progressFunction)                                     

                                            local LrShell             = import 'LrShell'           

                                             if(WIN_ENV) then

                                                LrShell.openFilesInApp({"--ShutDown"}, _PLUGIN.path..'/MIDI2LR.exe')

                                            else

                                                LrShell.openFilesInApp({"--ShutDown"}, _PLUGIN.path..'/MIDI2LR.app') -- On Mac it seems like the files argument has to include an existing file

                                            end

                                           

                                            for i=0,1,1

                                            do

                                               progressFunction (i, "Thank you for using MIDI2LR")

                                               sleep(1)

                                            end

                                           

                                            doneFunction ()

                                       

                                        end

                                        }

                                 

                                So items I found:

                                1]. if(progressFunction) statements equals stop script. Don't not why. but it does.

                                2]. Sending parameters is a pain in the #ss, so I misused the openFilesInApp as the openPathsViaCommandLine is not working as expected.

                                 

                                Thank you for your support, I could not have done this without you.

                                • 13. Re: LrShutdownApp example
                                  johnrellis Most Valuable Participant

                                  Glad you've got something working.  A couple of notes:

                                   

                                  - Did you try using LrTasks.sleep() to actually sleep?

                                   

                                  - Consider using LrTasks.execute() to execute the command line, rather than LrShell.  On Windows, a long-standing bug requires you to wrap the command line in an extra set of quotes.  See this post for more details: Re: Problem using openPathsViaCommandLine

                                  • 14. Re: LrShutdownApp example
                                    jeffreyw24183857 Level 1

                                    I have not tried it yet.

                                    Maybe in the future, it works at the moment so I am returning to the c++ domain I am more comfortable with.

                                     

                                    The usage of the LrShell.openFilesInApp is heritage from the project.

                                    I just wanted to add a solution for the app to stop when LR is stopping.

                                     

                                    In the future I will have a look at it, but currently there is too much other work I would like to do.