Toggle F-key setting in Preference Pane

I own a new iMac with the slim keyboard, but miss the normal f-key functions that I’m used to.
You can click “Use all F1, F2, etc. keys as standard function keys” to toggle between this function.
Here’s my first applescript, patchworked by the the fine material I found on this site.

tell application "System Preferences" to set current pane to pane "com.apple.preference.keyboard"
tell application "System Events"
	if UI elements enabled then
		tell tab group 1 of window 1 of process "System Preferences"
			click radio button "Keyboard"
			click checkbox "Use all F1, F2, etc. keys as standard function keys"
		end tell
	else
		tell application "System Preferences"
			activate
			set current pane to pane "com.apple.preference.universalaccess"
			display dialog "To perform this task, I need you to click the \"Enable access for assistive devices\" at the bottom of the Universal Access Pane" buttons {"OK"}
		end tell
	end if
end tell

The resulting application can be put on the doc for use or assigned to a keyboard shortcut using a third-party app.

Any comments welcome!

Model: iMac 24inch 2.0MHZ
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hello and welcome.

Nice little script, works fine :).
Note: There is a shortcut to reveal the tabs (called anchors) directly,
and with GUI scripting I normally use the index numbers instead of the names of the UI elements,
to make the script also working for non-english systems.

tell application "System Preferences" to reveal anchor "keyboardTab" of pane id "com.apple.preference.keyboard"
tell application "System Events"
	if UI elements enabled then
		click checkbox 1 of tab group 1 of window 1 of process "System Preferences"
	else
		tell application "System Preferences"
			activate
			set current pane to pane "com.apple.preference.universalaccess"
			display dialog "To perform this task, I need you to click the \"Enable access for assistive devices\" at the bottom of the Universal Access Pane" buttons {"OK"}
		end tell
	end if
end tell

You can accomplish the same thing (in Tiger) with this line:

tell application "System Events" to set (UI elements enabled) to true

You will be asked for a password.

Thanks Adam.
Yes, I had a version that used that instead but didnt like the password request.
I felt that If I had to turn UI elements on from within the script by:

tell application "System Events" to set (UI elements enabled) to true

Then I’d have to turn it off again after I’d finished, and then of course be asked for the password TWICE.
So I left it as is.
Good trick tho.

Cheers, Nik

Thanks StefanK for the ammendments.
That seems a more elegant way of wording it, but of course as a complete beginner, I was pleased to just make it work.
You’ve made it much more friendly for other languages.
Merci viel mal!
Thanks a lot, Nik