Skip to main content
Participant
January 13, 2017
Question

Get output from LrTasks.execute(cmd)

  • January 13, 2017
  • 1 reply
  • 1342 views

I would like to know if there is a way to get the output of a LrTasks.execute(cmd) call, without piping it to an external text file and reading it back ?

Many thanks !

This topic has been closed for replies.

1 reply

johnrellis
Legend
January 13, 2017

Including output redirection to a temp file in the command line is the only documented way of capturing the output.  There is the undocumented LrRemoteCommunication which appears to start a task with pipes for reading and writing, but I haven't seen any reports of anyone figuring out how that works.

Here is the utility function I used for running commands:

--[[----------------------------------------------------------------------------

public int exitCode, string output, string errOutput

safeExecute (string commandLine [, boolean getOutput])

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, "output" will contain standard out and standard

error and "errOutput" will be "".  If "getOutput" is "separate", then

"output" will contain standard out and "errOutput" will contain standard

error.  If "getOutput" is false, then both "output" and "errOutput" will be

"".

Returns in "exitCode" the exit code of the command line. If any errors

occur in safeExecute itself, "exitCode" will be -1, and "output" and

"errOutput" will be:

getOuptut == "separate": "", <error message>

otherwise:              <error message>, ""

------------------------------------------------------------------------------]]

function Util.safeExecute (commandLine, getOutput)

return LrFunctionContext.callWithContext ("", function (context)

    local outFile, errFile

    context:addCleanupHandler (function ()

        if outFile then LrFileUtils.delete (outFile) end

        if errFile then LrFileUtils.delete (errFile) end

        end)

      

    if getOutput then

        local uuid = LrUUID.generateUUID ()

        outFile = child (getStandardFilePath ("temp"), uuid .. ".out")

        commandLine = commandLine .. ' > "' .. outFile .. '"'

        if getOutput == "separate" then

            errFile = child (getStandardFilePath ("temp"), uuid .. ".err")

            commandLine = commandLine .. ' 2>"' .. errFile .. '"'

        else

            commandLine = commandLine .. ' 2>&1'

            end

        end

    if WIN_ENV then commandLine = '"' .. commandLine .. '"' end

    local exitStatus = LrTasks.execute (commandLine)

    local output, errOutput, success = "", ""

    local function outputErr (file, output)

        local err = string.format ("Couldn't read output:\n%s\n%s",

            file, output)

        if getOutput == "separate" then

            return -1, "", err

        else

            return -1, err, ""

            end

        end

    if outFile then

        success, output = pcall (LrFileUtils.readFile, outFile)

        if not success then return outputErr (outFile, output) end

        end

    if errFile then

        success, errOutput = pcall (LrFileUtils.readFile, errFile)

        if not success then return outputErr (errFile, errOutput) end

        end

    return exitStatus, output, errOutput

    end) end

rderimayAuthor
Participant
January 13, 2017

ok, bad news... Thanks !