1

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.

3
  • You can declare/define variables using set theResult to do shell script "/usr/local/bin/blueutil status". However, I've tried to execute blueutil status in my terminal and apparently the argument Status doesn't exist. Commented Jul 22, 2021 at 14:18
  • Also note that Terminal is not needed when using do shell script. Commented Jul 22, 2021 at 14:44
  • @red_menace still, blueutil has no argument called status. I think OP is searching for blueutil -p. Commented Jul 22, 2021 at 14:46

1 Answer 1

2

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.