How to change Keyboard Shortcuts in System Preferences?

Hi, I’m a very new AppleScript user.

I’m trying to create a script that changes two keyboard shortcuts in the System Preferences to F9 and F10:

“Capture selection to file” (default is Apple-Shift-4, want to change it to F9)
“Capture selection to clipboard” (default is Apple-Control-Shift-4, want to change it to F10).

Here’s what I have so far, but the “set” commands are not working? Not sure what exactly I’m doing wrong, but I’m assuming I’m just missing something obvious?

tell application "System Preferences"
	set current pane to pane "com.apple.preference.keyboard"
	tell application "System Events" to tell process "System Preferences" to tell tab group 1 of window 1
       
		click radio button "Keyboard Shortcuts"
		set value of text field 2 of row 4 of outline 1 of scroll area 1 to "F9"
		set value of text field 2 of row 5 of outline 1 of scroll area 1 to "F10"
       
	end tell
end tell

Any help would be greatly appreciated, thank you!

When I try that in the pref pane itself, I get a warning that there’s another one somewhere.

Further testing shows that I can get the keys in text field 2 (in part - see below), but cannot set them that way (there’s nothing the matter with your script for getting a value, just setting it fails for me too).


tell application "System Preferences"
	set current pane to pane "com.apple.preference.keyboard"
	tell application "System Events" to tell process "System Preferences" to tell tab group 1 of window 1
		click radio button "Keyboard Shortcuts"
		set g to value of text field 2 of row 4 of outline 1 of scroll area 1 --to "F9"
		set h to value of text field 2 of row 5 of outline 1 of scroll area 1 --to "F10"
		-- g and h both show shift 4, but neither show command or control characters properly
	end tell
end tell

Jacques, thanks for your help!

Can anyone determine why the set commands I had were not working?