Function keys (F1..F12) and "fn" in AppleScript

I’m trying to know how to simulate in AppleScript the pressing of the Function keys (F1…F12); also pressing the “fn” key

The problem is not sending the keystrokes, but the security entitlements to allow it.

This will work with conditions, SEE BELOW.

use scripting additions
-- F3 Key
tell application "System Events"
	key code 160 -- Show Mission Control
end tell


-- Space Key with Command Key
tell application "System Events"
	key code 49 using command down -- Show Spotlight Search
end tell

This will work in “Script Editor”, but only if you grant the application permission in “System Preferences → Security & Privacy → Privacy → Accessibility”.
Similarly you need to grant these same permissions for any other script you create, that’s run outside of the “Script Editor”, and also the correct entitlements in the “info.plist” and “myAppName.entitlements” file of any AppleScript application bundle you create.

It gets very complicated for app’s you create, as you also have to give entitlements to “System Events” as well as your own app.
You will have to search the interweb for the various entitlements required for your created app’s.
And there is also further entitlements needed for sandboxed app’s.

This link shows the various AppleScript key codes.

But depending on your MacOS version, you will have to look up the security permissions and entitlements needed to run this kind of “System Events” code.

Regards Mark


Go to Keyboard preferences and set the checkbox Use F1,F2,etc. as standard function keys.

Now run this example:

tell application "System Events"
	key down 63 -- holding Fn key
	repeat 5 times
		-- gradually decreaze the brightness
		key code 145 -- to increaze, use 144
	end repeat
	key up 63
end tell

NOTE: on my keyboard code 145 is F1 key, and code 144 is F2 key.

Thank you very much, KniazidisR
It has been very helpful :slight_smile:

Hi Mark FX.

Thank you very much for your help.


I’m lost… MacBook Air M1, Monterey 12.5.1 (21G83), “Use F1, F2 …as standard function keys” selected.

Pressing fn+F1 decreases the display brightness, fn+F2 increases the display brightness

I’ve installed F1 is seen as 122, F2 as 120, but neither work in the script

tell application "System Events"
key down 63
key code 122
key up 63
end tell


tell application "System Events" to key code 145

decreases the brightness

tell application "System Events" to key code 144

increases the brightness, both without key 63/fn

How did you get the key codes for your keyboard? What are the codes for the rest of the F keys.

I don’t want to appear smarter to anyone than I really am. I found these codes by dumb trial and error. The search was performed on the values closest to 122.

This is what I do whenever something documented doesn’t work, either because the documentation is outdated or Apple changes something without warning users. I confess that much of what relates to Apple products is a big mystery to me.

You can determine the codes for the rest of the F keys using dumb trial and error method. When some key code gives the desired effect, then it is what you need.

Here is other example I provided for Fn keys in the past:

Clicking “fn” and “enter” together

Thank you for the answer.
F1 is:

Testing some values I got
show desktop

tell application "System Events" to key code 103

Mission control
key code 160
key code 129
key code 177
key code 130
key code 131
And at this point I gave up.