Skip to content

Instantly share code, notes, and snippets.

@paulera
Last active April 19, 2021 19:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulera/d6a89b769877666f269515501b252c5d to your computer and use it in GitHub Desktop.
Save paulera/d6a89b769877666f269515501b252c5d to your computer and use it in GitHub Desktop.
Script to toggle the FN Lock state in MacOS.
# toggle-fn.scpt
#
# This AppleScript toggles the FN key state.
#
# To assign a keyboard shortcut (long story short):
# Export this AppleScript to an app (a service to just run the script won't have accessibility privileges)
# ----> In the script editor, go to File -> Export, change format to "Application" and save. It will create a .app file.
# When you try to run for the first time, it will ask for access to Accessibility control. Allow it.
# Create a service in Automator to open the app.
# Create a keyboard shortcut to run the service created in Automator.
#
# Follow: @paulo_dev
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell application process "System Preferences"
get properties
delay 0.3
# Troubleshooting:
# Problem: Can’t get window "Keyboard" of application process "System Preferences"
# Solution: tweak the delay to a higher value
click radio button "Keyboard" of tab group 1 of window "Keyboard"
set theCheckbox to checkbox "Use F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
# Troubleshoot
# Problem: Can’t get window "Keyboard" of application process "System Preferences"
# Solution: fix the "click checkbox" using the exact text found in System Preferences -> Keyboard -> Keyboard (tab) for you MacOS version
click theCheckbox
tell theCheckbox
if (its value as boolean) then
display notification "Using standard F1, F2, F3, F4, ..."
else
display notification "FN Locked (special function keys activated)"
end if
delay 1 # necessary to keep the notification, otherwise it will die with the script
end tell
end tell
tell application "System Preferences" to quit
end tell
# --------------------------------------------------------------------
# Originally seen in http://forums.macrumors.com/threads/how-to-f-lock.313721/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment