Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
11.88k stars 578 forks source link

hs.execute does not return anything #2129

Closed csteinlehner closed 5 years ago

csteinlehner commented 5 years ago

Hello,

it seems like hs.execute doesn't return anything. With this script, I just get an empty return.

function bluetooth(power)
    print("Setting bluetooth to " .. power)
    result = hs.execute('blueutil -p ' .. power, true)
    print(result)  
end

If I want to get some expected information from the result object (like output etc.), the script throws an error.

if result.rc ~= 0 then  
        print("Unexpected result executing `blueutil`: rc=" .. result.rc .. " type=" .. result.type .. " output="  .. result.output)  
end

I call this script from hs.caffeinate.watcher.systemWillSleep event. Could this be an issue?

Thanks for your help.

asmagill commented 5 years ago

hs.execute isn't returning a table but three actual results, so you should call it in one of the following ways:

output, status, termType = hs.execute('blueutil -p ' .. power, true)

-- or

result = table.pack(hs.execute('blueutil -p ' .. power, true))

The first puts the three return values into separate variables; the second packs the results into a table where output is in results[1], the status is in results[2], and the termination type is in results[3].

csteinlehner commented 5 years ago

@asmagill Thanks for the insight. The first attempt with three variables didn't work for me, but the table.pack approach works just fine. Thanks for the help!