Activating Exposé via Applescript

On a standard keyboard layout, I can rewrite the screen by activating and then un-activating Exposé this way:

delay 0.5
tell application "System Events"
	key code 109
	delay 0.3
	key code 109
end tell

(without the initial delay, this doesn’t work, for reasons I can’t figure out).

But of course, F10 (key code 109) may not be the Exposé application key on other systems, so I see that I can do this to determine what the Exposé application key is:

tell application "System Events"
	tell expose preferences
		set fKey to (get function key of application windows shortcut) as string
		set fKeyMod to (get function key modifiers of application windows shortcut) as string
	end tell
end tell

On my system this returns “F10” and nothing. My question is: how can I convert what I get from expose preferences into key codes that I can use to run Exposé on another system.

Thanks again for help.

I guess you will be most interested in the key code table posted in a German AppleScript forum:

See the first post in this thread

Using the information given in the table you can easily convert between key codes and F codes:


set kcode to my getkcode("F13")

on getkcode(fcode)
	set fcodes to {"F10", "F12", "F13"}
	set kcodes to {109, 111, 105}
	
	set kcode to missing value
	if fcode is in fcodes then
		set countfcodes to length of fcodes
		repeat with i from 1 to countfcodes
			if fcode is equal to item i of fcodes then
				set kcode to item i of kcodes
				exit repeat
			end if
		end repeat
	end if
	return kcode
end getkcode

That works perfectly, thank you!

I now see that this also works, without keycodes:

tell application id "com.apple.exposelauncher" to launch
delay 0.1
tell application id "com.apple.exposelauncher" to launch

I’m not sure if there’s any disadvantage to this one, but it does seem simpler.

From Apple’s AppleScript website: