Skip to content

feat: add macOS defaults restore script - #37

Open
adlkt wants to merge 1 commit into
harry0703:mainfrom
adlkt:restore-macos-defaults-script
Open

adlkt wants to merge 1 commit into
harry0703:mainfrom
adlkt:restore-macos-defaults-script

Conversation

@adlkt

@adlkt adlkt commented Aug 28, 2026

Copy link
Copy Markdown

Motivation

While investigating the System Optimization feature on a real machine, I ran into two restore-related gaps:

  1. No one-click way back. After applying optimizations, there is no reliable path that returns all affected macOS settings to their factory defaults (toggling each setting off individually is tedious and still relies on per-setting restore state).
  2. Restoring by writing guessed default values is fragile. On macOS, an absent preference key means "use the system default". Writing a stored "default" value back can diverge from the real OS default, and at least one key misbehaves: com.apple.sound.beep.feedback (interface sound effects — volume beeps, drag-to-Trash sound). I observed that after deleting this key, macOS treated it as off on some systems, even though the documented default is on.

What this PR adds

scripts/restore-macos-defaults.sh, a maintenance script that restores every macOS preference System Optimization can touch:

  • Deletes the 70 preference keys written by System Optimization (mirroring the domain/key mapping in src-tauri/crates/mangodisk-platform/src/macos/system_settings.rs and the values in src-tauri/crates/mangodisk-core/src/system_settings/catalog.rs). Deleting a key restores the OS factory default instead of a guessed value.
  • Writes com.apple.sound.beep.feedback back to true explicitly instead of deleting it, for the reason above.
  • Backs up first: all 15 affected preference domains are exported to ~/Desktop/mango-restore-backup-<timestamp>/ before anything is deleted, so any domain can be rolled back with defaults import.
  • Only deletes keys from the list; untouched keys and unrelated settings are never modified. Keys that were never written are skipped silently.
  • Restarts Finder / Dock / SystemUIServer at the end so animations and Dock behavior re-apply immediately.

README updates (en / zh-CN / zh-TW / ja) document the script under "System Optimization".

Validation

  • bash -n passes; the script was executed end-to-end on a real macOS machine (Apple Silicon, macOS 26): it restored 51 previously overwritten keys, correctly skipped 20 keys that were already at their defaults, and the interface sound effects key was written back to true. Finder animations and Dock genie effect were confirmed restored after the restart.
  • No Rust, frontend, or locale resources are touched, so pnpm check and cargo test are unaffected by this change.
  • Script comments are English, per AGENTS.md; the script filename and paths follow the repository naming conventions.

Follow-up ideas (not in this PR)

  • Wire the same "delete = factory default" semantics into the in-app restore path.
  • Auto-export affected domains (the same defaults export backup) before applying optimizations in-app.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ac937ea01c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

# Domains that were never written cannot be exported either; skip them.
defaults export "$d" "$BACKUP_DIR/$d.plist" 2>/dev/null \
|| defaults read "$d" >"$BACKUP_DIR/$d.txt" 2>/dev/null \
|| true

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Abort before deletion when a required backup fails

If both defaults export and the fallback defaults read fail for an existing domain—for example because the backup volume fills—the final || true lets the script continue deleting preferences with no usable backup, contradicting its stated backup-first safety guarantee. Treat an unbacked existing domain as fatal before entering the deletion phase.

AGENTS.md reference: AGENTS.md:L29-L29

Useful? React with 👍 / 👎.

d="${entry%%|*}"
k="${entry#*|}"
if defaults read "$d" "$k" >/dev/null 2>&1; then
defaults delete "$d" "$k" >/dev/null 2>&1 || true

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Treat failed preference deletions as errors

When defaults delete returns nonzero, this suppression still prints restored, increments deleted, and ultimately reports success even though the key remains unchanged. This can leave a partially restored system after a managed-preference or transient write failure; check the exit status and verify the key is absent instead of counting the operation as successful.

AGENTS.md reference: AGENTS.md:L29-L29

Useful? React with 👍 / 👎.

echo ""
echo "Done: $deleted key(s) restored, $absent already at the default."
echo "Backup location: $BACKUP_DIR"
echo "Roll back one domain with: defaults import <domain> \"$BACKUP_DIR/<domain>.plist\""

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use the actual fallback filename in rollback instructions

For NSGlobalDomain, the script explicitly expects export to fail and writes the backup as NSGlobalDomain.txt, but this final instruction always points users to NSGlobalDomain.plist, which does not exist on that path. Consequently, the advertised rollback command fails for the domain containing twenty affected keys; print the recorded fallback path or normalize every successful backup to the documented filename.

AGENTS.md reference: AGENTS.md:L29-L29

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant