在OS X 10.8中,我有一个整洁的小AppleScript,我常常在不使用鼠标的情况下快速切换蓝牙。
我更新到10.9,它为系统偏好设置添加了几个UI更改。除此之外,它还取代了将蓝牙从复选框切换到按钮的元素。我的脚本现在已经破了,因此我的工作流也是如此。
问题是按钮的名称会根据其状态从“打开蓝牙”变为“关闭蓝牙”。我没有充分掌握AppleScript以找出解决方法,并且想知道你们是否可以帮助我。
答案 0 :(得分:7)
这在10.9中对我有用:
tell application "System Preferences"
reveal pane "com.apple.preferences.Bluetooth"
end tell
tell application "System Events" to tell process "System Preferences"
click button 6 of window 1
end tell
quit application "System Preferences"
您也可以使用blueutil:
/usr/local/bin/blueutil|grep -q 'Power: 1';/usr/local/bin/blueutil power $?
答案 1 :(得分:2)
这在 10.15.6 中对我有用,我的解决方案可能过于复杂,它运行脚本 1(关闭蓝牙)然后运行脚本 2(打开蓝牙)。
脚本 1. 这是用于关闭蓝牙
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:background="@color/colorPrimary">
<ImageView
android:id="@+id/navi"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:layout_marginStart="20dp"
android:contentDescription="@string/todo"
android:src="@drawable/navi" />
<ImageView
android:layout_width="180dp"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:contentDescription="@string/todo"
android:src="@drawable/header2" />
</RelativeLayout>
<include layout="@layout/fragment_home" />
</RelativeLayout>
脚本 2. 这是用于打开蓝牙
tell application "System Events" to tell process "SystemUIServer"
tell (menu bar item 1 of menu bar 1 where description is "bluetooth")
click
click menu item "Turn Bluetooth Off" of menu 1
end tell
tell window 1
click button "Turn Bluetooth Off"
end tell
end tell
所以我执行一个命令,它会一个接一个地运行脚本,睡眠是为了让 UI 正确更新。
tell application "System Events" to tell process "SystemUIServer"
tell (menu bar item 1 of menu bar 1 where description is "bluetooth")
click
click menu item "Turn Bluetooth On" of menu 1
end tell
end tell
您可以将命令保存在一个文件中并使用以下命令执行它:(它们必须在同一目录中)。
osascript bluetooth_off.scpt && sleep 3s && osascript bluetooth_on.scpt
答案 2 :(得分:1)
tell application "System Preferences"
reveal pane "com.apple.preferences.Bluetooth"
end tell
tell application "System Events" to tell process "System Preferences"
repeat until exists window "Bluetooth"
end repeat
try
click button "Turn Bluetooth Off" of window "Bluetooth"
do shell script "networksetup -setairportpower airport off"
on error
click button "Turn Bluetooth On" of window "Bluetooth"
do shell script "networksetup -setairportpower airport on"
end try
end tell
tell application "System Preferences" to quit
答案 3 :(得分:0)
为我工作,没有蓝色的工具:
tell application "System Preferences"
reveal pane id "com.apple.preferences.Bluetooth"
-- activate
set the current pane to pane id "com.apple.preferences.Bluetooth"
try
tell application "System Events" to tell process "System Preferences"
click button "Turn Bluetooth Off" of window "Bluetooth"
click button "Turn Bluetooth Off" of sheet 1 of window "Bluetooth" of application process "System Preferences" of application "System Events"
end tell
delay 1
on error
tell application "System Events" to tell process "System Preferences"
click button "Turn Bluetooth On" of window "Bluetooth"
quit
end tell
end try
end tell
答案 4 :(得分:0)
有时我的 Mac 会断开与我的 Logitech MX Master 3 的连接,因此需要快速切换蓝牙。但是我在关闭笔记本电脑的情况下工作,所以一旦我关闭蓝牙,我就会失去对计算机的输入,直到我物理打开它
此脚本通过关闭蓝牙,等待 5 秒,然后重新打开来解决问题
当我的鼠标连接断开但我仍然可以使用键盘访问时,我导航到包含此脚本的目录,命令 + 向下箭头打开它,命令 + R 运行它。 5 秒后,我的鼠标又回来了!
切换蓝牙.scpt:
display dialog "Toggle Bluetooth?"
-- turn bluetooth off
tell application "System Events" to tell process "SystemUIServer"
tell (menu bar item 1 of menu bar 1 where description is "bluetooth")
click
click menu item "Turn Bluetooth Off" of menu 1
end tell
end tell
-- wait 5 seconds
delay 5
-- turn bluetooth on
tell application "System Events" to tell process "SystemUIServer"
tell (menu bar item 1 of menu bar 1 where description is "bluetooth")
click
click menu item "Turn Bluetooth On" of menu 1
end tell
end tell
display dialog "Welcome Back!"
答案 5 :(得分:0)
对于 Big Sur,Enrique Scherer 的回答不再有效。但是,blueutil 实用程序 has been updated 可以从 Homebrew 等安装。如果您想要使用 Applescript 和/或不使用第三方实用程序的解决方案,我修改了 some scripts posted on reddit 以使其工作:
bluetooth_toggle.scpt
tell application "System Events"
tell process "ControlCenter"
set BluetoothButton to menu bar item "Bluetooth" of menu bar 1
click BluetoothButton
delay 1
set OnSwitch to checkbox "Bluetooth" of group 1 of window "Control Center"
click OnSwitch
delay 3
click OnSwitch
end tell
key code 53
end tell
用法:
osascript bluetooth_toggle.scpt