在菜单栏中显示蓝牙/音量 Apple 脚本

Posted

技术标签:

【中文标题】在菜单栏中显示蓝牙/音量 Apple 脚本【英文标题】:Show Bluetooth / Volume in menu bar Apple Script 【发布时间】:2020-05-04 14:17:02 【问题描述】:

我有一个工作脚本,我想看看是否有办法隐藏或只是让脚本在后台运行。当前的工作脚本会物理激活窗格,因此用户可以看到它发生在他们面前。

tell application "System Preferences"
    activate
    set the current pane to pane "Bluetooth"
    delay 1
end tell

tell application "System Events"
    tell process "System Preferences"
        set toggleBluetooth to the checkbox "Show Bluetooth in menu bar" of the window "Bluetooth"
        click toggleBluetooth
    end tell
end tell

我还找到了一种可以立即打开某些东西的方法,我想知道是否有办法让它与蓝牙和音量配合使用。

tell application "Finder"
    tell Finder preferences
        set desktop shows hard disks to true
    end tell
end tell

【问题讨论】:

这有帮助吗? Menu bar customization 【参考方案1】:

以下 AppleScript 代码可用于打开和关闭蓝牙菜单栏项,而无需将系统偏好设置放在前面。

if application "System Preferences" is running then
    do shell script "killall 'System Preferences'"
    repeat until application "System Preferences" is not running
        delay 0.1
    end repeat
end if

tell application "System Preferences"
    reveal anchor "Main" of pane id "com.apple.preferences.Bluetooth"
end tell

tell application "System Events"
    repeat until checkbox "Show Bluetooth in menu bar" of window "Bluetooth" of application process "System Preferences" exists
        delay 0.1
    end repeat
    click checkbox "Show Bluetooth in menu bar" of window "Bluetooth" of application process "System Preferences"
end tell

tell application "System Preferences" to quit

这将对菜单栏中的音量图标执行完全相同的操作

if application "System Preferences" is running then
    do shell script "killall 'System Preferences'"
    repeat until application "System Preferences" is not running
        delay 0.1
    end repeat
end if

tell application "System Preferences"
    reveal anchor "output" of pane id "com.apple.preference.sound"
end tell

tell application "System Events"
    repeat until checkbox "Show volume in menu bar" of window "Sound" of application process "System Preferences" exists
        delay 0.1
    end repeat
    click checkbox "Show volume in menu bar" of window "Sound" of application process "System Preferences"
end tell

tell application "System Preferences" to quit

【讨论】:

以上是关于在菜单栏中显示蓝牙/音量 Apple 脚本的主要内容,如果未能解决你的问题,请参考以下文章

在菜单栏中显示静态文本

检查 NSStatusItem 当前是不是显示在 OS X NSStatusBar 主菜单栏中

如何管理Mac电脑菜单栏中的图标

在android操作栏中的菜单项之间显示分隔符

mac蓝牙开机之后无法启动蓝牙怎么办

苹果电脑蓝牙误关闭怎么开启?