Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

thevibesman

macrumors regular
Original poster
Oct 26, 2007
139
0
I'm trying to write a bash script to swap the command and control keys for when I'm doing a lot of emacs work. I know I can do this from the Keyboard/Mouse PreferencesPane, but it would be faster if I could just run a script.

I swapped the keys via the PreferecePane and then ran defaults -currentHost read -g to see what was changed by the PreferencePane. I then used that info to write the following bash script to swap the keys:

Code:
#!/bin/bash                                                                      

mappingplist=com.apple.keyboard.modifiermapping

if [ $1 == "emacs" ]; then
    echo "Switching to emacs modifiers"
    defaults -currentHost write -g $mappingplist '(                              
                {                                                                
            HIDKeyboardModifierMappingDst = 4;                                   
            HIDKeyboardModifierMappingSrc = 2; },                                
                {                                                                
            HIDKeyboardModifierMappingDst = 12;                                  
            HIDKeyboardModifierMappingSrc = 10;                                  
        },                                                                       
                {                                                                
            HIDKeyboardModifierMappingDst = 2;                                   
            HIDKeyboardModifierMappingSrc = 4;                                   
        },                                                                       
                {                                                                
            HIDKeyboardModifierMappingDst = 10;                                  
            HIDKeyboardModifierMappingSrc = 12;                                  
        })'


else
    echo "Switching to default modifiers"
    defaults -currentHost delete -g $mappingplist
fi

The script takes one argument, if the argument is emacs, then it swaps command and control, if the argument is anything else it restores the defaults. When I run the script it seems to make the appropriate changes when I run defaults -currentHost read -g, but there seems to be no effect (they keys behave the same as before the script was run and the changes are not reflected in the PreferencePane). Is there some sort of refresh I need to do to make these changes go into effect after running the script?
 
Were you able to solve your problem?

I tried (in OSX 10.7.4):
- killing Finder and/or SystemUIServer
- changing language or input source in hope to trigger some kind of reload
- opening /System/Library/CoreServices/{KeyboardSetupAssistant.app, Setup Assistant.app} in hope to trigger some kind of reload

Logging out and in again changes what is displayed in system settings but does not actually change the settings.

I just want to be able to automate the task of remapping the caps lock key and found this which references your post.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.