系统
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"