MacOS – Trigger function key using AppleScript

applescriptkeyboardmacos

I'm trying to trigger a function key using applescript. I need it to press the 'volume up' key with fn.

I wrote the following AppleScript but it isn't effective:

tell application "System Events"
    key down 63 -- fn
    key code 111 -- f12
    key up 63
end tell

just executing key code 111 gives me the same result as the code above,

 key down 63 

doesn't seem to effect the behaviour of the script, the f12 key is being pressed, without the function modifier.

Is there anything like

 using function down / using fn down

just like there is for the command key

 using command down

or is there any other way to achieve this?

Best Answer

if you are trying to set the volume in AppleScript a correct syntax would be as the following, no need to force button presses :

tell application "System Events"
set volume 4
end tell

Now as you can see its a forced process. If you want more flexibility like determining where the users volume is currently at or set it to increment increase/decrease by 1, yes it's possible, but a little more code heavy than i can manage right now (i'm typing from my iPhone)

However,

If at possible, and your end goal wouldn't be affected by using Automator. I would use Automator if I was you, it has built in functions like this with easy drag and drop motives. 'Set Computer Volume is one of them'. But thats an alternative, I just emulated that with the code above.