年前逛张大妈的时候跟风买了一台 " 云轩 h170" 准系统,配上 CPU、[内存]、[硬盘]后装上 macOS 来用,目前使用下来还不错。不过,可能是出于成本的考量,这台主机是没有音频接口的,为此我买了一个带[声卡]的 Type-C 转 3.5mm 的转接头用来接[耳机]。但是我其实是不喜欢一直带着耳机的,所以平时主要是用蓝牙连接小度[音箱]外放使用。这样使用唯一的麻烦就是每次开机都要手动连接一下音箱。今天试着网上搜索了一下,发现其实是有解决方案的,下面记录一下,希望给有同样需求的朋友一点帮助。
一、AppleScript
AppleScript是用在macOS上的脚本语言,与系统结合非常紧密,操作起来也很方便。我在网上搜到了这个"Connect to bluetooth device (iPhone) via command line on MacOSX",里面的脚本是用来自动连接iPhone的,连接音箱也是一个原理。根据我的需求,我稍微做了一点修改。
set DeviceName to "xiaodu-A9-9191"
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
click bt
if exists menu item DeviceName of menu of bt then
tell (first menu item whose title is DeviceName) of menu of bt
click
tell menu 1
if exists menu item "连接" then
click menu item "连接"
return "Connecting..."
else
key code 53 -- hit Escape to close BT menu
return "No connect button; is it already connected?"
end if
end tell
end tell
else
key code 53 -- hit Escape to close BT menu
return "Cannot find that device, check the name"
end if
end tell
之后保存为以 .scpt 为扩展名的文件,用脚本编辑器打开。运行一次测试一下。
image脚本编辑器中运行
二、让脚本开机运行
为了让脚本开机自动运行,我决定把它变成一个 app。
首先打开 " 自动操作 "。
image自动操作
新建文稿,选择 " 应用程序 "。
image选择 " 实用工具 "," 运行 AppleScript 脚本 "。
image把刚才写好的脚本复制进来,然后保存。
image这样就获得了一个 app,把 app 放到 dock 栏上,选择登录时打开,就可以了。
登录后,菜单栏会有一个转动的齿轮,表示脚本正在运行。
网友评论