├── .DS_Store ├── modules ├── storage_optimizations.sh ├── maintenance.sh ├── network_optimizations.sh ├── performance_tweaks.sh ├── system_optimizations.sh ├── system_monitoring.sh ├── ui_components.sh ├── menu_handler.sh ├── power_management.sh └── config.sh ├── script.sh ├── fix_permissions.sh └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/koding88/MacBook-Optimization-Script/HEAD/.DS_Store -------------------------------------------------------------------------------- /modules/storage_optimizations.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Storage Optimization Functions 4 | 5 | function clear_system_caches() { 6 | sudo rm -rf ~/Library/Caches/* 7 | sudo rm -rf /Library/Caches/* 8 | check_status "System caches cleared" "cache_clear" 9 | } 10 | 11 | function remove_unused_languages() { 12 | sudo rm -rf /System/Library/CoreServices/Language\ Chooser.app 13 | check_status "Unused languages removed" "language_cleanup" 14 | } 15 | 16 | function clear_font_caches() { 17 | sudo atsutil databases -remove 18 | sudo atsutil server -shutdown 19 | sudo atsutil server -ping 20 | check_status "Font caches cleared" "font_cache" 21 | } 22 | 23 | function remove_ds_store_files() { 24 | find . -name '.DS_Store' -depth -exec rm {} \; 25 | check_status "DS_Store files removed" "ds_store_cleanup" 26 | } -------------------------------------------------------------------------------- /modules/maintenance.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Maintenance Functions 4 | 5 | function verify_disk_permissions() { 6 | sudo diskutil verifyPermissions / 7 | check_status "Disk permissions verified" "disk_permissions" 8 | } 9 | 10 | function run_maintenance_scripts() { 11 | sudo periodic daily weekly monthly 12 | check_status "Maintenance scripts executed" "maintenance_scripts" 13 | } 14 | 15 | function clear_system_logs() { 16 | sudo rm -rf /var/log/* 17 | check_status "System logs cleared" "log_cleanup" 18 | } 19 | 20 | function reset_smc() { 21 | echo "Please follow these steps to reset SMC:" 22 | echo "1. Shut down your MacBook" 23 | echo "2. Hold Shift + Control + Option and Power button for 10 seconds" 24 | echo "3. Release all keys and power button" 25 | echo "4. Press power button to turn on your MacBook" 26 | read -p "Press Enter when done..." 27 | check_status "SMC reset instructions provided" "smc_reset" 28 | } -------------------------------------------------------------------------------- /modules/network_optimizations.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Network Optimization Functions 4 | 5 | function optimize_network_settings() { 6 | echo "Optimizing network settings..." 7 | 8 | # Enhanced network optimization commands 9 | sudo sysctl -w net.inet.tcp.delayed_ack=0 10 | sudo sysctl -w net.inet.tcp.mssdflt=1440 11 | sudo sysctl -w net.inet.tcp.blackhole=2 12 | sudo sysctl -w net.inet.icmp.icmplim=50 13 | sudo sysctl -w net.inet.tcp.path_mtu_discovery=1 14 | sudo sysctl -w net.inet.tcp.tcp_keepalive=1 15 | 16 | check_status "Network settings optimized" "network_optimization" 17 | } 18 | 19 | function flush_dns_cache() { 20 | sudo dscacheutil -flushcache 21 | sudo killall -HUP mDNSResponder 22 | check_status "DNS cache flushed" "dns_flush" 23 | } 24 | 25 | function enable_firewall() { 26 | sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate on 27 | check_status "Network firewall enabled" "firewall" 28 | } -------------------------------------------------------------------------------- /script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Source all modules 4 | source ./modules/config.sh 5 | source ./modules/ui_components.sh 6 | source ./modules/system_optimizations.sh 7 | source ./modules/network_optimizations.sh 8 | source ./modules/storage_optimizations.sh 9 | source ./modules/performance_tweaks.sh 10 | source ./modules/maintenance.sh 11 | source ./modules/system_monitoring.sh 12 | source ./modules/power_management.sh 13 | source ./modules/menu_handler.sh 14 | 15 | # Main script execution 16 | function main() { 17 | echo -e "${BLUE}Initializing MacBook Optimization Script...${NC}" 18 | 19 | if ! initialize_config; then 20 | echo -e "${RED}Failed to initialize configuration. Script may not work properly.${NC}" 21 | echo -e "${YELLOW}Press any key to continue anyway, or Ctrl+C to exit...${NC}" 22 | read -n 1 -s 23 | fi 24 | 25 | while true; do 26 | clear 27 | display_main_menu 28 | handle_user_choice 29 | done 30 | } 31 | 32 | # Start the script 33 | main 34 | -------------------------------------------------------------------------------- /modules/performance_tweaks.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Performance Optimization Functions 4 | 5 | function disable_spotlight() { 6 | sudo mdutil -a -i off 7 | check_status "Spotlight indexing disabled" "spotlight" 8 | } 9 | 10 | function disable_dashboard() { 11 | defaults write com.apple.dashboard mcx-disabled -boolean YES 12 | killall Dock 13 | check_status "Dashboard disabled" "dashboard" 14 | } 15 | 16 | function disable_animations() { 17 | defaults write NSGlobalDomain NSAutomaticWindowAnimationsEnabled -bool false 18 | defaults write NSGlobalDomain NSWindowResizeTime -float 0.001 19 | defaults write com.apple.dock launchanim -bool false 20 | killall Dock 21 | check_status "Animations disabled" "animations" 22 | } 23 | 24 | function optimize_dock() { 25 | defaults write com.apple.dock launchanim -bool false 26 | defaults write com.apple.dock expose-animation-duration -float 0 27 | defaults write com.apple.dock springboard-show-duration -int 0 28 | defaults write com.apple.dock springboard-hide-duration -int 0 29 | killall Dock 30 | check_status "Dock animations disabled" "dock_optimization" 31 | } -------------------------------------------------------------------------------- /modules/system_optimizations.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # System Optimization Functions 4 | 5 | function optimize_system_performance() { 6 | echo "Optimizing system performance..." 7 | 8 | # Enhanced system performance optimization 9 | sudo sysctl -w kern.ipc.somaxconn=2048 10 | sudo sysctl -w kern.ipc.nmbclusters=65536 11 | sudo sysctl -w kern.maxvnodes=750000 12 | sudo sysctl -w kern.maxproc=2048 13 | sudo sysctl -w kern.maxfiles=200000 14 | sudo sysctl -w kern.maxfilesperproc=100000 15 | 16 | check_status "System performance optimized" "system_performance" 17 | } 18 | 19 | function optimize_memory_management() { 20 | echo "Optimizing memory management..." 21 | 22 | # macOS memory optimization 23 | # Purge inactive memory 24 | sudo purge 25 | 26 | # Disable sudden motion sensor if it's a MacBook with HDD 27 | sudo pmset -a sms 0 28 | 29 | # Set memory pressure parameters 30 | sudo sysctl -w kern.maxvnodes=750000 31 | sudo sysctl -w kern.maxproc=2048 32 | sudo sysctl -w kern.maxfiles=200000 33 | sudo sysctl -w kern.maxfilesperproc=100000 34 | 35 | # Clear system caches 36 | sudo sync 37 | sudo purge 38 | 39 | check_status "Memory management optimized" "memory_management" 40 | } 41 | 42 | function optimize_ssd() { 43 | echo "Optimizing SSD settings..." 44 | 45 | # SSD optimizations 46 | sudo trimforce enable 47 | sudo pmset -a hibernatemode 0 48 | sudo rm /var/vm/sleepimage 49 | 50 | check_status "SSD optimized" "ssd_optimization" 51 | } 52 | 53 | function optimize_security() { 54 | echo "Optimizing security settings..." 55 | 56 | # Security optimizations 57 | sudo defaults write /Library/Preferences/com.apple.alf globalstate -int 1 58 | sudo defaults write /Library/Preferences/com.apple.alf stealthenabled -int 1 59 | sudo defaults write /Library/Preferences/com.apple.alf allowsignedenabled -int 1 60 | 61 | check_status "Security settings optimized" "security_optimization" 62 | } -------------------------------------------------------------------------------- /modules/system_monitoring.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # System Monitoring Functions 4 | 5 | function display_system_info() { 6 | local info_type=$1 7 | 8 | case $info_type in 9 | "cpu") 10 | clear 11 | echo -e "\n${YELLOW}CPU Information:${NC}" 12 | echo -e "CPU Model: $(sysctl -n machdep.cpu.brand_string)" 13 | echo -e "CPU Cores: $(sysctl -n hw.ncpu)" 14 | echo -e "CPU Speed: $(sysctl -n hw.cpufrequency_max | awk '{print $1 / 1000000000 "GHz"}')" 15 | echo -e "\nCPU Load:" 16 | top -l 1 | grep -E "^CPU" 17 | ;; 18 | "memory") 19 | clear 20 | echo -e "\n${YELLOW}Memory Status:${NC}" 21 | echo -e "Total RAM: $(sysctl -n hw.memsize | awk '{print $1 / 1024/1024/1024 "GB"}')" 22 | vm_stat | perl -ne '/page size of (\d+)/ and $size=$1; /Pages\s+([^:]+)[^0-9]+(\d+)/ and printf("%-16s % 16.2f MB\n", "$1:", $2 * $size / 1048576);' 23 | ;; 24 | "gpu") 25 | clear 26 | echo -e "\n${YELLOW}GPU Information:${NC}" 27 | system_profiler SPDisplaysDataType | grep -A 10 "Chipset Model" 28 | ;; 29 | "battery") 30 | clear 31 | echo -e "\n${YELLOW}Battery Information:${NC}" 32 | pmset -g batt | grep -v "Now drawing from" 33 | system_profiler SPPowerDataType | grep -E "Cycle Count|Condition|Charge Remaining|Charging|Full Charge Capacity|Battery Installed" 34 | ;; 35 | "disk") 36 | clear 37 | echo -e "\n${YELLOW}Disk Space:${NC}" 38 | df -h / | tail -n 1 | awk '{print "Used: " $3 " of " $2 " (" $5 " used)"}' 39 | ;; 40 | "network") 41 | clear 42 | echo -e "\n${YELLOW}Network Status:${NC}" 43 | echo -e "Active Interfaces:" 44 | netstat -nr | grep default | awk '{print $NF}' 45 | ;; 46 | "temperature") 47 | clear 48 | echo -e "\n${YELLOW}Temperature Sensors:${NC}" 49 | if command -v istats &> /dev/null; then 50 | istats 51 | else 52 | echo "Installing iStats for temperature monitoring..." 53 | sudo gem install iStats 54 | istats 55 | fi 56 | ;; 57 | esac 58 | } 59 | 60 | function check_system_status() { 61 | while true; do 62 | clear 63 | echo -e "\n${BLUE}=== System Status Check ===${NC}" 64 | echo -e "${BLUE}------------------------${NC}" 65 | echo -e "\nChoose information to view:" 66 | echo -e "${YELLOW}1. CPU Information${NC}" 67 | echo -e "${YELLOW}2. Memory Status${NC}" 68 | echo -e "${YELLOW}3. GPU Information${NC}" 69 | echo -e "${YELLOW}4. Battery Information${NC}" 70 | echo -e "${YELLOW}5. Disk Space${NC}" 71 | echo -e "${YELLOW}6. Network Status${NC}" 72 | echo -e "${YELLOW}7. Temperature Sensors${NC}" 73 | echo -e "${YELLOW}8. View All Information${NC}" 74 | echo -e "${YELLOW}0. Back to Main Menu${NC}" 75 | 76 | read -p "Enter your choice (0-8): " info_choice 77 | 78 | case $info_choice in 79 | 1) display_system_info "cpu" ;; 80 | 2) display_system_info "memory" ;; 81 | 3) display_system_info "gpu" ;; 82 | 4) display_system_info "battery" ;; 83 | 5) display_system_info "disk" ;; 84 | 6) display_system_info "network" ;; 85 | 7) display_system_info "temperature" ;; 86 | 8) 87 | for type in "cpu" "memory" "gpu" "battery" "disk" "network" "temperature"; do 88 | display_system_info "$type" 89 | echo -e "\nPress Enter to continue..." 90 | read 91 | done 92 | ;; 93 | 0) return ;; 94 | *) echo -e "${RED}Invalid choice. Please enter a number between 0 and 8.${NC}" ;; 95 | esac 96 | 97 | if [ "$info_choice" != "0" ] && [ "$info_choice" != "8" ]; then 98 | echo -e "\nPress Enter to return to system status menu..." 99 | read 100 | fi 101 | done 102 | 103 | check_status "System status check completed" "system_check" 104 | } -------------------------------------------------------------------------------- /fix_permissions.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # MacBook Optimization Script - Permission Fix Utility 4 | # This script fixes common permission issues with the .macbook_optimizer_state.conf file 5 | 6 | # Colors for output 7 | RED='\033[0;31m' 8 | GREEN='\033[0;32m' 9 | YELLOW='\033[1;33m' 10 | BLUE='\033[0;34m' 11 | NC='\033[0m' # No Color 12 | 13 | CONFIG_FILE="$HOME/.macbook_optimizer_state.conf" 14 | 15 | echo -e "${BLUE}MacBook Optimization Script - Permission Fix Utility${NC}" 16 | echo -e "${BLUE}====================================================${NC}" 17 | echo "" 18 | 19 | echo -e "${YELLOW}Checking configuration file: $CONFIG_FILE${NC}" 20 | 21 | # Check if file exists 22 | if [ -f "$CONFIG_FILE" ]; then 23 | echo -e "${GREEN}✓ Config file exists${NC}" 24 | 25 | # Check current permissions 26 | CURRENT_PERMS=$(stat -f "%Mp%Lp" "$CONFIG_FILE" 2>/dev/null || ls -l "$CONFIG_FILE" | cut -d' ' -f1) 27 | echo -e "${BLUE}Current permissions: $CURRENT_PERMS${NC}" 28 | 29 | # Check if readable 30 | if [ -r "$CONFIG_FILE" ]; then 31 | echo -e "${GREEN}✓ File is readable${NC}" 32 | else 33 | echo -e "${RED}✗ File is not readable${NC}" 34 | fi 35 | 36 | # Check if writable 37 | if [ -w "$CONFIG_FILE" ]; then 38 | echo -e "${GREEN}✓ File is writable${NC}" 39 | echo -e "${GREEN}No permission fixes needed!${NC}" 40 | exit 0 41 | else 42 | echo -e "${RED}✗ File is not writable${NC}" 43 | fi 44 | 45 | # Attempt to fix permissions 46 | echo "" 47 | echo -e "${YELLOW}Attempting to fix permissions...${NC}" 48 | 49 | if chmod 644 "$CONFIG_FILE" 2>/dev/null; then 50 | echo -e "${GREEN}✓ Successfully fixed file permissions${NC}" 51 | 52 | # Verify fix 53 | if [ -w "$CONFIG_FILE" ]; then 54 | echo -e "${GREEN}✓ File is now writable${NC}" 55 | echo -e "${GREEN}Permission issue resolved!${NC}" 56 | else 57 | echo -e "${RED}✗ File is still not writable after permission change${NC}" 58 | echo -e "${YELLOW}This might be due to file system or ownership issues${NC}" 59 | fi 60 | else 61 | echo -e "${RED}✗ Failed to change file permissions${NC}" 62 | echo -e "${YELLOW}You may need to run: sudo chown $USER:$(id -gn) \"$CONFIG_FILE\"${NC}" 63 | fi 64 | 65 | else 66 | echo -e "${YELLOW}Config file doesn't exist${NC}" 67 | echo -e "${YELLOW}Attempting to create it...${NC}" 68 | 69 | # Try to create the file 70 | if touch "$CONFIG_FILE" 2>/dev/null; then 71 | echo -e "${GREEN}✓ Successfully created config file${NC}" 72 | 73 | # Set proper permissions 74 | if chmod 644 "$CONFIG_FILE" 2>/dev/null; then 75 | echo -e "${GREEN}✓ Set proper permissions (644)${NC}" 76 | echo -e "${GREEN}Config file ready!${NC}" 77 | else 78 | echo -e "${YELLOW}⚠ Created file but couldn't set permissions${NC}" 79 | fi 80 | else 81 | echo -e "${RED}✗ Failed to create config file${NC}" 82 | echo -e "${YELLOW}Please check your home directory permissions${NC}" 83 | 84 | # Check home directory permissions 85 | if [ -w "$HOME" ]; then 86 | echo -e "${GREEN}✓ Home directory is writable${NC}" 87 | else 88 | echo -e "${RED}✗ Home directory is not writable${NC}" 89 | echo -e "${YELLOW}This is likely the root cause of the issue${NC}" 90 | fi 91 | fi 92 | fi 93 | 94 | echo "" 95 | echo -e "${BLUE}Diagnostic Information:${NC}" 96 | echo -e "${BLUE}======================${NC}" 97 | echo -e "Config file path: $CONFIG_FILE" 98 | echo -e "Home directory: $HOME" 99 | echo -e "Current user: $(whoami)" 100 | echo -e "User ID: $(id -u)" 101 | echo -e "Group ID: $(id -g)" 102 | echo -e "Home directory permissions: $(ls -ld "$HOME" | cut -d' ' -f1)" 103 | 104 | if [ -f "$CONFIG_FILE" ]; then 105 | echo -e "Config file owner: $(ls -l "$CONFIG_FILE" | awk '{print $3":"$4}')" 106 | echo -e "Config file permissions: $(ls -l "$CONFIG_FILE" | cut -d' ' -f1)" 107 | fi 108 | 109 | echo "" 110 | echo -e "${YELLOW}If issues persist, try:${NC}" 111 | echo -e "1. ${BLUE}rm \"$CONFIG_FILE\" && touch \"$CONFIG_FILE\" && chmod 644 \"$CONFIG_FILE\"${NC}" 112 | echo -e "2. ${BLUE}sudo chown \$USER:\$(id -gn) \"$CONFIG_FILE\"${NC}" 113 | echo -e "3. Check if your home directory has proper permissions" 114 | echo "" 115 | -------------------------------------------------------------------------------- /modules/ui_components.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # UI Components 4 | 5 | function print_menu_item() { 6 | local number=$1 7 | local description=$2 8 | local feature_id=$3 9 | local padding=$4 10 | 11 | # Print menu item 12 | printf "%-${padding}s" "$number. $description" 13 | 14 | # Print status if feature_id is provided 15 | if [ ! -z "$feature_id" ]; then 16 | if [ -f "$CONFIG_FILE" ] && [ -r "$CONFIG_FILE" ] && grep -q "^$feature_id=" "$CONFIG_FILE" 2>/dev/null; then 17 | local line=$(grep "^$feature_id=" "$CONFIG_FILE" | tail -n 1) 18 | local status=$(echo "$line" | cut -d'|' -f1 | cut -d'=' -f2) 19 | local timestamp=$(echo "$line" | cut -d'|' -f2) 20 | if [ "$status" = "enabled" ]; then 21 | echo -e "${GREEN}[ENABLED]${NC} $timestamp" 22 | else 23 | echo -e "${RED}[FAILED]${NC} $timestamp" 24 | fi 25 | else 26 | echo -e "${YELLOW}[NOT RUN]${NC}" 27 | fi 28 | else 29 | echo "" 30 | fi 31 | } 32 | 33 | function print_section_header() { 34 | local title=$1 35 | echo -e "\n${YELLOW}$title${NC}" 36 | echo -e "${BLUE}$(printf '=%.0s' {1..60})${NC}" 37 | } 38 | 39 | function show_quick_summary() { 40 | echo -e "\n${BLUE}Quick Status Summary:${NC}" 41 | echo -e "${BLUE}------------------${NC}" 42 | 43 | if [ ! -f "$CONFIG_FILE" ] || [ ! -r "$CONFIG_FILE" ]; then 44 | echo -e "${YELLOW}Config file not accessible${NC}" 45 | echo -e "${BLUE}------------------${NC}" 46 | return 1 47 | fi 48 | 49 | local total=$(wc -l < "$CONFIG_FILE" 2>/dev/null || echo "0") 50 | local enabled=$(grep -c "=enabled|" "$CONFIG_FILE" 2>/dev/null || echo "0") 51 | local failed=$(grep -c "=failed|" "$CONFIG_FILE" 2>/dev/null || echo "0") 52 | echo -e "Total Optimizations Run: ${YELLOW}$total${NC}" 53 | echo -e "Successfully Enabled: ${GREEN}$enabled${NC}" 54 | echo -e "Failed: ${RED}$failed${NC}" 55 | echo -e "${BLUE}------------------${NC}" 56 | } 57 | 58 | function display_main_menu() { 59 | echo -e "${GREEN}=== MacBook Optimization Script ===${NC}" 60 | 61 | # Show quick status summary 62 | show_quick_summary 63 | 64 | # System Optimizations Section 65 | print_section_header "System Optimizations" 66 | print_menu_item "1" "Optimize System Performance" "system_performance" 45 67 | print_menu_item "2" "Optimize Memory Management" "memory_management" 45 68 | print_menu_item "3" "Optimize SSD Settings" "ssd_optimization" 45 69 | print_menu_item "4" "Optimize Security" "security_optimization" 45 70 | print_menu_item "5" "Optimize Power Settings" "power_optimization" 45 71 | 72 | # Network Optimizations Section 73 | print_section_header "Network Optimizations" 74 | print_menu_item "6" "Optimize Network Settings" "network_optimization" 45 75 | print_menu_item "7" "Flush DNS Cache" "dns_flush" 45 76 | print_menu_item "8" "Enable Network Firewall" "firewall" 45 77 | 78 | # Storage Optimizations Section 79 | print_section_header "Storage Optimizations" 80 | print_menu_item "9" "Clear System Caches" "cache_clear" 45 81 | print_menu_item "10" "Remove Unused Languages" "language_cleanup" 45 82 | print_menu_item "11" "Clear Font Caches" "font_cache" 45 83 | print_menu_item "12" "Remove .DS_Store Files" "ds_store_cleanup" 45 84 | 85 | # Performance Tweaks Section 86 | print_section_header "Performance Tweaks" 87 | print_menu_item "13" "Disable Spotlight Indexing" "spotlight" 45 88 | print_menu_item "14" "Disable Dashboard" "dashboard" 45 89 | print_menu_item "15" "Disable Animations" "animations" 45 90 | print_menu_item "16" "Optimize Dock" "dock_optimization" 45 91 | 92 | # Maintenance Section 93 | print_section_header "Maintenance" 94 | print_menu_item "17" "Verify Disk Permissions" "disk_permissions" 45 95 | print_menu_item "18" "Run Daily Maintenance Scripts" "maintenance_scripts" 45 96 | print_menu_item "19" "Clear System Logs" "log_cleanup" 45 97 | print_menu_item "20" "Reset SMC" "smc_reset" 45 98 | 99 | # System Monitoring Section 100 | print_section_header "System Monitoring and Status" 101 | print_menu_item "21" "View All Optimization States" "" 45 102 | print_menu_item "22" "Reset All Optimizations" "" 45 103 | print_menu_item "23" "Check System Status" "system_check" 45 104 | print_menu_item "24" "Toggle Power Saving Mode" "power_saving" 45 105 | print_menu_item "25" "Toggle AutoBoot (Intel Only)" "autoboot" 45 106 | print_menu_item "26" "Check MDM Status" "" 45 107 | 108 | echo -e "\n${BLUE}$(printf '=%.0s' {1..60})${NC}" 109 | print_menu_item "0" "Quit" "" 45 110 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🚀 MacBook Optimization Script 2 | 3 | ## 📋 Overview 4 | 5 | MacBook Optimization Script is a comprehensive tool designed to enhance your MacBook's performance through various optimizations and provide real-time system monitoring. Built with modularity in mind, it offers an intuitive interface and extensive customization options. 6 | 7 | ### 🌟 Key Highlights 8 | 9 | - 📊 Real-time system monitoring 10 | - 🔧 One-click optimizations 11 | - 🔄 Automatic status tracking 12 | - 🛡️ Safe and reversible changes 13 | - 📱 User-friendly interface 14 | - 🔍 MDM Status Detection 15 | - 💻 Intel/Apple Silicon compatibility checks 16 | 17 | ## 🚀 Quick Start 18 | 19 | ```bash 20 | # Clone this repository 21 | git clone https://github.com/koding88/MacBook-Optimization-Script.git 22 | 23 | # Go into the repository 24 | cd MacBook-Optimization-Script 25 | 26 | # Make the script executable 27 | chmod +x script.sh 28 | 29 | # Run the script 30 | ./script.sh 31 | ``` 32 | 33 | ## 📚 Documentation 34 | 35 | ### System Requirements 36 | 37 | - macOS 10.15 (Catalina) or later 38 | - Administrative privileges 39 | - Terminal access 40 | - Internet connection (for some features) 41 | 42 | ### Directory Structure 43 | 44 | ``` 45 | MacBook-Optimization-Script/ 46 | ├── script.sh # Main script 47 | ├── modules/ # Module directory 48 | │ ├── config.sh # Configuration module 49 | │ ├── ui_components.sh # UI components 50 | │ ├── menu_handler.sh # Menu handling 51 | │ ├── power_management.sh # Power and boot management 52 | │ ├── system_monitoring.sh # System monitoring 53 | │ └── ... # Other modules 54 | ├── assets/ # Images and resources 55 | └── docs/ # Documentation 56 | ``` 57 | 58 | ## ✨ Features 59 | 60 | ### 🖥 System Optimizations 61 | 62 | - CPU and Memory optimization 63 | - SSD performance tuning 64 | - Security enhancements 65 | - Power management optimization 66 | - AutoBoot control (Intel Macs) 67 | - MDM status detection 68 | 69 | ### 🌐 Network Optimizations 70 | 71 | - TCP/IP stack optimization 72 | - DNS cache management 73 | - Firewall configuration 74 | - Network performance tuning 75 | 76 | ### 💾 Storage Optimizations 77 | 78 | - System cache cleanup 79 | - Unused language removal 80 | - Font cache optimization 81 | - .DS_Store file management 82 | 83 | ### ⚡ Performance Tweaks 84 | 85 | - Spotlight indexing control 86 | - Animation optimization 87 | - Dashboard management 88 | - Dock performance tuning 89 | 90 | ### 🔋 Power Management 91 | 92 | - Power saving mode toggle 93 | - AutoBoot control (Intel Macs) 94 | - Sleep/Wake optimization 95 | - Battery life enhancement 96 | 97 | ### 🔍 System Monitoring 98 | 99 | - Real-time performance tracking 100 | - MDM status detection 101 | - System health checks 102 | - Optimization status tracking 103 | 104 | ## 📊 Status Tracking 105 | 106 | The script includes a comprehensive status tracking system that provides: 107 | 108 | | Feature | Description | 109 | | ----------------- | ----------------------------------------- | 110 | | Real-time Updates | Immediate feedback on optimization status | 111 | | History Logging | Track all performed optimizations | 112 | | Success Metrics | Monitor success/failure rates | 113 | | Timestamps | Record when optimizations were performed | 114 | | MDM Detection | Check for Mobile Device Management | 115 | 116 | ## 🤝 Contributing 117 | 118 | We welcome contributions! Here's how you can help: 119 | 120 | 1. Fork the repository at https://github.com/koding88/MacBook-Optimization-Script/fork 121 | 2. Create your feature branch: 122 | ``` 123 | git checkout -b feature/AmazingFeature 124 | ``` 125 | 3. Commit your changes: 126 | ``` 127 | git commit -m 'Add some AmazingFeature' 128 | ``` 129 | 4. Push to the branch: 130 | ``` 131 | git push origin feature/AmazingFeature 132 | ``` 133 | 5. Open a Pull Request at https://github.com/koding88/MacBook-Optimization-Script/pulls 134 | 135 | For more details, please see our [Contributing Guidelines](CONTRIBUTING.md). 136 | 137 | ## 🔒 Security 138 | 139 | This script requires administrative privileges. Please: 140 | 141 | - Review the code before running 142 | - Keep your system up to date 143 | - Back up important data 144 | - Report security issues through our [Security Policy](SECURITY.md) 145 | 146 | ## 📝 License 147 | 148 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. 149 | 150 | ## 🙏 Acknowledgments 151 | 152 | - [Apple Developer Documentation](https://developer.apple.com/documentation/) 153 | - [MacOS Command Line Tools](https://developer.apple.com/library/archive/technotes/tn2002/tn2002.html) 154 | - All [contributors](https://github.com/koding88/MacBook-Optimization-Script/graphs/contributors) 155 | 156 | ## 📞 Support 157 | 158 | Need help? Here are some resources: 159 | 160 | - 🐛 Report bugs in [Issues](https://github.com/koding88/MacBook-Optimization-Script/issues) 161 | - 📧 Contact: [duongngocanh2k03@gmail.com](mailto:duongngocanh2k03@gmail.com) 162 | 163 | --- 164 | -------------------------------------------------------------------------------- /modules/menu_handler.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Menu Handler Function 4 | 5 | function handle_user_choice() { 6 | while true; do 7 | clear 8 | display_main_menu 9 | read -p "Enter your choice: " choice 10 | echo "" # Add a blank line for better readability 11 | 12 | case $choice in 13 | 1) 14 | optimize_system_performance 15 | get_feature_status "system_performance" 16 | ;; 17 | 2) 18 | optimize_memory_management 19 | get_feature_status "memory_management" 20 | ;; 21 | 3) 22 | optimize_ssd 23 | get_feature_status "ssd_optimization" 24 | ;; 25 | 4) 26 | optimize_security 27 | get_feature_status "security_optimization" 28 | ;; 29 | 5) 30 | optimize_power 31 | get_feature_status "power_optimization" 32 | ;; 33 | 6) 34 | optimize_network_settings 35 | get_feature_status "network_optimization" 36 | ;; 37 | 7) 38 | flush_dns_cache 39 | get_feature_status "dns_flush" 40 | ;; 41 | 8) 42 | enable_firewall 43 | get_feature_status "firewall" 44 | ;; 45 | 9) 46 | clear_system_caches 47 | get_feature_status "cache_clear" 48 | ;; 49 | 10) 50 | remove_unused_languages 51 | get_feature_status "language_cleanup" 52 | ;; 53 | 11) 54 | clear_font_caches 55 | get_feature_status "font_cache" 56 | ;; 57 | 12) 58 | remove_ds_store_files 59 | get_feature_status "ds_store_cleanup" 60 | ;; 61 | 13) 62 | disable_spotlight 63 | get_feature_status "spotlight" 64 | ;; 65 | 14) 66 | disable_dashboard 67 | get_feature_status "dashboard" 68 | ;; 69 | 15) 70 | disable_animations 71 | get_feature_status "animations" 72 | ;; 73 | 16) 74 | optimize_dock 75 | get_feature_status "dock_optimization" 76 | ;; 77 | 17) 78 | verify_disk_permissions 79 | get_feature_status "disk_permissions" 80 | ;; 81 | 18) 82 | run_maintenance_scripts 83 | get_feature_status "maintenance_scripts" 84 | ;; 85 | 19) 86 | clear_system_logs 87 | get_feature_status "log_cleanup" 88 | ;; 89 | 20) 90 | reset_smc 91 | get_feature_status "smc_reset" 92 | ;; 93 | 21) 94 | show_all_statuses 95 | ;; 96 | 22) 97 | echo -e "${YELLOW}Resetting all optimization states...${NC}" 98 | if [ -f "$CONFIG_FILE" ]; then 99 | if rm "$CONFIG_FILE" 2>/dev/null; then 100 | if initialize_config; then 101 | echo -e "${GREEN}All optimization states reset successfully${NC}" 102 | else 103 | echo -e "${RED}Failed to recreate config file${NC}" 104 | fi 105 | else 106 | echo -e "${RED}Failed to remove existing config file${NC}" 107 | echo -e "${YELLOW}You may need to manually delete: $CONFIG_FILE${NC}" 108 | fi 109 | else 110 | echo -e "${YELLOW}Config file doesn't exist, creating new one...${NC}" 111 | if initialize_config; then 112 | echo -e "${GREEN}Config file created successfully${NC}" 113 | else 114 | echo -e "${RED}Failed to create config file${NC}" 115 | fi 116 | fi 117 | ;; 118 | 23) 119 | check_system_status 120 | get_feature_status "system_check" 121 | ;; 122 | 24) 123 | toggle_power_saving 124 | get_feature_status "power_saving" 125 | ;; 126 | 25) 127 | toggle_auto_boot 128 | get_feature_status "autoboot" 129 | ;; 130 | 26) 131 | check_mdm_status 132 | ;; 133 | 0) 134 | echo -e "${GREEN}Quitting the script. Bye!${NC}" 135 | exit 0 136 | ;; 137 | *) 138 | echo -e "${RED}Invalid choice. Please enter a valid option.${NC}" 139 | ;; 140 | esac 141 | 142 | # Add pause after each action 143 | if [ "$choice" != "0" ]; then 144 | echo -e "\nPress Enter to continue..." 145 | read 146 | fi 147 | done 148 | } -------------------------------------------------------------------------------- /modules/power_management.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Power Management Functions 4 | 5 | function optimize_power() { 6 | echo "Optimizing power settings..." 7 | 8 | # Power management optimizations 9 | sudo pmset -a displaysleep 15 10 | sudo pmset -a disksleep 10 11 | sudo pmset -a womp 1 12 | sudo pmset -a networkoversleep 0 13 | 14 | check_status "Power settings optimized" "power_optimization" 15 | } 16 | 17 | function toggle_power_saving() { 18 | echo "Power Saving Mode Management..." 19 | 20 | # Get current power saving status 21 | local current_mode=$(pmset -g | grep lowpowermode | awk '{print $2}') 22 | 23 | if [ "$current_mode" = "1" ]; then 24 | # Disable power saving mode 25 | sudo pmset -a lowpowermode 0 26 | check_status "Power saving mode disabled" "power_saving" 27 | else 28 | # Enable power saving mode 29 | sudo pmset -a lowpowermode 1 30 | # Additional power saving optimizations 31 | sudo pmset -a displaysleep 5 32 | sudo pmset -a disksleep 5 33 | sudo pmset -a sleep 10 34 | sudo pmset -a lessbright 1 35 | sudo pmset -a halfdim 1 36 | check_status "Power saving mode enabled" "power_saving" 37 | fi 38 | 39 | # Show current power settings 40 | echo -e "\n${BLUE}Current Power Settings:${NC}" 41 | pmset -g 42 | } 43 | 44 | function check_mdm_status() { 45 | echo -e "${YELLOW}Checking MDM (Mobile Device Management) Status...${NC}" 46 | echo "----------------------------------------" 47 | 48 | local mdm_detected=false 49 | local mdm_details="" 50 | 51 | # Check 1: Examine /etc/hosts for MDM blocking entries 52 | echo -e "\n${BLUE}Checking /etc/hosts for MDM entries:${NC}" 53 | if grep -q "deviceenrollment.apple.com\|mdmenrollment.apple.com\|iprofiles.apple.com" /etc/hosts; then 54 | echo -e "${RED}Found MDM blocking entries in /etc/hosts:${NC}" 55 | grep "deviceenrollment.apple.com\|mdmenrollment.apple.com\|iprofiles.apple.com" /etc/hosts 56 | mdm_detected=true 57 | mdm_details+="MDM blocking entries found in /etc/hosts\n" 58 | else 59 | echo -e "${GREEN}No MDM blocking entries found in /etc/hosts${NC}" 60 | fi 61 | 62 | # Check 2: Check enrollment profiles 63 | echo -e "\n${BLUE}Checking enrollment profiles:${NC}" 64 | local profile_output=$(sudo profiles show -type enrollment 2>&1) 65 | if [[ $profile_output == *"There are no enrollment profiles"* ]]; then 66 | echo -e "${GREEN}No enrollment profiles found${NC}" 67 | else 68 | echo -e "${RED}Enrollment profiles detected:${NC}" 69 | echo "$profile_output" 70 | mdm_detected=true 71 | mdm_details+="MDM enrollment profiles detected\n" 72 | fi 73 | 74 | # Summary 75 | echo -e "\n${YELLOW}MDM Status Summary:${NC}" 76 | if [ "$mdm_detected" = true ]; then 77 | echo -e "${RED}MDM Detection: POSITIVE${NC}" 78 | echo -e "Details:" 79 | echo -e "$mdm_details" 80 | echo -e "\nRecommendation: Your device appears to be under MDM control." 81 | else 82 | echo -e "${GREEN}MDM Detection: NEGATIVE${NC}" 83 | echo "Your device appears to be free from MDM control." 84 | fi 85 | 86 | read -p "Press Enter to continue..." 87 | } 88 | 89 | function toggle_auto_boot() { 90 | echo "AutoBoot Feature Management..." 91 | 92 | # Check if running on Intel Mac 93 | if [[ $(sysctl -n machdep.cpu.brand_string) == *"Intel"* ]]; then 94 | # Get current AutoBoot status (if possible) 95 | echo "Current AutoBoot status:" 96 | nvram -p | grep "AutoBoot" || echo "AutoBoot status not found" 97 | 98 | echo -e "\n1. Disable AutoBoot (prevent auto-start when opening lid)" 99 | echo "2. Enable AutoBoot (restore default behavior)" 100 | echo "3. Return to previous menu" 101 | 102 | read -p "Enter your choice (1-3): " choice 103 | 104 | case $choice in 105 | 1) 106 | sudo nvram AutoBoot=%00 107 | check_status "AutoBoot disabled - Mac won't automatically start when opening lid" "autoboot" 108 | echo -e "\nNew AutoBoot status:" 109 | nvram -p | grep "AutoBoot" || echo "AutoBoot status not found" 110 | 111 | echo -e "\n${YELLOW}The changes require a system restart to take effect.${NC}" 112 | read -p "Would you like to restart now? (y/n): " restart_choice 113 | if [[ $restart_choice =~ ^[Yy]$ ]]; then 114 | echo "System will restart in 5 seconds..." 115 | sleep 5 116 | sudo shutdown -r now 117 | else 118 | echo "Please remember to restart your system later for changes to take effect." 119 | fi 120 | ;; 121 | 2) 122 | sudo nvram AutoBoot=%03 123 | check_status "AutoBoot enabled - Mac will start when opening lid" "autoboot" 124 | echo -e "\nNew AutoBoot status:" 125 | nvram -p | grep "AutoBoot" || echo "AutoBoot status not found" 126 | 127 | echo -e "\n${YELLOW}The changes require a system restart to take effect.${NC}" 128 | read -p "Would you like to restart now? (y/n): " restart_choice 129 | if [[ $restart_choice =~ ^[Yy]$ ]]; then 130 | echo "System will restart in 5 seconds..." 131 | sleep 5 132 | sudo shutdown -r now 133 | else 134 | echo "Please remember to restart your system later for changes to take effect." 135 | fi 136 | ;; 137 | 3) 138 | return 139 | ;; 140 | *) 141 | echo "Invalid choice. Please try again." 142 | ;; 143 | esac 144 | else 145 | echo "This feature is only available for Intel-based Macs." 146 | echo "Your Mac appears to be using Apple Silicon." 147 | fi 148 | 149 | read -p "Press Enter to continue..." 150 | } -------------------------------------------------------------------------------- /modules/config.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Configuration constants 4 | CONFIG_FILE="$HOME/.macbook_optimizer_state.conf" 5 | 6 | # Colors for status messages 7 | RED='\033[0;31m' 8 | GREEN='\033[0;32m' 9 | YELLOW='\033[1;33m' 10 | BLUE='\033[0;34m' 11 | NC='\033[0m' # No Color 12 | 13 | # Initialize configuration 14 | function initialize_config() { 15 | # Create config directory if it doesn't exist 16 | local config_dir=$(dirname "$CONFIG_FILE") 17 | if [ ! -d "$config_dir" ]; then 18 | mkdir -p "$config_dir" 2>/dev/null || { 19 | echo -e "${RED}Error: Cannot create config directory $config_dir${NC}" 20 | return 1 21 | } 22 | fi 23 | 24 | # Create config file with proper permissions 25 | if [ ! -f "$CONFIG_FILE" ]; then 26 | if touch "$CONFIG_FILE" 2>/dev/null; then 27 | chmod 644 "$CONFIG_FILE" 2>/dev/null 28 | else 29 | echo -e "${RED}Error: Cannot create config file $CONFIG_FILE${NC}" 30 | echo -e "${YELLOW}Please check your home directory permissions${NC}" 31 | return 1 32 | fi 33 | fi 34 | 35 | # Ensure the file is writable by current user 36 | if [ ! -w "$CONFIG_FILE" ]; then 37 | # Try to fix permissions 38 | if chmod 644 "$CONFIG_FILE" 2>/dev/null; then 39 | echo -e "${YELLOW}Fixed permissions for config file${NC}" 40 | else 41 | echo -e "${RED}Error: Config file exists but is not writable${NC}" 42 | echo -e "${YELLOW}Try running: chmod 644 $CONFIG_FILE${NC}" 43 | return 1 44 | fi 45 | fi 46 | 47 | return 0 48 | } 49 | 50 | # Status tracking functions 51 | function add_timestamp() { 52 | echo "$(date '+%Y-%m-%d %H:%M:%S')" 53 | } 54 | 55 | function check_status() { 56 | local timestamp=$(add_timestamp) 57 | local status_message 58 | local config_entry 59 | 60 | if [ $? -eq 0 ]; then 61 | status_message="${GREEN}Success: $1${NC}" 62 | config_entry="$2=enabled|$timestamp" 63 | else 64 | status_message="${RED}Error: $1${NC}" 65 | config_entry="$2=failed|$timestamp" 66 | fi 67 | 68 | echo -e "$status_message" 69 | 70 | # Safely write to config file with permission checks 71 | if write_to_config "$config_entry"; then 72 | return 0 73 | else 74 | echo -e "${YELLOW}Warning: Could not save status to config file${NC}" 75 | return 1 76 | fi 77 | } 78 | 79 | # Safe function to write to config file 80 | function write_to_config() { 81 | local entry="$1" 82 | 83 | # Check if config file is accessible 84 | if [ ! -f "$CONFIG_FILE" ]; then 85 | echo -e "${YELLOW}Config file doesn't exist, attempting to create...${NC}" 86 | initialize_config || return 1 87 | fi 88 | 89 | # Check write permissions 90 | if [ ! -w "$CONFIG_FILE" ]; then 91 | echo -e "${RED}Error: Cannot write to config file (permission denied)${NC}" 92 | echo -e "${YELLOW}Config file location: $CONFIG_FILE${NC}" 93 | echo -e "${YELLOW}Please run: chmod 644 $CONFIG_FILE${NC}" 94 | return 1 95 | fi 96 | 97 | # Attempt to write to file 98 | if echo "$entry" >> "$CONFIG_FILE" 2>/dev/null; then 99 | return 0 100 | else 101 | echo -e "${RED}Error: Failed to write to config file${NC}" 102 | return 1 103 | fi 104 | } 105 | 106 | # Safe function to read from config file 107 | function safe_read_config() { 108 | if [ ! -f "$CONFIG_FILE" ]; then 109 | return 1 110 | fi 111 | 112 | if [ ! -r "$CONFIG_FILE" ]; then 113 | echo -e "${RED}Error: Cannot read config file (permission denied)${NC}" 114 | echo -e "${YELLOW}Config file location: $CONFIG_FILE${NC}" 115 | echo -e "${YELLOW}Please run: chmod 644 $CONFIG_FILE${NC}" 116 | return 1 117 | fi 118 | 119 | return 0 120 | } 121 | 122 | function get_feature_status() { 123 | local feature=$1 124 | echo -e "\n${BLUE}Feature Status Report:${NC}" 125 | echo -e "${BLUE}------------------${NC}" 126 | 127 | if ! safe_read_config; then 128 | echo -e "Feature: ${YELLOW}$feature${NC}" 129 | echo -e "Status: ${RED}config file not accessible${NC}" 130 | echo -e "${BLUE}------------------${NC}\n" 131 | return 1 132 | fi 133 | 134 | if grep -q "^$feature=" "$CONFIG_FILE" 2>/dev/null; then 135 | local line=$(grep "^$feature=" "$CONFIG_FILE" | tail -n 1) 136 | local status=$(echo "$line" | cut -d'|' -f1 | cut -d'=' -f2) 137 | local timestamp=$(echo "$line" | cut -d'|' -f2) 138 | echo -e "Feature: ${YELLOW}$feature${NC}" 139 | if [ "$status" = "enabled" ]; then 140 | echo -e "Status: ${GREEN}$status${NC}" 141 | else 142 | echo -e "Status: ${RED}$status${NC}" 143 | fi 144 | echo -e "Last Run: $timestamp" 145 | else 146 | echo -e "Feature: ${YELLOW}$feature${NC}" 147 | echo -e "Status: ${YELLOW}never run${NC}" 148 | fi 149 | echo -e "${BLUE}------------------${NC}\n" 150 | } 151 | 152 | function show_all_statuses() { 153 | echo -e "\n${BLUE}Complete System Status Report${NC}" 154 | echo -e "${BLUE}===========================${NC}" 155 | 156 | if ! safe_read_config; then 157 | echo -e "${RED}Cannot access configuration file${NC}" 158 | return 1 159 | fi 160 | 161 | if [ ! -s "$CONFIG_FILE" ]; then 162 | echo -e "${YELLOW}No optimizations have been run yet.${NC}" 163 | return 164 | fi 165 | 166 | while IFS= read -r line; do 167 | if [ -n "$line" ]; then # Skip empty lines 168 | local feature=$(echo "$line" | cut -d'=' -f1) 169 | local status=$(echo "$line" | cut -d'|' -f1 | cut -d'=' -f2) 170 | local timestamp=$(echo "$line" | cut -d'|' -f2) 171 | 172 | echo -e "${YELLOW}$feature${NC}:" 173 | if [ "$status" = "enabled" ]; then 174 | echo -e " Status: ${GREEN}$status${NC}" 175 | else 176 | echo -e " Status: ${RED}$status${NC}" 177 | fi 178 | echo -e " Last Run: $timestamp" 179 | echo -e "${BLUE}------------------${NC}" 180 | fi 181 | done < "$CONFIG_FILE" 182 | } --------------------------------------------------------------------------------