Benches macOS

4

Keyboard Modifier toggle from command to …

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hi guys I have this script working after a bit of tweaking and the script was originally posted by Lance Ball long ago for Tiger and was for users with a Pc Keyboard. I am swapping control and command for pure gaming preferences; Unfortunately, The script will not work when the Keyboard Shortcuts menu has been opened last in system preferences, would anyone know how to slightly tweak this applescript so that it selects the keyboard button prior?

--
********************************************************************************

-- Utility script to switch keyboard mapping for Command and Control keys.

-- Author: Lance Ball & tweaked by Beau Parkes for Snow Leopard

--
********************************************************************************

-- Open System Preferences

tell application "System Preferences"

activate

set current pane to pane "com.apple.preference.keyboard"

end tell

tell application "System Events"

-- If we don't have UI Elements enabled, then nothing is really going to work.

if UI elements enabled then

tell application process "System Preferences"

get properties

-- Open up the Modifier Keys sheet

click button "Modifier Keys…" of tab group 1 of window "Keyboard"

tell sheet 1 of window "Keyboard"

-- get the text of the 3rd pop up button

click pop up button 5

click menu item "Apple Internal Keyboard / Trackpad" of menu 1 of pop up button 5

set commandKey to value of pop up button 3

-- looks like we're in default mode. Swap the keys

if commandKey ends with "Control" then

click pop up button 3

click menu item 4 of menu 1 of pop up button 3

click pop up button 1

click menu item 2 of menu 1 of pop up button 1

else

-- We're in PC keyboard mode. Swap back to the defaults

click button "Restore Defaults"

end if

-- close the sheet

click button "OK"

end tell

end tell

tell application "System Preferences" to quit

else

-- UI elements not enabled. Display an alert

tell application "System Preferences"

activate

set current pane to pane "com.apple.preference.universalaccess"

display dialog "UI element scripting is not enabled.

Check "Enable access for assistive devices""

end tell

end if

end tell

********************************************************************************

Thanks guys, Im excited to hear how I can improve the code.

Beau Parkes

4 replies

Sign in or join Beachball Help to reply.

  1. 0

    Pierre L.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi, Beau Parks. Welcome to this forum.

    The following version of your script (tested with Mac OS X 10.6.2) should do what you are asking for:

    --BEGINNING OF SCRIPT

    --Optional part:

    tell application "System Events" to set UI_enabled to UI elements enabled

    if not UI_enabled then

    display dialog "This script utilizes the built-in Graphic User Interface Scripting architecture of Mac OS X which is currently disabled." & return & return & "Do you want to activate GUI Scripting by selecting the checkbox “Enable access for assistive devices” in the Universal Access preference pane?" buttons {"Cancel", "OK"} default button 2 with icon 0

    tell application "System Preferences"

    activate

    reveal pane id "com.apple.preference.universalaccess"

    end tell

    return

    end if

    --Main part:

    tell application "System Preferences"

    reveal anchor "keyboardTab" of pane id "com.apple.preference.keyboard"

    tell application "System Events" to tell window "Keyboard" of process "System Preferences" -- GUI Scripting

    click button "Modifier Keys…" of tab group 1 -- open up the Modifier Keys sheet

    tell sheet 1

    click pop up button 5 -- the first button from the top

    click menu item "Apple Internal Keyboard / Trackpad" of menu 1 of pop up button 5

    if (value of pop up button 3 ends with "Control") then -- we're in default mode

    click pop up button 3 -- Control Key

    click menu item 4 of menu 1 of pop up button 3 -- Control Key --> Command Key

    click pop up button 1 -- Command Key

    click menu item 2 of menu 1 of pop up button 1 -- Command Key --> Control Key

    else -- we're in PC keyboard mode; swap back to the defaults

    click button "Restore Defaults"

    end if

    click button "OK" -- close the sheet

    end tell

    end tell

    quit

    end tell

    --END OF SCRIPT

    Message was edited by: Pierre L. (I removed a link to a web page)

  2. 0

    Beau Parkes

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi Thank you for welcoming me to the thread!

    Thats script was amazing, not only did it fix my problem but it is much faster than my original script I was using. Thank you many times over!

    I hope to learn more about apple scripting as I am still very novice. You've opened my eyes and saved me many more hours of tedious prodding. I would love to thank you more but Im too busy now implementing my new script, :D:D Cheers!!

  3. 0

    Pierre L.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    It was my pleasure. (I apologize for having misspelled your name in my previous post.)

  4. 0

    Pierre L.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Oops… You could still improve the script by adding just one short line of code at the beginning of the main part, like this:

    …

    *reveal anchor "keyboardTab" of pane id "com.apple.preference.keyboard"*

    activate -- to keep the Dock icon from bouncing

    *tell application "System Events" to tell window "Keyboard" of process "System Preferences"*

    …

    Better late than never!