This is the new improved script which does not mess with your caps lock and control keyboard settings. Once again the source is right here and the file is attached! Have Fun.
Code:
-- GUI Scripting Demonstration
--
-- Modify Keyboard System Preferences to swap the Option (Alt) and Command (Windows) keys
-- so their placement on the keyboard matches that of the standard Macintosh keyboard. This
-- will also allow us to change the preferences back so the keys go back to normal
-- when the PC keyboard is disconnected.
on setKeyboard(keyboardType)
tell application "System Events"
*get properties
*tell process "System Preferences"
* -- click "Modifier Keys..." at bottom of Keyboard tab
* click button "Modifier Keys…" of tab group 1 of window "Keyboard & Mouse"
*
* if keyboardType is equal to "Macintosh" then
* *-- click the pop up button menu "Command", this menu does not exist until it is clicked in the GUI
* *click pop up button 3 of sheet 1 of window "Keyboard & Mouse"
* *-- click "Option" of the pop up menu
* *click menu item 3 of menu 1 of pop up button 3 of sheet 1 of window "Keyboard & Mouse"
* *
* *-- delay briefly
* *delay 1
* *
* *-- click the pop up button menu "Option", this menu does not exist until it is clicked in the GUI
* *click pop up button 4 of sheet 1 of window "Keyboard & Mouse"
* *-- click "Command" of the pop up menu
* *click menu item 4 of menu 1 of pop up button 4 of sheet 1 of window "Keyboard & Mouse"
* end if
*
* if keyboardType is equal to "PC" then
* *-- click the pop up button menu "Option", this menu does not exist until it is clicked in the GUI
* *click pop up button 3 of sheet 1 of window "Keyboard & Mouse"
* *-- click "Command" of the pop up menu
* *click menu item 4 of menu 1 of pop up button 3 of sheet 1 of window "Keyboard & Mouse"
* *
* *-- delay briefly
* *delay 1
* *
* *-- click the pop up button menu "Command", this menu does not exist until it is clicked in the GUI
* *click pop up button 4 of sheet 1 of window "Keyboard & Mouse"
* *-- click "Option" of the pop up menu
* *click menu item 3 of menu 1 of pop up button 4 of sheet 1 of window "Keyboard & Mouse"
* end if
*
* -- click "OK" to dismiss the sheet
* click button "OK" of sheet 1 of window "Keyboard & Mouse"
*end tell
end tell
end setKeyboard
-- main script; preset desiredKeyboard to "Macintosh"
set desiredKeyboard to "Macintosh"
-- get users input; we're waiting until selection
display dialog "Which keyboard type?" buttons {"Macintosh", "PC", "Cancel"} with icon caution default button 3 cancel button 3 with title "iKeyboard"
-- set desiredKeyboard to "PC" if they picked "PC"
if button returned of result = "PC" then
set desiredKeyboard to "PC"
end if
-- activate System Preferences
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell
-- call subroutine with desired keyboard type
setKeyboard(desiredKeyboard)
-- quit from system preferences
tell application "System Preferences"
quit
end tell