Skip to content

Instantly share code, notes, and snippets.

@whitehat101
Last active September 3, 2017 10:24
Show Gist options
  • Save whitehat101/8a1c5b4c1dc1f8204c95f36cb94c783d to your computer and use it in GitHub Desktop.
Save whitehat101/8a1c5b4c1dc1f8204c95f36cb94c783d to your computer and use it in GitHub Desktop.
Attempt to update ModifierMapping via Command Line
#!/bin/bash
#
# WARNING
#
# This script does not "work". It does correctly upate the preferences (good syntax).
# However, those settings are not reloaded until the user logs in again.
# They do NOT apply to the current session, and I haven't been able to find anything that
# can reload these values, other than AppleScript (with Accessability) simulating user input
# to the System Preferences application.
#
# Since this script can't achieve its purpose, I've left it unfinished, with considerable debug code.
# To Get IDs
# ioreg -n IOHIDKeyboard -r | grep -e 'class IOHIDKeyboard' -e VendorID\" -e ProductID\"
VendorID=16700
ProductID=8455
KEY=com.apple.keyboard.modifiermapping.$VendorID-$ProductID-0
defaults -currentHost read -g $KEY
MacStyle=$(tr -d '[:space:]' <<PLIST
<array>
<dict>
<key>HIDKeyboardModifierMappingDst</key>
<integer>4</integer>
<key>HIDKeyboardModifierMappingSrc</key>
<integer>3</integer>
</dict>
<dict>
<key>HIDKeyboardModifierMappingDst</key>
<integer>12</integer>
<key>HIDKeyboardModifierMappingSrc</key>
<integer>11</integer>
</dict>
<dict>
<key>HIDKeyboardModifierMappingDst</key>
<integer>3</integer>
<key>HIDKeyboardModifierMappingSrc</key>
<integer>4</integer>
</dict>
<dict>
<key>HIDKeyboardModifierMappingDst</key>
<integer>11</integer>
<key>HIDKeyboardModifierMappingSrc</key>
<integer>12</integer>
</dict>
</array>
PLIST
)
# <array>
# <dict>
# <key>HIDKeyboardModifierMappingSrc</key><integer>3</integer>
# <key>HIDKeyboardModifierMappingDst</key><integer>4</integer>
# </dict><dict>
# <key>HIDKeyboardModifierMappingSrc</key><integer>4</integer>
# <key>HIDKeyboardModifierMappingDst</key><integer>3</integer>
# </dict><dict>
# <key>HIDKeyboardModifierMappingSrc</key><integer>11</integer>
# <key>HIDKeyboardModifierMappingDst</key><integer>12</integer>
# </dict><dict>
# <key>HIDKeyboardModifierMappingSrc</key><integer>12</integer>
# <key>HIDKeyboardModifierMappingDst</key><integer>11</integer>
# </dict>
# </array>
# PLIST
# )
echo Delete
defaults -currentHost delete -g $KEY
echo
echo Write
echo "defaults -currentHost write -g $KEY -array-add $MacStyle"
echo
defaults -currentHost write -g $KEY $MacStyle
echo
killall -u $USER cfprefsd
echo Read
defaults -currentHost read -g $KEY
echo
# plutil -convert xml1 -o - ~/Library/Preferences/ByHost/.GlobalPreferences.6AF4C9D6-4F93-551B-9480-A89666A74A9D.plist
exit
# Key Codes:
# None — –1
# Caps Lock — 0
# Shift (Left) — 1
# Control (Left) — 2
# Option (Left) — 3
# Command (Left) — 4
# Keypad 0 — 5
# Help — 6
# Shift (Right) — 9
# Control (Right) — 10
# Option (Right) — 11
# Command (Right) — 12
defaults -currentHost write -g com.apple.keyboard.modifiermapping.1118-219-0 -array-add '<dict><key>HIDKeyboardModifierMappingDst</key><integer>-1</integer><key>HIDKeyboardModifierMappingSrc</key><integer>0</integer></dict>'
defaults -currentHost delete -g $mappingplist
# $ keyboard_id = "$(ioreg -n IOHIDKeyboard -r | grep -e VendorID\" -e ProductID | tr -d \"\|[:blank:] | cut -d\= -f2 | tr '\n' -)"
# $ defaults -currentHost write -g "com.apple.keyboard.modifiermapping.${keyboard_id}0" '(
# {
# HIDKeyboardModifierMappingDst = 2;
# HIDKeyboardModifierMappingSrc = 0;
# })'
# defaults write com.apple.universalaccess mouseDriverCursorSize 4.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment