-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-file-extension-handler
executable file
·127 lines (113 loc) · 3.22 KB
/
set-file-extension-handler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/sh
set -e
set -u
fn_version() { (
echo "set-file-extension-handler v1.0.1 (2024-08-31)"
); }
fn_help() { (
echo ""
echo "USAGE"
echo " set-file-extension-handler <app> <extensions>"
echo ""
echo "EXAMPLES"
echo " set-file-extension-handler 'Brave Browser' .html .htm .svg"
echo " set-file-extension-handler 'BBEdit' .css .go .js .json .rs .sh .ts .txt .zig"
echo " set-file-extension-handler 'Markdown Editor' .mkdn .md"
echo ""
); }
fn_ensure_duti() { (
if command -v duti > /dev/null; then
return
fi
printf "\n"
printf "MISSING 'duti'\n\n"
printf " 'duti' is required to set file extension handlers\n"
printf " https://formulae.brew.sh/formula/duti \n"
printf "\n"
printf "Install 'duti' now? [Y/n] "
read -r b_yes
case "${b_yes:-}" in
"NO" | "No" | "N" | "no" | "n")
{
echo "abort: install 'duti', then run again"
echo " brew install duti"
} >&2
exit 1
;;
*) ;;
esac
if ! command -v brew > /dev/null; then
printf "\n"
printf "MISSING 'brew'\n\n"
printf " 'brew' can install 'duti', which doesn't have its own releases\n"
printf " https://webinstall.dev/brew/ \n"
printf " https://docs.brew.sh/Installation#alternative-installs \n"
printf "\n"
printf "Install 'brew' now? [Y/n] "
read -r b_yes
case "${b_yes:-}" in
"NO" | "No" | "N" | "no" | "n")
{
echo "abort: install 'brew' and 'duti', then run again"
echo " curl https://webi.sh/brew | sh"
echo " source ~/.config/envman/PATH.env"
echo " brew install duti"
} >&2
exit 1
;;
*) ;;
esac
curl --fail-with-body -sS https://webi.sh/brew | sh
# shellcheck disable=SC1090
. ~/.config/envman/PATH.env
fi
{
brew install duti
echo ""
if duti -h 2>&1 | grep -q 'duti -s'; then
echo "'duti' installed successfully"
else
echo "warn: 'duti' not installed correctly, or help output has changed"
fi
echo ""
} >&2
sleep 0.3
); }
main() { (
case "${1:-}" in
"")
fn_help >&2
exit 1
;;
"--help" | "help")
fn_version
fn_help
exit 0
;;
"-V" | "--version" | "version")
fn_version
exit 0
;;
esac
fn_ensure_duti
b_app=${1}
shift
# Example:
# osascript -e 'id of app "Brave Browser Beta"'
# osascript -e 'id of app "CotEditor"'
b_app_id="$(
osascript -e "id of app \"${b_app}\""
)"
echo ""
printf "set \033[32m%s\033[0m \033[2m(%s)\033[0m as default app for...\n" "${b_app}" "${b_app_id}"
echo ""
while test -n "${1:-}"; do
b_extension="${1}"
printf " \033[33m%s\033[0m\n" "${b_extension}"
duti -s "${b_app_id}" "${b_extension}" all
shift
done
echo ""
echo "done"
); }
main "${@:-}"