4 Replies Latest reply on May 11, 2015 3:11 PM by Lr5user-pt

    Problem using openPathsViaCommandLine

    Lr5user-pt Level 2

      Hello everybody,

      I’m writing a small LR script to invoke an external Windows program that will use the most selected photo in LR. The external program, let’s call it “MyApp”, expects a command line that includes the complete path to the file to use and an extra flag, in this case “–LR”, to signal that it was invoked by LR, and so adapt its own UI. The command line should be something like this:

       

      c:\somepath\MyApp.exe –LR c:\someotherpath\somephoto.dng

       

      In the script I used:

      MyApp = "C:\\Users\\carlos\\dev\\LR\\rawdataviewer\\rawdataviewer.exe"

      AppArg = "-LR"

      .

      .

      Some code to get the most selected photo in "photoPath"...

      .

      LrShell.openPathsViaCommandLine( {photoPath}, MyApp, AppArg)

       

       

      The problem is that MyApp never gets called. No error messages, just nothing happens.

       

      Using:

       

      LrShell.openFilesInApp( {photoPath}, MyApp )

       

      It works fine, MyApp gets invoked, it opens the file, but, of course, it does not get the “-LR” argument and it does not adapt the interface as intended. This was only meant to validate both “photoPath” and “MyApp”, and both seem ok.

       

      I end up using:

       

      LrTasks.execute (MyApp.. ' '.. AppArg.. ' "' .. photoPath ..'"')

       

      and all seems fine, but I have to surround photoPath in quotation marks as it seems to not like spaces in the path string.

      The problem is solved, for now, but would like to understand what I have done wrong while using openPathsViaCommandLine.

      Thanks.

        • 1. Re: Problem using openPathsViaCommandLine
          johnrellis Most Valuable Participant

          LrShell.openPathsViaCommandLine() doesn't work; apparently it was never even tested.  Please vote and add your opinion to this bug report: Lightroom SDK: LrShell.openPathsViaCommandLine doesn't work.

           

          LrTasks.execute() is itself buggy and doesn't work properly when the program path in the command line is quoted.  Here's a function I use to work around that:

          --[[----------------------------------------------------------------------------
          public int exitCode, string output
          safeExecute (string commandLine [, boolean getOutput, [,string tempBase] ])
          
          Executes the command line "commandLine"in the platform shell via
          LrTasks.execute, working around a bug in execute() on Windows where quoted
          program names aren't accepted.
          
          If "getOutput" is true, then stdout and stderr are redirected to a single temp
          file. If "tempBase" is non-nil, it should be a fully qualified file path; the
          output captured in tempBase.log, and the file won't be deleted at the end;
          otherwise, the file will be created in the system temp directory and deleted
          afterwards.
          
          Returns in "exitCode" the exit code of the command line, and in "output" the
          contents of stdout/stderr, or "" if ouput is not being captured or was unable to
          read.  If any errors occur in safeExecute itself, "exitCode" will be -1, and
          "output" will be an error message.
          ------------------------------------------------------------------------------]]
          
          function Util.safeExecute (commandLine, getOutput, tempBase)
          return LrFunctionContext.callWithContext ("", function (context)
              local f, logFile, msg
              context:addCleanupHandler (function ()
                  if f then f:close () end
                  if not tempBase and logFile then LrFileUtils.delete (logFile) end
                  end)
                 
              if getOutput then
                  if tempBase then
                      logFile = tempBase .. ".log"
                  else
                      logFile = LrPathUtils.child (
                          LrPathUtils.getStandardFilePath ("temp"), "safeExecute.log")
                      logFile = LrFileUtils.chooseUniqueFileName (logFile)
                      end
                  commandLine = string.format ('%s > "%s" 2>&1',commandLine, logFile)
                  end
                 
              if WIN_ENV then commandLine = '"' .. commandLine .. '"' end
              local exitStatus = LrTasks.execute (commandLine)
          
              local output = ""
              if getOutput then
                  f, msg = io.open (logFile, "r")
                  if not f then return -1, msg end
                  output, msg = f:read("*a")
                  if not output then return -1, msg end
                  f:close (); f = nil
                  end
          
              return exitStatus, output
              end) end
          
          • 2. Re: Problem using openPathsViaCommandLine
            Lr5user-pt Level 2

            Thank you for your answer and code, much appreciated. I voted your bug report. Thanks again.

            • 3. Re: Problem using openPathsViaCommandLine
              johnrellis Most Valuable Participant

              Looks like LrShell.openPathsViaCommandLine() is fixed in LR 6.0.1.

              • 4. Re: Problem using openPathsViaCommandLine
                Lr5user-pt Level 2

                Good news. Thanks.