Applescript Function Key

Joined
Dec 17, 2013
Messages
1
Reaction score
0
Points
1
Hello everyone,

I'm not exactly sure which sub-forum to put this in so I hope this is okay.

I was curious how I could program my mac to keystroke Fn+F8. I know that F8 is key code 100 but I can't figure out how to get the script to also press Fn.

I'm trying to make a script that sends this keystroke so that I can use Quicksilver to activate this script when I press option+j. Essentially, I'm trying to Pause/Play VLC by pressing option+J.

Thanks so much in advance!! :)
 
Joined
Oct 1, 2007
Messages
7,163
Reaction score
275
Points
83
Location
UK
Your Mac's Specs
Mac Mini i5 (2014 High Sierra), iPhone X, Apple Watch, iPad Pro 12.9, AppleTV (4)
Fastscripts lets you assign function keys to Applescripts

Although your example of VLC seems strange to me as it already has it's own keyboard shortcuts.

You can just tap the space bar for Play/Pause for example
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
If your scripting key combinations frequently and need to get codes, maybe a little app like "Key Codes" on the App Store will help. That said, I just tried it and get 100 for Fn-F8. You shouldn't need to push function though - the play/pause button should work as normal in VLC.
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
You can send keystrokes with Applescript using System Events, but you can't capture keystrokes with it, I've given some examples below.

Code:
tell application "System Events"
    keystroke "n" using command down
end tell
Sends the "n" key to the frontmost app, with command key is down.

Code:
tell application "System Events"
    keystroke "Apple" using option down
end tell
Types the word "Apple" to the frontmost app, with option key down.

Code:
tell application "System Events"
    keystroke "n" using {command down, shift down}
end tell
Sends the "n" key to the frontmost app, with both command and option keys down.

The alternative is to use the System Events "key code" commands like this.

Code:
tell application "System Events"
    key code 123
end tell
Sends the left arrow key to the frontmost app.

Code:
tell application "TextEdit" to activate
tell application "System Events"
    keystroke "n" using command down
    keystroke "Apple"
    key code 123
    key code {123, 123, 123} using shift down
    delay (3.0)
    key code 51
end tell
This script starts TextEdit and starts a new document, then types the word "Apple" into the document, then moves back one character and selects the previous three characters, then pauses for three seconds, then deletes these middle three characters from the word "Apple".

So to solve your problem using vanilla Applescript, find out the key codes for the various keys you wish to process, and use as shown above.
One word of caution though, key code numbers vary based on the keyboard settings and layout, and also may vary with the nationality settings of the keyboard, so the examples above assume a U.S. English (QWERTY) keyboard.

Hope this is of some help.

Regards Mark
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top