Configuring Keyboard Settings on Mac Devices
Managing keyboard settings consistently across a Mac fleet is essential for maintaining productivity and user experience standards. This comprehensive guide provides shell scripts and techniques to configure various keyboard settings on macOS devices, including key repeat rates, delays, keyboard navigation, and auto-correct features.
Understanding Mac Keyboard Settings
macOS provides several keyboard configuration options that can significantly impact user productivity and comfort:
Key Repeat Rate
The speed at which a character is repeatedly registered when a key is held down. A higher rate means faster character repetition.
Delay Until Repeat
The amount of time the keyboard waits before starting to repeat a key when held down. A shorter delay means repetition begins sooner.
Keyboard Navigation
Allows users to navigate interface elements using the keyboard instead of mouse clicks, improving accessibility and workflow efficiency.
Auto-Correct
Automatically corrects misspelled words as you type, which can be helpful or disruptive depending on user preferences and use cases.
Prerequisites
Before configuring keyboard settings, ensure you have:
- Administrative privileges on the Mac
- Terminal or SSH access
- Understanding of user context requirements
- Backup of current settings (recommended)
Basic Keyboard Configuration Commands
Understanding the Configuration Tools
Mac keyboard settings are managed through several system components:
defaults
: Command-line interface to user defaults systemlaunchctl
: Interface to launchd for running commands in user context- NSGlobalDomain: Domain for system-wide settings
- User context: Settings must be applied in the correct user session
Current User Detection
Most keyboard settings require user context. Here's how to detect the current user:
#!/bin/bash
# Get current console user
get_current_user() {
local current_user=$(ls -l /dev/console | awk '/ / { print $3 }')
echo "$current_user"
}
# Get user ID
get_user_id() {
local username=$1
local user_id=$(id -u "$username")
echo "$user_id"
}
# Example usage
current_user=$(get_current_user)
user_id=$(get_user_id "$current_user")
echo "Current user: $current_user"
echo "User ID: $user_id"
Key Repeat Rate Configuration
Basic Key Repeat Rate Script
Configure the speed of key repetition:
#!/bin/bash
# Configure key repeat rate
# Values: 1-15 (1 = fastest, 15 = slowest)
KEY_REPEAT_RATE=2
echo "Configuring key repeat rate to: $KEY_REPEAT_RATE"
# Get current user and user ID
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")
# Apply setting in user context
if launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" defaults write -g KeyRepeat -int $KEY_REPEAT_RATE; then
echo "✓ Key repeat rate set successfully"
else
echo "✗ Failed to set key repeat rate"
exit 1
fi
echo "Changes will take effect after user logout/login"
Advanced Key Repeat Rate Script
More comprehensive script with validation and logging:
#!/bin/bash
# Advanced key repeat rate configuration script
LOG_FILE="/var/log/keyboard_settings.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
# Function to log messages
log_message() {
echo "[$TIMESTAMP] $1" | tee -a "$LOG_FILE"
}
# Function to validate key repeat rate
validate_key_repeat_rate() {
local rate=$1
if [[ ! "$rate" =~ ^[0-9]+$ ]]; then
log_message "ERROR: Key repeat rate must be a number"
return 1
fi
if [ "$rate" -lt 1 ] || [ "$rate" -gt 15 ]; then
log_message "ERROR: Key repeat rate must be between 1-15"
return 1
fi
return 0
}
# Function to get current key repeat rate
get_current_key_repeat_rate() {
local current_user=$1
local current_uid=$2
local current_rate=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read -g KeyRepeat 2>/dev/null)
if [ -n "$current_rate" ]; then
echo "$current_rate"
else
echo "Not set"
fi
}
# Function to set key repeat rate
set_key_repeat_rate() {
local rate=$1
local current_user=$2
local current_uid=$3
log_message "Setting key repeat rate to: $rate for user: $current_user"
# Get current setting
local current_rate=$(get_current_key_repeat_rate "$current_user" "$current_uid")
log_message "Current key repeat rate: $current_rate"
# Apply new setting
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write -g KeyRepeat -int $rate; then
log_message "SUCCESS: Key repeat rate set to $rate"
# Verify setting
local new_rate=$(get_current_key_repeat_rate "$current_user" "$current_uid")
log_message "Verified new key repeat rate: $new_rate"
return 0
else
log_message "ERROR: Failed to set key repeat rate"
return 1
fi
}
# Main execution
KEY_REPEAT_RATE=${1:-2} # Default to 2 if not specified
log_message "Starting key repeat rate configuration"
# Validate input
if ! validate_key_repeat_rate "$KEY_REPEAT_RATE"; then
log_message "Invalid key repeat rate: $KEY_REPEAT_RATE"
exit 1
fi
# Get current user
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")
if [ -z "$CurrentUser" ] || [ "$CurrentUser" = "root" ]; then
log_message "ERROR: No valid user session found"
exit 1
fi
log_message "Current user: $CurrentUser (UID: $CurrentUserUID)"
# Set key repeat rate
if set_key_repeat_rate "$KEY_REPEAT_RATE" "$CurrentUser" "$CurrentUserUID"; then
log_message "Key repeat rate configuration completed successfully"
else
log_message "Key repeat rate configuration failed"
exit 1
fi
log_message "Note: Changes will take effect after user logout/login"
Delay Until Repeat Configuration
Basic Delay Script
Configure the delay before key repetition begins:
#!/bin/bash
# Configure delay until repeat
# Values: 0-3 (0 = shortest delay, 3 = longest delay)
KEY_DELAY=1
echo "Configuring delay until repeat to: $KEY_DELAY"
# Get current user and user ID
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")
# Apply setting in user context
if launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" defaults write -g InitialKeyRepeat -int $KEY_DELAY; then
echo "✓ Delay until repeat set successfully"
else
echo "✗ Failed to set delay until repeat"
exit 1
fi
echo "Changes will take effect after user logout/login"
Advanced Delay Configuration Script
Comprehensive script with validation and multiple user support:
#!/bin/bash
# Advanced delay until repeat configuration script
LOG_FILE="/var/log/keyboard_settings.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
# Function to log messages
log_message() {
echo "[$TIMESTAMP] $1" | tee -a "$LOG_FILE"
}
# Function to validate delay value
validate_delay() {
local delay=$1
if [[ ! "$delay" =~ ^[0-9]+$ ]]; then
log_message "ERROR: Delay must be a number"
return 1
fi
if [ "$delay" -lt 0 ] || [ "$delay" -gt 3 ]; then
log_message "ERROR: Delay must be between 0-3"
return 1
fi
return 0
}
# Function to get current delay setting
get_current_delay() {
local current_user=$1
local current_uid=$2
local current_delay=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read -g InitialKeyRepeat 2>/dev/null)
if [ -n "$current_delay" ]; then
echo "$current_delay"
else
echo "Not set"
fi
}
# Function to set delay until repeat
set_delay_until_repeat() {
local delay=$1
local current_user=$2
local current_uid=$3
log_message "Setting delay until repeat to: $delay for user: $current_user"
# Get current setting
local current_delay=$(get_current_delay "$current_user" "$current_uid")
log_message "Current delay until repeat: $current_delay"
# Apply new setting
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write -g InitialKeyRepeat -int $delay; then
log_message "SUCCESS: Delay until repeat set to $delay"
# Verify setting
local new_delay=$(get_current_delay "$current_user" "$current_uid")
log_message "Verified new delay until repeat: $new_delay"
return 0
else
log_message "ERROR: Failed to set delay until repeat"
return 1
fi
}
# Main execution
KEY_DELAY=${1:-1} # Default to 1 if not specified
log_message "Starting delay until repeat configuration"
# Validate input
if ! validate_delay "$KEY_DELAY"; then
log_message "Invalid delay value: $KEY_DELAY"
exit 1
fi
# Get current user
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")
if [ -z "$CurrentUser" ] || [ "$CurrentUser" = "root" ]; then
log_message "ERROR: No valid user session found"
exit 1
fi
log_message "Current user: $CurrentUser (UID: $CurrentUserUID)"
# Set delay until repeat
if set_delay_until_repeat "$KEY_DELAY" "$CurrentUser" "$CurrentUserUID"; then
log_message "Delay until repeat configuration completed successfully"
else
log_message "Delay until repeat configuration failed"
exit 1
fi
log_message "Note: Changes will take effect after user logout/login"
Keyboard Navigation Configuration
Basic Keyboard Navigation Script
Enable keyboard navigation for better accessibility:
#!/bin/bash
# Enable keyboard navigation
echo "Enabling keyboard navigation..."
# Get current user and user ID
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")
# Enable keyboard navigation (3 = full keyboard access)
if launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" defaults write NSGlobalDomain AppleKeyboardUIMode -int 3; then
echo "✓ Keyboard navigation enabled successfully"
else
echo "✗ Failed to enable keyboard navigation"
exit 1
fi
echo "Keyboard navigation is now enabled"
echo "Note: This feature requires macOS 13.0 or later"
Advanced Keyboard Navigation Script
Comprehensive script with version checking and configuration options:
#!/bin/bash
# Advanced keyboard navigation configuration script
LOG_FILE="/var/log/keyboard_settings.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
# Function to log messages
log_message() {
echo "[$TIMESTAMP] $1" | tee -a "$LOG_FILE"
}
# Function to check macOS version
check_macos_version() {
local version=$(sw_vers -productVersion)
local major_version=$(echo "$version" | cut -d. -f1)
log_message "macOS version: $version"
if [ "$major_version" -ge 13 ]; then
log_message "macOS version is compatible with keyboard navigation"
return 0
else
log_message "WARNING: macOS version may not fully support keyboard navigation"
return 1
fi
}
# Function to get current keyboard navigation setting
get_keyboard_navigation_status() {
local current_user=$1
local current_uid=$2
local current_setting=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read NSGlobalDomain AppleKeyboardUIMode 2>/dev/null)
if [ -n "$current_setting" ]; then
case "$current_setting" in
0) echo "Disabled (text boxes and lists only)" ;;
1) echo "Enabled (text boxes and lists only)" ;;
2) echo "Enabled (controls when Tab key is pressed)" ;;
3) echo "Enabled (all controls)" ;;
*) echo "Unknown ($current_setting)" ;;
esac
else
echo "Not set (default)"
fi
}
# Function to set keyboard navigation
set_keyboard_navigation() {
local mode=$1
local current_user=$2
local current_uid=$3
log_message "Setting keyboard navigation mode to: $mode for user: $current_user"
# Get current setting
local current_status=$(get_keyboard_navigation_status "$current_user" "$current_uid")
log_message "Current keyboard navigation: $current_status"
# Apply new setting
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain AppleKeyboardUIMode -int $mode; then
log_message "SUCCESS: Keyboard navigation set to mode $mode"
# Verify setting
local new_status=$(get_keyboard_navigation_status "$current_user" "$current_uid")
log_message "Verified new keyboard navigation: $new_status"
return 0
else
log_message "ERROR: Failed to set keyboard navigation"
return 1
fi
}
# Main execution
KEYBOARD_MODE=${1:-3} # Default to 3 (full keyboard access) if not specified
log_message "Starting keyboard navigation configuration"
# Check macOS version
check_macos_version
# Validate mode
if [[ ! "$KEYBOARD_MODE" =~ ^[0-3]$ ]]; then
log_message "ERROR: Invalid keyboard mode: $KEYBOARD_MODE (must be 0-3)"
exit 1
fi
# Get current user
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")
if [ -z "$CurrentUser" ] || [ "$CurrentUser" = "root" ]; then
log_message "ERROR: No valid user session found"
exit 1
fi
log_message "Current user: $CurrentUser (UID: $CurrentUserUID)"
# Set keyboard navigation
if set_keyboard_navigation "$KEYBOARD_MODE" "$CurrentUser" "$CurrentUserUID"; then
log_message "Keyboard navigation configuration completed successfully"
else
log_message "Keyboard navigation configuration failed"
exit 1
fi
log_message "Keyboard navigation is now configured"
Auto-Correct Configuration
Basic Auto-Correct Disable Script
Disable automatic spelling correction:
#!/bin/bash
# Disable auto-correct
echo "Disabling auto-correct..."
# Get current user and user ID
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")
# Disable auto-correct
if launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool false; then
echo "✓ Auto-correct disabled successfully"
else
echo "✗ Failed to disable auto-correct"
exit 1
fi
echo "Auto-correct is now disabled"
echo "Note: Changes will take effect after user logout/login"
Advanced Auto-Correct Management Script
Comprehensive script for managing various auto-correct features:
#!/bin/bash
# Advanced auto-correct configuration script
LOG_FILE="/var/log/keyboard_settings.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
# Function to log messages
log_message() {
echo "[$TIMESTAMP] $1" | tee -a "$LOG_FILE"
}
# Function to get current auto-correct settings
get_autocorrect_settings() {
local current_user=$1
local current_uid=$2
log_message "Getting current auto-correct settings for user: $current_user"
# Spelling correction
local spelling_correction=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read NSGlobalDomain NSAutomaticSpellingCorrectionEnabled 2>/dev/null)
# Capitalize words
local capitalize_words=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read NSGlobalDomain NSAutomaticCapitalizationEnabled 2>/dev/null)
# Add period with double-space
local period_substitution=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read NSGlobalDomain NSAutomaticPeriodSubstitutionEnabled 2>/dev/null)
# Smart quotes
local smart_quotes=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled 2>/dev/null)
# Smart dashes
local smart_dashes=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read NSGlobalDomain NSAutomaticDashSubstitutionEnabled 2>/dev/null)
echo "Current Auto-Correct Settings:"
echo " Spelling Correction: ${spelling_correction:-Not set}"
echo " Capitalize Words: ${capitalize_words:-Not set}"
echo " Period Substitution: ${period_substitution:-Not set}"
echo " Smart Quotes: ${smart_quotes:-Not set}"
echo " Smart Dashes: ${smart_dashes:-Not set}"
}
# Function to configure auto-correct features
configure_autocorrect() {
local current_user=$1
local current_uid=$2
local spelling_correction=$3
local capitalize_words=$4
local period_substitution=$5
local smart_quotes=$6
local smart_dashes=$7
log_message "Configuring auto-correct settings for user: $current_user"
local success=0
# Configure spelling correction
if [ -n "$spelling_correction" ]; then
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool $spelling_correction; then
log_message "SUCCESS: Spelling correction set to $spelling_correction"
else
log_message "ERROR: Failed to set spelling correction"
success=1
fi
fi
# Configure capitalize words
if [ -n "$capitalize_words" ]; then
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticCapitalizationEnabled -bool $capitalize_words; then
log_message "SUCCESS: Capitalize words set to $capitalize_words"
else
log_message "ERROR: Failed to set capitalize words"
success=1
fi
fi
# Configure period substitution
if [ -n "$period_substitution" ]; then
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticPeriodSubstitutionEnabled -bool $period_substitution; then
log_message "SUCCESS: Period substitution set to $period_substitution"
else
log_message "ERROR: Failed to set period substitution"
success=1
fi
fi
# Configure smart quotes
if [ -n "$smart_quotes" ]; then
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool $smart_quotes; then
log_message "SUCCESS: Smart quotes set to $smart_quotes"
else
log_message "ERROR: Failed to set smart quotes"
success=1
fi
fi
# Configure smart dashes
if [ -n "$smart_dashes" ]; then
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticDashSubstitutionEnabled -bool $smart_dashes; then
log_message "SUCCESS: Smart dashes set to $smart_dashes"
else
log_message "ERROR: Failed to set smart dashes"
success=1
fi
fi
return $success
}
# Main execution
log_message "Starting auto-correct configuration"
# Configuration options (change as needed)
SPELLING_CORRECTION=${1:-false}
CAPITALIZE_WORDS=${2:-false}
PERIOD_SUBSTITUTION=${3:-false}
SMART_QUOTES=${4:-false}
SMART_DASHES=${5:-false}
# Get current user
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")
if [ -z "$CurrentUser" ] || [ "$CurrentUser" = "root" ]; then
log_message "ERROR: No valid user session found"
exit 1
fi
log_message "Current user: $CurrentUser (UID: $CurrentUserUID)"
# Show current settings
get_autocorrect_settings "$CurrentUser" "$CurrentUserUID"
# Configure auto-correct
if configure_autocorrect "$CurrentUser" "$CurrentUserUID" "$SPELLING_CORRECTION" "$CAPITALIZE_WORDS" "$PERIOD_SUBSTITUTION" "$SMART_QUOTES" "$SMART_DASHES"; then
log_message "Auto-correct configuration completed successfully"
else
log_message "Some auto-correct configurations failed"
exit 1
fi
log_message "Note: Changes will take effect after user logout/login"
Comprehensive Keyboard Configuration
All-in-One Configuration Script
Script to configure all keyboard settings at once:
#!/bin/bash
# Comprehensive keyboard configuration script
LOG_FILE="/var/log/keyboard_settings.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
# Configuration variables
KEY_REPEAT_RATE=2
KEY_DELAY=1
KEYBOARD_NAVIGATION=3
DISABLE_AUTOCORRECT=true
DISABLE_CAPITALIZE=true
DISABLE_PERIOD_SUB=true
DISABLE_SMART_QUOTES=true
DISABLE_SMART_DASHES=true
# Function to log messages
log_message() {
echo "[$TIMESTAMP] $1" | tee -a "$LOG_FILE"
}
# Function to apply all keyboard settings
apply_keyboard_settings() {
local current_user=$1
local current_uid=$2
log_message "Applying comprehensive keyboard settings for user: $current_user"
local success=0
# Key repeat rate
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write -g KeyRepeat -int $KEY_REPEAT_RATE; then
log_message "SUCCESS: Key repeat rate set to $KEY_REPEAT_RATE"
else
log_message "ERROR: Failed to set key repeat rate"
success=1
fi
# Delay until repeat
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write -g InitialKeyRepeat -int $KEY_DELAY; then
log_message "SUCCESS: Delay until repeat set to $KEY_DELAY"
else
log_message "ERROR: Failed to set delay until repeat"
success=1
fi
# Keyboard navigation
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain AppleKeyboardUIMode -int $KEYBOARD_NAVIGATION; then
log_message "SUCCESS: Keyboard navigation set to $KEYBOARD_NAVIGATION"
else
log_message "ERROR: Failed to set keyboard navigation"
success=1
fi
# Auto-correct features
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool $DISABLE_AUTOCORRECT; then
log_message "SUCCESS: Auto-correct disabled"
else
log_message "ERROR: Failed to disable auto-correct"
success=1
fi
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticCapitalizationEnabled -bool $DISABLE_CAPITALIZE; then
log_message "SUCCESS: Auto-capitalize disabled"
else
log_message "ERROR: Failed to disable auto-capitalize"
success=1
fi
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticPeriodSubstitutionEnabled -bool $DISABLE_PERIOD_SUB; then
log_message "SUCCESS: Period substitution disabled"
else
log_message "ERROR: Failed to disable period substitution"
success=1
fi
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool $DISABLE_SMART_QUOTES; then
log_message "SUCCESS: Smart quotes disabled"
else
log_message "ERROR: Failed to disable smart quotes"
success=1
fi
if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticDashSubstitutionEnabled -bool $DISABLE_SMART_DASHES; then
log_message "SUCCESS: Smart dashes disabled"
else
log_message "ERROR: Failed to disable smart dashes"
success=1
fi
return $success
}
# Main execution
log_message "Starting comprehensive keyboard configuration"
# Get current user
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")
if [ -z "$CurrentUser" ] || [ "$CurrentUser" = "root" ]; then
log_message "ERROR: No valid user session found"
exit 1
fi
log_message "Current user: $CurrentUser (UID: $CurrentUserUID)"
# Apply all settings
if apply_keyboard_settings "$CurrentUser" "$CurrentUserUID"; then
log_message "Comprehensive keyboard configuration completed successfully"
echo "✓ All keyboard settings applied successfully"
else
log_message "Some keyboard configurations failed"
echo "⚠ Some settings may not have been applied. Check log: $LOG_FILE"
exit 1
fi
log_message "Note: Changes will take effect after user logout/login"
echo "Note: Please log out and log back in for changes to take effect"
Enterprise Deployment Scripts
Multi-User Configuration Script
Script for applying settings to multiple users:
#!/bin/bash
# Multi-user keyboard configuration script
LOG_FILE="/var/log/keyboard_settings.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
# Configuration
KEY_REPEAT_RATE=2
KEY_DELAY=1
KEYBOARD_NAVIGATION=3
DISABLE_AUTOCORRECT=false
# Function to log messages
log_message() {
echo "[$TIMESTAMP] $1" | tee -a "$LOG_FILE"
}
# Function to get all users
get_all_users() {
# Get users with UID >= 500 (regular users)
local users=$(dscl . -list /Users UniqueID | awk '$2 >= 500 { print $1 }')
echo "$users"
}
# Function to configure user settings
configure_user_keyboard() {
local username=$1
local user_uid=$(id -u "$username" 2>/dev/null)
if [ -z "$user_uid" ]; then
log_message "ERROR: Cannot get UID for user: $username"
return 1
fi
log_message "Configuring keyboard settings for user: $username (UID: $user_uid)"
# Check if user has a home directory
local home_dir=$(eval echo ~$username)
if [ ! -d "$home_dir" ]; then
log_message "WARNING: Home directory not found for user: $username"
return 1
fi
# Apply settings
launchctl asuser $user_uid sudo -iu "$username" defaults write -g KeyRepeat -int $KEY_REPEAT_RATE
launchctl asuser $user_uid sudo -iu "$username" defaults write -g InitialKeyRepeat -int $KEY_DELAY
launchctl asuser $user_uid sudo -iu "$username" defaults write NSGlobalDomain AppleKeyboardUIMode -int $KEYBOARD_NAVIGATION
launchctl asuser $user_uid sudo -iu "$username" defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool $DISABLE_AUTOCORRECT
log_message "SUCCESS: Keyboard settings configured for user: $username"
return 0
}
# Main execution
log_message "Starting multi-user keyboard configuration"
# Get all users
users=$(get_all_users)
if [ -z "$users" ]; then
log_message "ERROR: No users found"
exit 1
fi
log_message "Found users: $users"
# Configure each user
successful=0
failed=0
for user in $users; do
if configure_user_keyboard "$user"; then
((successful++))
else
((failed++))
fi
done
log_message "Multi-user configuration completed"
log_message "Successfully configured: $successful users"
log_message "Failed: $failed users"
echo "Configuration Summary:"
echo " Successfully configured: $successful users"
echo " Failed: $failed users"
echo " Log file: $LOG_FILE"
Remote Deployment Script
Script for deploying configurations remotely:
#!/bin/bash
# Remote keyboard configuration deployment script
REMOTE_HOSTS=(
"10.0.1.10"
"10.0.1.11"
"10.0.1.12"
# Add more hosts as needed
)
SSH_USER="admin"
SSH_KEY_PATH="/path/to/ssh/key"
LOG_FILE="/var/log/remote_keyboard_deployment.log"
# Function to log messages
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
# Function to deploy to remote host
deploy_to_host() {
local host=$1
log_message "Deploying keyboard configuration to host: $host"
# Copy script to remote host
if scp -i "$SSH_KEY_PATH" -o StrictHostKeyChecking=no ./keyboard_config.sh "$SSH_USER@$host:/tmp/"; then
log_message "Script copied to $host"
else
log_message "ERROR: Failed to copy script to $host"
return 1
fi
# Execute script on remote host
if ssh -i "$SSH_KEY_PATH" -o StrictHostKeyChecking=no "$SSH_USER@$host" "chmod +x /tmp/keyboard_config.sh && sudo /tmp/keyboard_config.sh"; then
log_message "SUCCESS: Configuration applied on $host"
return 0
else
log_message "ERROR: Failed to apply configuration on $host"
return 1
fi
}
# Main execution
log_message "Starting remote keyboard configuration deployment"
successful=0
failed=0
for host in "${REMOTE_HOSTS[@]}"; do
if deploy_to_host "$host"; then
((successful++))
else
((failed++))
fi
done
log_message "Remote deployment completed"
log_message "Successfully deployed: $successful hosts"
log_message "Failed: $failed hosts"
echo "Deployment Summary:"
echo " Successfully deployed: $successful hosts"
echo " Failed: $failed hosts"
echo " Log file: $LOG_FILE"
Best Practices and Recommendations
1. User Context Management
- Always apply settings in the correct user context
- Use
launchctl asuser
for user-specific settings - Verify current user before applying configurations
2. Setting Validation
- Validate input values before applying settings
- Check for existing settings before modification
- Verify settings after application
3. Error Handling
- Implement comprehensive error checking
- Log all operations for troubleshooting
- Provide clear error messages
4. Testing and Deployment
- Test scripts on individual devices before mass deployment
- Use staged rollouts for large environments
- Monitor for issues after deployment
5. Documentation
- Document all customizations
- Maintain configuration version control
- Keep logs of all changes
Conclusion
Effective keyboard configuration management is essential for maintaining consistent user experiences across Mac fleets. The scripts and techniques provided in this guide offer comprehensive solutions for various keyboard configuration scenarios.
Key takeaways:
- Use appropriate user context for setting application
- Implement proper validation and error handling
- Test configurations before mass deployment
- Monitor and log all configuration changes
- Consider user preferences and accessibility needs
Remember that keyboard settings are highly personal, so consider providing options for users to customize their experience while maintaining enterprise standards where necessary.