首页 > 解决方案 > 如何将脚本外壳的结果与applescript一起使用?

问题描述

我正在尝试从 applescript 运行一个简单的代码。

它使用 blueutil,一个命令行实用程序,可以查询蓝牙的状态(打开或关闭)/打开/关闭它。

我尝试了 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

没有成功。如果我清理所有内容并且只保留关于关闭或打开的线路,它可以工作。

我似乎得到的最干净的代码是:

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

但还是不行。

也许是关于使用查询结果作为变量?

我真的不是你可能猜到的专业人士,所以任何帮助将不胜感激!谢谢,克里斯托夫。

标签: shellapplescript

解决方案


首先,您根本不需要 Terminal.app。

其次,没有参数status,要获取电源状态:

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

结果是truefalse


要切换电源状态,请写入:

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

要将电源状态设置为开启:

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

要将电源状态设置为关闭:

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

是的,这只是一行。


您可以获得显示手册页的帮助消息。

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

推荐阅读