shell - 如何将脚本外壳的结果与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
但还是不行。
也许是关于使用查询结果作为变量?
我真的不是你可能猜到的专业人士,所以任何帮助将不胜感激!谢谢,克里斯托夫。
解决方案
首先,您根本不需要 Terminal.app。
其次,没有参数status
,要获取电源状态:
set powerStatus to do shell script "/usr/local/bin/blueutil -p" as boolean
结果是true
或false
。
要切换电源状态,请写入:
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"
推荐阅读
- sql - XML 导入 SQL Server 的噩梦
- java - 为什么使用 webflux 进行 spring boot 测试会忽略自定义 jackson 模块
- javascript - 等待所有异步请求在循环内完成
- javascript - 在javascript中动态验证html控件
- install4j - 为什么 Install4j 在卸载后需要在下次安装时重新启动?
- angular - 带有模板驱动表单的 MatHorizontalStepper stepControl
- android - 如何在xml中的按钮中心设置图标
- python - Tkinter如何停止在条目中首先输入0,除非后跟小数点
- python - Python Kivy:在 Kivy 的类中使用变量
- domain-driven-design - DDD - 具有大关系的聚合,是不变量的一部分