Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript Keystroke FN+CMD and function Key

all I am trying to do is produce a script to press 3 keys at once,


This is what I have done so far but where key code 120 is F2 this does not appear to happen?


Matt


tell application "System Events" to key code {120, 63} using command down

Posted on Nov 20, 2015 4:00 AM

Reply
14 replies

Nov 21, 2015 12:38 PM in response to MattJayC

perhaps. the using supplies the modifiers, so fn would need to be in the using. I haven't tested this.

tell application "System Events" to key code {120} using { fn down, command down}

name define here:

http://macbiblioblog.blogspot.com/2014/12/key-codes-for-function-and-special-key s.html



Some what curious that you would have to write two logic paths based on what type of keyboard you used. Perhaps you do not need the fn. What happens in the real usage? Not just displaying what you typed.

Nov 21, 2015 2:38 PM in response to MattJayC

Hi


"using fn down" isn't part of System Events' AppleScript dictionary, and so will always give you a syntax error. (Same with "using function down".) You can script command, option, shift, control and combinations thereof, but not the function key.


On my 2012 MacBook Air with a UK English keyboard, I can increase and decrease screen brightness using the following scripts:


tell application "System Events" to key code 107 -- decreases brightness


tell application "System Events" to key code 113 -- increases brightness


It may not work for everyone, and it may not be what you actually want to do. But if you can tell us why you want to achieve by pressing that key combination, there may be other ways.


There is a very useful page here which goes into a lot of detail about key code scripting - definitely worth reading. (It's where I got the key codes for screen brightness.)


One of the points the guy makes is that trying to script the function keys is unreliable and not always possible.


Cheers,


H

Nov 24, 2015 4:50 AM in response to MattJayC

Hello


You may try key code 144. (Not tested, for I don't have necessary environment)



-- toggle target display mode (?) tell application "System Events" key code 144 using {command down} end tell




As far as I know, this key code is not documented. I found it by searching web via Google with the following keywords:


"OS X" "Target Display Mode" "key code"



cf.

Sending key via ssh or using mouse to activate Target Display Mode?

http://superuser.com/questions/540799/sending-key-via-ssh-or-using-mouse-to-acti vate-target-display-mode


how can I script a shortcut for command-F2 (target display mode)?

https://discussions.apple.com/thread/5953084



Good luck,

H

Nov 25, 2015 4:44 AM in response to Hiroto

That works for me,


However I am trying to run this through shell script so I have this



ssh StudioServer@192.168.2.4

osascript -e 'tell application "System Events" to key code 144 using {command down}'


Ran through terminal this works, how can I get to be a one liner? I've also shared the password so password is not required.


Matt

Nov 26, 2015 1:14 AM in response to MattJayC

You will have to add an extra layer of quoted string protection because the current shell is going to strip the '...' pair, then send it to the remote system where a 2nd shell is going to see

osascript -e tell application "System Events" to key code 144 using {command down}

Without any quotes around the tell... statement. And that is going to blow up the osascript command on the remote Mac.


Borrowing from Hiroto's example, and modifying

ssh StudioServer@192.168.2.4 osascript -e \'tell application \"System Events\" to key code 144 using {command down}\'

protect the quotes on the local system from the shell, and then the remote system should see

osascript -e 'tell application "System Events" to key code 144 using {command down}'

with all the quotes preserved.


NOTE: Getting through multiple layers of shell command lines and having the quoting be correct when the command is finally executed, can be tricky, and often times requires lots of experiments. I think I have a good example, but I could be wrong.

Nov 26, 2015 1:13 AM in response to BobHarris

Oops. Thank you for correction! I should have tested the code in my environment...


Now I have confirmed the following variants should work as well.



ssh user@host osascript -e "'tell application \"System Events\" to key code 144 using {command down}'"



ssh user@host osascript <<'EOF' tell application "System Events" to key code 144 using {command down} EOF




All the best,

H

Applescript Keystroke FN+CMD and function Key

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.