Skip to content

Instantly share code, notes, and snippets.

@scaryrawr
Created February 1, 2026 02:02
Show Gist options
  • Select an option

  • Save scaryrawr/fd8988212efdc71ef02416308e90573b to your computer and use it in GitHub Desktop.

Select an option

Save scaryrawr/fd8988212efdc71ef02416308e90573b to your computer and use it in GitHub Desktop.
autoduti: setup your default text editor on macOS using duti
#!/usr/bin/env bash
die() { echo "Error: $*" >&2; exit 1; }
main() {
set -euo pipefail
command -v duti >/dev/null 2>&1 || die "duti not installed"
local app_name bundle_id
app_name="${*:-}"
[ -n "$app_name" ] || die "Usage: $0 <app name>"
if ! bundle_id="$(osascript \
-e 'on run argv' \
-e 'id of application (item 1 of argv)' \
-e 'end run' \
-- "$app_name" | tr -d '\r\n')"; then
# Fallback: allow passing a bundle id directly (must contain at least one dot).
if [[ "$app_name" =~ ^[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)+$ ]]; then
bundle_id="$app_name"
else
die "could not resolve bundle id for: $app_name"
fi
fi
[ -n "$bundle_id" ] || die "could not resolve bundle id for: $app_name"
[[ "$bundle_id" =~ ^[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)+$ ]] || die "unexpected bundle id: $bundle_id"
echo "Setting $app_name ($bundle_id) as default application for various file types..."
# JSON and JSONC files
duti -s "$bundle_id" .jsonc all
duti -s "$bundle_id" .json all
# Markdown files
duti -s "$bundle_id" .md all
# Source code files
duti -s "$bundle_id" public.source-code all
# Text files (extensions + UTI)
duti -s "$bundle_id" public.plain-text all
duti -s "$bundle_id" .txt all
# TypeScript/JavaScript variants
duti -s "$bundle_id" .tsx all
duti -s "$bundle_id" .ts all
duti -s "$bundle_id" .jsx all
duti -s "$bundle_id" .js all
echo "All associations set successfully!"
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
main "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment