gullible garlic
gullible garlic

Reputation: 11

How to use result from a script shell with applescript?

I'm trying to run a simple code from applescript.

It use blueutil, a command-line utility that can query Bluetooth’s status (on or off) / turn it on / turn it off.

I try this code from rob cottingham:

tell application “Terminal”
do shell script “/usr/local/bin/blueutil status”
set _Result tothe result
if _Result is “Status: on” then
do shell script “/usr/local/bin/blueutil off”
endif
if _Result is “Status: off” then
do shell script “/usr/local/bin/blueutil on”
endif
endtell

Without success. If i clean all and only keep the lines about turning off or on, it works though.

Cleanest code I seems to get is:

tell application "Terminal"
    do shell script "/usr/local/bin/blueutil status"
    
    set theResult to the result
    if "result" is "Status: on" then
        do shell script "/usr/local/bin/blueutil off"
    end if
    
end tell

But still doesn't work.

Maybe it's about using the result of the query as a variable?

I'm really not a professional it as you probably guessed, so any help will be appreciated ! Thanks, Christophe.

Upvotes: 1

Views: 1149

Answers (1)

vadian
vadian

Reputation: 285064

First of all you don't need Terminal.app at all.

Second of all there is no argument status, to get the power state write:

set powerStatus to do shell script "/usr/local/bin/blueutil -p" as boolean

The result is true or false.


To toggle the power state write:

do shell script "/usr/local/bin/blueutil -p toggle"

To set the power state to on:

do shell script "/usr/local/bin/blueutil -p on"

To set the power state to off:

do shell script "/usr/local/bin/blueutil -p off"

Yes, it's just one line respectively.


And you can get the help message showing the man page.

set helpText to do shell script "/usr/local/bin/blueutil -h"

Upvotes: 2

Related Questions