How to define an array with a single `defaults` command?

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?


Solution 1:

-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; } )'