Skip to content

Commit edb69e1

Browse files
committed
Update file association scripts.
1 parent 2b4c917 commit edb69e1

6 files changed

+101
-59
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# macOS Initialization Scripts
22

3-
> Modify some system preferences
4-
>
5-
> Change default file associations
3+
My preferences.
4+
5+
1. Modify system preferences
6+
2. Change default file associations
7+
8+
Get more from https://github.com/kevinSuttle/macOS-Defaults

associate_changer_file_extensions.zsh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/zsh
2+
3+
export LS_PLIST="$HOME/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist"
4+
5+
function associate_extension {
6+
EXT=$1
7+
APP=$2
8+
9+
i=0
10+
while true; do
11+
# Check if we reach the end of LSHandlers array.
12+
CHECK_EXIST_CMD='Print :LSHandlers:'$i
13+
EXIST=$(/usr/libexec/PlistBuddy -c "$CHECK_EXIST_CMD" $LS_PLIST 2>/dev/null)
14+
if [ $? -ne 0 ]; then
15+
break
16+
fi
17+
18+
CHECK_TAG_CMD='Print :LSHandlers:'$i':LSHandlerContentTag'
19+
# Check if there is a LSHandlerContentTag and it is the one we need to modify.
20+
PREVIOUS=$(/usr/libexec/PlistBuddy -c "$CHECK_TAG_CMD" $LS_PLIST 2>/dev/null)
21+
if [[ "$PREVIOUS" = "$EXT" ]]; then
22+
# Found. Modify it!
23+
echo "Modified existing association: *.$EXT -> $APP"
24+
MODIFY_CMD='Set :LSHandlers:'$i':LSHandlerRoleAll $APP'
25+
/usr/libexec/PlistBuddy -c "$MODIFY_CMD" $LS_PLIST
26+
return
27+
fi
28+
29+
i=$((i+1))
30+
done
31+
32+
# Exist entry not found. Just create a new one.
33+
CONTENT="<dict>
34+
<key>LSHandlerContentTag</key>
35+
<string>$EXT</string>
36+
<key>LSHandlerContentTagClass</key>
37+
<string>public.filename-extension</string>
38+
<key>LSHandlerPreferredVersions</key>
39+
<dict>
40+
<key>LSHandlerRoleAll</key>
41+
<string>-</string>
42+
</dict>
43+
<key>LSHandlerRoleAll</key>
44+
<string>$APP</string>
45+
</dict>"
46+
defaults write com.apple.LaunchServices/com.apple.launchservices.secure LSHandlers -array-add $CONTENT
47+
48+
echo "Created new association: *.$EXT -> $APP"
49+
}

association_changer.swift association_changer_public_uti.swift

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/xcrun swift
22

3+
// For public.* UTIs.
4+
35
import Foundation
46

57
func main() -> Void {

change_file_associations.sh

-56
This file was deleted.

change_file_associations.zsh

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/zsh
2+
3+
source $PWD/associate_changer_file_extensions.zsh
4+
5+
set -xe
6+
7+
VSCODE='com.microsoft.vscode'
8+
9+
OPEN_WITH_VSCODE=(
10+
'net.daringfireball.markdown'
11+
'public.c-header'
12+
'public.objective-c-source'
13+
'public.plain-text'
14+
'public.patch-file'
15+
'public.python-script'
16+
'public.c-plus-plus-source'
17+
'com.netscape.javascript-source'
18+
'public.xml'
19+
'public.yaml'
20+
'public.protobuf-source'
21+
'public.html'
22+
)
23+
24+
OPEN_WITH_DUMB_BROWSER=(
25+
'public.url'
26+
)
27+
28+
# PDF
29+
$PWD/association_changer_public_uti.swift com.adobe.pdf com.readdle.pdfexpert-mac
30+
31+
# Open with VSCode
32+
for ty in $OPEN_WITH_VSCODE do
33+
$PWD/association_changer_public_uti.swift $ty $VSCODE
34+
done
35+
36+
# For some custom exension names
37+
associate_extension "gn" $VSCODE
38+
associate_extension "gni" $VSCODE
39+
associate_extension "meta" $VSCODE
40+
associate_extension "toml" $VSCODE
41+
associate_extension "props" $VSCODE
42+
43+
# Restart LaunchServices
44+
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain system -domain user
File renamed without changes.

0 commit comments

Comments
 (0)