0

系统

M1 MacBook Pro

MacOS 大苏尔

问题

我喜欢日常使用的默认 Mac 触控栏布局,但我更喜欢在编程时触手可及的 F1-F12 键。我也不喜欢按住 fn 键。这就是为什么我写了两个 AppleScripts 来切换布局(包括在下面)。

脚本工作,但他们是错误的。这是因为他们依赖于打开系统偏好设置应用程序并在菜单中导航。我用 Automator 制作了几个“应用程序”,它们简单地运行脚本,然后将它们分配给键盘快捷键。

这是一个不错的解决方案,但我想做一些更优雅的事情。理想情况下,我的脚本应该在幕后运行并立即更改触摸栏布局,而不是打开系统偏好设置,从下拉列表中选择项目,然后最后关闭系统偏好设置。

在求助于使用 Automator 之前,我在 shell 上搞砸了很长一段时间都没有成功。那些对事情更精通的人有什么建议吗?

代码

这使得 F1-F12 键成为默认的触摸栏布局:

tell application "System Preferences"
    set the current pane to pane id "com.apple.preference.keyboard"
    delay 0.25
    reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell

tell application "System Events"
    tell process "System Preferences"
        click pop up button 2 of tab group 1 of window "Keyboard"
    end tell
end tell

tell application "System Events"
    tell process "System Preferences"
        click menu item "F1, F2, etc. Keys" of menu 1 of pop up button 2 of tab group 1 of window "Keyboard"
    end tell
end tell

tell application "System Events"
    tell process "System Preferences"
        click pop up button 4 of tab group 1 of window "Keyboard"
    end tell
end tell

tell application "System Events"
    tell process "System Preferences"
        click menu item "Show App Controls" of menu 1 of pop up button 4 of tab group 1 of window "Keyboard"
    end tell
end tell

quit application "System Preferences"

而这个则相反(使应用程序控制默认的触摸栏布局):

tell application "System Preferences"
    set the current pane to pane id "com.apple.preference.keyboard"
    delay 0.25
    reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell


tell application "System Events"
    tell process "System Preferences"
        click pop up button 2 of tab group 1 of window "Keyboard"
    end tell
end tell

tell application "System Events"
    tell process "System Preferences"
        click menu item "App Controls" of menu 1 of pop up button 2 of tab group 1 of window "Keyboard"
    end tell
end tell

tell application "System Events"
    tell process "System Preferences"
        click pop up button 4 of tab group 1 of window "Keyboard"
    end tell
end tell

tell application "System Events"
    tell process "System Preferences"
        click menu item "Show F1, F2, etc. Keys" of menu 1 of pop up button 4 of tab group 1 of window "Keyboard"
    end tell
end tell

quit application "System Preferences"
4

1 回答 1

1

macOS Catalina上,当切换目标设置时,它会将从to更改为,反之亦然。但是,当它在UI中更改它时使用命令以编程方式切换它,它不会在Touch Bar上更改它,而无需重新启动ControlStrip进程PresentationModeGlobal ~/Library/Preferences/com.apple.touchbar.agent.plistappWithControlStripfunctionKeysdefaults

以下示例 shell 脚本 代码是我使用单个键盘快捷键在Show App ControlsF1、F2 等之间切换的代码,因为它们分别在我的系统上的System Preferences > Keyboard > Keyboard中设置。

示例 shell 脚本 代码

#!/bin/zsh

pmg="$(defaults read com.apple.touchbar.agent 'PresentationModeGlobal')"

if [[ $pmg == "appWithControlStrip" ]]; then
    defaults write com.apple.touchbar.agent 'PresentationModeGlobal' 'functionKeys'
    killall "ControlStrip"
else
    defaults write com.apple.touchbar.agent 'PresentationModeGlobal' 'appWithControlStrip'
    killall 'ControlStrip'
fi

笔记:

其他版本的macOS可能需要更改其他设置和/或重新启动其他进程pkill 'Touch Bar agent',例如,如果适用的话。

终端中,您可以使用命令的read 动词defaults检查com.apple.touchbar.agent.plist您在UI中所做的更改,以查看是否也需要更改其他键的值。




边注

至于您的AppleScript 代码,我将根据您的代码作为单个脚本来在两个选项之间切换。

你没有说你运行的是什么版本的macOS,因为我没有弹出按钮 4我无法测试下面显示的示例 AppleScript 代码,但是,这应该消除两个单独的脚本,它只是在两者之间切换一个键盘快捷键

示例 AppleScript 代码

tell application "System Preferences"
    set the current pane to pane id "com.apple.preference.keyboard"
    delay 0.25
    reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell

tell application "System Events"
    tell tab group 1 of window 1 of process "System Preferences"
        if value of pop up button 2 is "Show App Controls" then
            click pop up button 2
            click menu item "F1, F2, etc. Keys" of menu 1 of pop up button 2
            click pop up button 4
            click menu item "Show App Controls" of menu 1 of pop up button 4
        else
            click pop up button 2
            click menu item "Show App Controls" of menu 1 of pop up button 2
            click pop up button 4
            click menu item "F1, F2, etc. Keys" of menu 1 of pop up button 4
        end if
    end tell
end tell

quit application "System Preferences"


注意:示例 AppleScript 代码就是这样,并且没有任何包含的错误处理,不包含任何可能适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript Language Guide中的try 语句error 语句。另请参阅处理错误。此外,在适当的情况下,可能需要在事件之间使用延迟命令,例如,使用延迟 delay 0.5适当设置。

于 2021-09-25T02:19:15.747 回答