3

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?

  • The volume up/down keys are not accessible as 'keys' from applescript. See this for a way to achieve system volume control by Applescript - superuser.com/questions/224264/… – Tetsujin Dec 30 '14 at 15:52
  • @Tetsujin Hey, thanks for your reply. Is there really no way to trigger them as keys being pressed? It's not just the volume key I want to trigger. I want to trigger the pause key in the OS determined context (could pause VLC, iTunes etc.). – rikudesu Dec 30 '14 at 15:55
  • You can call 'true' F-Keys, but not their 'extra function' alternatives. keystroke "F12" will press F12 but not Fn/F12 [doesn't matter which way you have your system prefs set, it will send just F12, not volume up.] – Tetsujin Dec 30 '14 at 15:59
  • Alright thanks, is there any way to trigger the actions bound to the extra function keys? What does fn+f12 execute? Can I call this manually? – rikudesu Dec 30 '14 at 16:01
  • This is actually becoming an XY Problem What you are looking for is a way to control specific functions, volume, pause etc, not the key-presses required to trigger those functions - there are examples for iTunes on the link I posted. I'm not sure VLC can be controlled like iTunes when it is not the frontmost app, so the solutions may differ. – Tetsujin Dec 30 '14 at 16:53
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .