Skip to main content

A macOS command-line utility for switching default file associations by file extension

Project description

macdefault

A macOS command-line utility for switching default file associations by file extension.

Overview

macdefault manages default application associations for common office document formats using macOS's duti utility. Switch entire office suites with a single command instead of manually changing file associations through Finder.

Supported file extensions:

  • Word: doc, docx
  • Excel: xls, xlsx
  • PowerPoint: ppt, pptx
  • Other: rtf, csv

Supported application suites:

  • Microsoft Office - Word, Excel, PowerPoint
  • WPS Office - Single application (Kingsoft)
  • Apple iWork - Pages, Numbers, Keynote, TextEdit

Requirements

  • macOS only (uses LaunchServices and duti)
  • Python 3.10+
  • duti - Install via: brew install duti
  • uv - Python package manager (installation)
  • Python deps - Installed automatically by uv (click, questionary, wcwidth)

Installation

Quick Start with uv

# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh

# Install duti
brew install duti

# Run directly without installation
uv run macdefault --help

Install as a Tool

# Install globally with uv
uv tool install .

# Now you can run it anywhere
macdefault --help

Development Setup

# Clone the repository
git clone https://github.com/ac1982/macdefault.git
cd macdefault

# uv will automatically create a virtual environment and install dependencies
uv sync

# Run in development mode
uv run macdefault --help

Usage

Switch to a Suite

Switch all supported file types to Microsoft Office:

uv run macdefault --microsoft
# or
uv run macdefault --office

Switch to WPS Office:

uv run macdefault --wps
# or
uv run macdefault --kingsoft

Switch to Apple iWork suite:

uv run macdefault --apple

Interactive Mode

Choose a default app for a specific extension:

uv run macdefault --ext=doc

This will:

  1. Show the current default handler
  2. List all installed apps that support .doc files
  3. Let you select one from the list using Up/Down arrows or by typing a number (1-9), press q to cancel
  4. Apply the changes

Show candidates without making changes:

uv run macdefault --ext=doc --show

Diagnostics

Print a summary of installed office suites:

uv run macdefault --print-bundle-ids

Full diagnostic output (bundle IDs + current defaults):

uv run macdefault --doctor

This shows:

  • Which office apps are installed
  • Their bundle IDs and paths
  • Current default handlers for all managed extensions

Additional Options

Dry run (preview changes without applying):

uv run macdefault --microsoft --dry-run

Disable verification (skip checking if changes succeeded):

uv run macdefault --wps --no-verify

Fail fast (stop on first error):

uv run macdefault --apple --fail-fast

How It Works

  1. Discovery: Scans common application directories (/Applications, etc.) for installed apps
  2. UTI Matching: Uses macOS Uniform Type Identifiers to find apps that support each file extension
  3. LaunchServices: Registers apps with lsregister when needed
  4. duti: Applies the default handler settings via duti -s <bundle-id> <extension> all
  5. Verification: Checks that changes were applied correctly

Special Handling

  • WPS Office: Uses a stable path (/Applications/wpsoffice.app) since LaunchServices name lookup can be unreliable
  • Microsoft Word .doc files: Includes automatic repair logic if the default doesn't stick, re-registering with LaunchServices and applying multiple UTI variants

Examples

Typical Workflow

Check what's currently set:

uv run macdefault --doctor

Switch to Microsoft Office:

uv run macdefault --microsoft

Verify the changes:

uv run macdefault --doctor

Choose Custom Handler for One Extension

Set a different app for .md files:

uv run macdefault --ext=md

See what handles .csv without changing:

uv run macdefault --ext=csv --show

Troubleshooting

"duti not found"

  • Install via: brew install duti

"Microsoft Office apps not fully resolvable"

  • Ensure Word, Excel, and PowerPoint are installed in /Applications
  • Try running uv run macdefault --print-bundle-ids to see what's detected

Changes don't stick for .doc files

  • The script includes automatic repair logic for this common issue
  • It re-registers Word with LaunchServices and tries multiple UTI identifiers

App not appearing in --ext list

  • The app must declare support for that file type in its Info.plist
  • As a workaround, use Finder: right-click file → Open With → Other → Change All

Project Structure

macdefault/
├── macdefault.py       # Main script
├── pyproject.toml      # Project metadata and dependencies
├── README.md           # This file
├── .python-version     # Python version specification
└── .gitignore          # Git ignore rules

Development

Running Tests

# Add tests in the future
uv run pytest

Code Formatting

# Add formatters as dev dependencies if needed
uv add --dev ruff black
uv run black macdefault.py
uv run ruff check macdefault.py

Technical Details

  • Uses mdls for fast metadata queries when available
  • Falls back to parsing Info.plist directly
  • Filters out overly-generic UTIs (public.data, public.item, public.content)
  • Supports wide Unicode characters in table output via wcwidth
  • Interactive mode supports ESC to cancel (when running in a TTY)

License

MIT License - See LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

macdefault-0.2.1.tar.gz (46.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

macdefault-0.2.1-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file macdefault-0.2.1.tar.gz.

File metadata

  • Download URL: macdefault-0.2.1.tar.gz
  • Upload date:
  • Size: 46.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for macdefault-0.2.1.tar.gz
Algorithm Hash digest
SHA256 9fe07d9cbd777087c2dfd1b9e9ba055043c13ba9f0f9e13ecf8d44b410097f15
MD5 eb0c16106a4b3984a4d50959929ecda9
BLAKE2b-256 1ecfd6b007b99cb407c6b69338e203ae5d49aa9f7449eb9028d8b17bbf64682b

See more details on using hashes here.

File details

Details for the file macdefault-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: macdefault-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for macdefault-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d6fa543461813a2bb87795d705d54e93feb4e14806d6d7ccfe372c1df767f1ed
MD5 ea686322975cbfbbc4ab66325a04bf07
BLAKE2b-256 6d7c1016ea4096932475a9816b17d469537686fd44f8136df653ae82f8a7a762

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page