--Check if GUI Scripting is Enabled
tell application "System Events"
if not UI elements enabled then
set UI elements enabled to true
end if
end tell
--Enable/Disable "Use all F1, F2, etc. keys as standard function keys" option in Keyboard & Mouse Preference pane and close System Preferences
tell application "System Events"
tell application "System Preferences"
reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
click checkbox 1 of tab group 1 of window 1 of application process "System Preferences"
end tell
if application "System Preferences" is running then
tell application "System Preferences" to quit
end if
-- Apple Script (i.e. Use in Apple's Script Editor Application) to Toggle Function Keys / Media keys on/off
-- Tested on MacOS Big Sur (11.4) June 2021
-- Project Path: https://github.com/MrSimonC/Toggle-Mac-Function-Keys
tell application "System Preferences"
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
if UI elements enabled then
tell application process "System Preferences"
repeat until exists tab group 1 of window "Keyboard"
delay 0.5
end repeat
click radio button "Keyboard" of tab group 1 of window "Keyboard"
click checkbox "Use F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
end tell
tell application "System Preferences" to quit
else
-- GUI scripting not enabled. Display an alert
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.security"
display dialog "UI element scripting is not enabled. Please activate this app under Privacy -> Accessibility so it can access the settings it needs."
end tell
end if
end tell
osascript <<EOD
-- Apple Script (i.e. Use in Apple's Script Editor Application) to Toggle Function Keys / Media keys on/off
-- Tested on MacOS Monterey (12.6.2) July 2023, MacOS Ventura (13.4.1) July 2023, MacOS Sonoma Preview (14.0) July 2023
-- Project Path: https://github.com/MrSimonC/Toggle-Mac-Function-Keys
set osver to system version of (system info)
if osver ≥ 13.0 then
open location "x-apple.systempreferences:com.apple.Keyboard-Settings.extension"
tell application "System Events" to tell process "System Settings"
# example window title: "Keyboard – 86%", so "begins with" is needed
repeat until window begins with "Keyboard" exists
end repeat
# wait until Keyboard window is the main window of the application and is accessible
repeat until exists of (1st window whose value of attribute "AXMain" is true)
end repeat
# wait until the group is displayed (needed else fails on Apple M2 Pro)
repeat until exists group 1 of group 2 of splitter group 1 of group 1 of window 1
end repeat
# "Keyboard Shortcuts..." Button
set keyboardButton to 1
# in MacOS 14 Sonoma, button 1 & 2 are assigned to keyboard brightness
if osver ≥ 14.0 then
set keyboardButton to 3
end if
click button keyboardButton of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
repeat until sheet 1 of window 1 exists
end repeat
# Click Function Keys
keystroke "f"
repeat until checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1 exists
end repeat
click checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1
# "Done" Button - Close the sheet so the application can quit
click button 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1
# Attempting to check the sheet at a certain point while closing will throw an error
# In that case, the outer repeat will try again
repeat
try
repeat while sheet 1 of window 1 exists
end repeat
exit repeat
end try
end repeat
end tell
tell application "System Settings" to quit
else if osver < 13.0 then
-- Below for MacOS Monterey and below
tell application "System Settings"
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
if UI elements enabled then
tell application process "System Preferences"
repeat until exists tab group 1 of window "Keyboard"
delay 0.5
end repeat
click radio button "Keyboard" of tab group 1 of window "Keyboard"
try
click checkbox "Use F1, F2, etc. keys as standard function keys on external keyboards" of tab group 1 of window "Keyboard"
end try
try
click checkbox "Use F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
end try
end tell
tell application "System Settings" to quit
else
-- GUI scripting not enabled. Display an alert
tell application "System Settings"
activate
set current pane to pane "com.apple.preference.security"
display dialog "UI element scripting is not enabled. Please activate this app under Privacy -> Accessibility so it can access the settings it needs."
end tell
end if
end tell
end if
EOD
#!/bin/bash
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Toggle Fn
# @raycast.mode silent
# Optional parameters:
# @raycast.icon 📟
# Documentation:
# @raycast.description Toggle Fn Keys
# @raycast.author smthng
# @raycast.authorURL https://github.com/smthng
# @raycast.packageName System
osascript <<EOD
-- Apple Script (i.e. Use in Apple's Script Editor Application) to Toggle Function Keys / Media keys on/off
-- Tested on MacOS Monterey (12.6.2) July 2023, MacOS Ventura (13.4.1) July 2023, MacOS Sonoma Preview (14.0) July 2023
-- Project Path: https://github.com/MrSimonC/Toggle-Mac-Function-Keys
set osver to system version of (system info)
if osver ≥ 13.0 then
open location "x-apple.systempreferences:com.apple.Keyboard-Settings.extension"
tell application "System Events" to tell process "System Settings"
# example window title: "Keyboard – 86%", so "begins with" is needed
repeat until window begins with "Keyboard" exists
end repeat
# wait until Keyboard window is the main window of the application and is accessible
repeat until exists of (1st window whose value of attribute "AXMain" is true)
end repeat
# wait until the group is displayed (needed else fails on Apple M2 Pro)
repeat until exists group 1 of group 2 of splitter group 1 of group 1 of window 1
end repeat
# "Keyboard Shortcuts..." Button
set keyboardButton to 1
# in MacOS 14 Sonoma, button 1 & 2 are assigned to keyboard brightness
if osver ≥ 14.0 then
set keyboardButton to 3
end if
click button keyboardButton of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
repeat until sheet 1 of window 1 exists
end repeat
# Click Function Keys
keystroke "f"
repeat until checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1 exists
end repeat
click checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1
# "Done" Button - Close the sheet so the application can quit
click button 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1
# Attempting to check the sheet at a certain point while closing will throw an error
# In that case, the outer repeat will try again
repeat
try
repeat while sheet 1 of window 1 exists
end repeat
exit repeat
end try
end repeat
end tell
tell application "System Settings" to quit
else if osver < 13.0 then
-- Below for MacOS Monterey and below
tell application "System Settings"
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
if UI elements enabled then
tell application process "System Preferences"
repeat until exists tab group 1 of window "Keyboard"
delay 0.5
end repeat
click radio button "Keyboard" of tab group 1 of window "Keyboard"
try
click checkbox "Use F1, F2, etc. keys as standard function keys on external keyboards" of tab group 1 of window "Keyboard"
end try
try
click checkbox "Use F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
end try
end tell
tell application "System Settings" to quit
else
-- GUI scripting not enabled. Display an alert
tell application "System Settings"
activate
set current pane to pane "com.apple.preference.security"
display dialog "UI element scripting is not enabled. Please activate this app under Privacy -> Accessibility so it can access the settings it needs."
end tell
end if
end tell
end if
EOD
1.将其作为脚本命令导入光线投射 1.运行它使用名称“切换fn”上的光线投射 注意:为了让raycast能够运行需要辅助权限的shell命令,您必须首先通过system settings > privacy and security > accessibility给予对raycast的辅助权限
5条答案
按热度按时间0mkxixxg1#
一个AppleScript应该可以完成这个任务--取自http://scriptbuilders.net/files/fn1.1.html,稍作修改
在MacOS 10.6.4上测试
o8x7eapl2#
命令是
defaults write -g com.apple.keyboard.fnState
,尽管我过去在更改它时遇到过问题。最后我只使用了一个AppleScript。给予一个尝试。编辑
详细地说,我遇到的问题是,实际值被更改,但它不会主动更改系统偏好设置中的设置,也不会切换fnState,因为文件只在 Boot /login等时读取。此外,对由另一个任务打开的配置文件进行更改听起来是损坏文件的好方法。
ruyhziif3#
你可以安装awsome Karabiner-Elements。
在"系统偏好设置“->”键盘偏好设置“下,确保“使用所有F1,F2等。键作为标准功能键”被选中作为一个额外条件。
e0bqpujr4#
对于其他任何人试图使这个工作-我终于得到了我的解决方案的工作。测试对象:MacOS Big Sur,11.4,2021年6月。
代码基于这里:https://github.com/MrSimonC/Toggle-Mac-Function-Keys
但为了简洁起见,以下是apple脚本文件的内容:
希望有人觉得有用!
fbcarpbf5#
感谢@simonc的回答,很容易地使用它与光线投射。
如果有人想通过终端运行功能:
1.保存下面的脚本作为一个sh文件,例如:toggle-fn.sh
1.使用
sh toggle-fn.sh
在终端上运行或者,如果有人正在使用光线投射,并希望通过其命令功能实现此功能:
1.保存下面的脚本作为一个sh文件,例如:toggle-fn.sh
1.将其作为脚本命令导入光线投射
1.运行它使用名称“切换fn”上的光线投射
注意:为了让raycast能够运行需要辅助权限的shell命令,您必须首先通过
system settings > privacy and security > accessibility
给予对raycast的辅助权限