4

I need to programmatically change the values of the Caps Lock, Control, Option and Command keys in "System Preferences > Keyboard > Modifier Keys..."

I dont want to use AppleScript.

Could someone point me in the right direction?

Pétur Ingi Egilsson
  • 4,368
  • 5
  • 44
  • 72

3 Answers3

3

Here is apple script capsLockOff.scpt:

tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.keyboard"
end tell

tell application "System Events"

    tell application process "System Preferences"
        get properties

        click button "Modifier Keys…" of tab group 1 of window "Keyboard"
        tell sheet 1 of window "Keyboard"
            click pop up button 4
            click menu item "No Action" of menu 1 of pop up button 4
            delay 1
            click button "OK"

        end tell
    end tell
end tell

tell application "System Preferences" to quit

Following is cocoa code to call above script. Hope this will help

-(void)runAppleScript{

    NSString *fileName = @"capsLockOff";

    NSString* path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"scpt"];
    NSURL* url = [NSURL fileURLWithPath:path];NSDictionary* errors = [NSDictionary dictionary];
    NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
    [appleScript executeAndReturnError:nil];
    [appleScript release];
}
j0k
  • 22,600
  • 28
  • 79
  • 90
2

See my answer in Does anyone know where OSX stores the settings in System Preferences > Keyboard > Modifier Keys?. The setting is in ~/Library/Preferences/ByHost/.GlobalPreferences.<UUID>.plist.

Community
  • 1
  • 1
Ted Middleton
  • 6,859
  • 10
  • 51
  • 71
1

As I suggested in comments :

I think if you can do it through AppleScript, then you can execute same commands via cocoa code ;)

You can refer this document to do so: Using AppleScript Scripts in Cocoa Applications

Hope this helps :)

Devarshi
  • 16,440
  • 13
  • 72
  • 125