4

I want a domain com.apple.keyboard.modifiermapping.1452-628-0 to contain the following array:

(
        {
        HIDKeyboardModifierMappingDst = 2;
        HIDKeyboardModifierMappingSrc = 0;
    },
        {
        HIDKeyboardModifierMappingDst = 0;
        HIDKeyboardModifierMappingSrc = 2;
    },
        {
        HIDKeyboardModifierMappingDst = 0;
        HIDKeyboardModifierMappingSrc = 10;
    }
)

I can achieve it by deleting the current value and running three -array-add commands:

defaults -currentHost delete -g com.apple.keyboard.modifiermapping.1452-628-0
defaults -currentHost write -g com.apple.keyboard.modifiermapping.1452-628-0 -array-add '{ HIDKeyboardModifierMappingDst = 2; HIDKeyboardModifierMappingSrc = 0; }'    
defaults -currentHost write -g com.apple.keyboard.modifiermapping.1452-628-0 -array-add '{ HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 2; }'    
defaults -currentHost write -g com.apple.keyboard.modifiermapping.1452-628-0 -array-add '{ HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 10; }'
defaults -currentHost read -g com.apple.keyboard.modifiermapping.1452-628-0

What syntax can I use to define the array in a single command?


  • If I run the following script:

    defaults -currentHost delete -g com.apple.keyboard.modifiermapping.1452-628-0
    defaults -currentHost write -g com.apple.keyboard.modifiermapping.1452-628-0 -array '(
      { HIDKeyboardModifierMappingDst = 2; HIDKeyboardModifierMappingSrc = 0; },
      { HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 2; },
      { HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 10; } )' 
    defaults -currentHost read -g com.apple.keyboard.modifiermapping.1452-628-0
    

    I get an array within an array:

    (
            (
                    {
                HIDKeyboardModifierMappingDst = 2;
                HIDKeyboardModifierMappingSrc = 0;
            },
                    {
                HIDKeyboardModifierMappingDst = 0;
                HIDKeyboardModifierMappingSrc = 2;
            },
                    {
                HIDKeyboardModifierMappingDst = 0;
                HIDKeyboardModifierMappingSrc = 10;
            }
        )
    )
    
  • If I try with -plist instead of -array I get an Unexpected argument.

  • If I run the following script:

    defaults -currentHost delete -g com.apple.keyboard.modifiermapping.1452-628-0    
    defaults -currentHost write -g com.apple.keyboard.modifiermapping.1452-628-0 -array '
      { HIDKeyboardModifierMappingDst = 2; HIDKeyboardModifierMappingSrc = 0; },
      { HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 2; },
      { HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 10; }'
    defaults -currentHost read -g com.apple.keyboard.modifiermapping.1452-628-0
    

    I get a syntax error defaults[22044:679428] Could not parse: ... Try single-quoting it.


Question again: what syntax can I use to define the array in a single command?

1 Answer 1

4

-array takes individual array elements as individual arguments.

defaults -currentHost write -g com.apple.keyboard.modifiermapping.1452-628-0 -array \
  '{ HIDKeyboardModifierMappingDst = 2; HIDKeyboardModifierMappingSrc = 0; }' \
  '{ HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 2; }' \
  '{ HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 10; }'

The ‘\’ are used to keep the shell waiting for the next line, so they can be omitted if written on one line. You don't need to use delete first: unlike -array-add, -array replaces the entire array with the new contents.

If you don't want to do it this way, here's an alternative: you can pass the data structure as a string. This is closer to the examples in your question and works the same.

defaults -currentHost write -g com.apple.keyboard.modifiermapping.1452-628-0 '(
  { HIDKeyboardModifierMappingDst = 2; HIDKeyboardModifierMappingSrc = 0; },
  { HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 2; },
  { HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 10; } )' 
3
  • This answers my question perfectly. However, shall I ask another question of how to do it with a single argument? My intention was to provide a single data structure, and when writing the question, I did not realise "a single command" and "a single data structure" were requirements that could be fulfilled in different ways.
    – techraf
    Dec 29, 2016 at 12:45
  • 1
    @techraf I've edited my answer to include a single-string method, see if that's better
    – grg
    Dec 29, 2016 at 12:50
  • Oh! Now I get it. -array and -dict are optional. And I tried to convince it with non-existent -plist... Thank you!
    – techraf
    Dec 29, 2016 at 12:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .