Skip to content

Commit 8219f6f

Browse files
committed
Use Swift verion of file association changer.
1 parent edb69e1 commit 8219f6f

4 files changed

+54
-93
lines changed

associate_changer_file_extensions.zsh

-49
This file was deleted.

association_changer.swift

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/xcrun swift
2+
3+
import Darwin
4+
import Foundation
5+
6+
func main() -> Void {
7+
let args = CommandLine.arguments
8+
9+
if args.count != 4 {
10+
print("Invalid arguments.")
11+
exit(1)
12+
}
13+
14+
let type = args[1]
15+
let ext = args[2] as CFString
16+
let bundleId = args[3] as CFString
17+
18+
let ret: OSStatus
19+
if type == "pub" {
20+
ret = LSSetDefaultRoleHandlerForContentType(ext, LSRolesMask.all, bundleId)
21+
}
22+
else if type == "ext" {
23+
let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, ext, nil)!.takeUnretainedValue()
24+
ret = LSSetDefaultRoleHandlerForContentType(uti, LSRolesMask.all, bundleId)
25+
}
26+
else {
27+
print("Invalid UTI type.")
28+
exit(1)
29+
}
30+
31+
if ret == kOSReturnSuccess {
32+
print("Succeeded.")
33+
}
34+
else {
35+
print("Failed to associate \(ext) with \(bundleId).")
36+
exit(1)
37+
}
38+
}
39+
40+
main()
41+
exit(0)

association_changer_public_uti.swift

-30
This file was deleted.

change_file_associations.zsh

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#!/bin/zsh
22

3-
source $PWD/associate_changer_file_extensions.zsh
4-
53
set -xe
64

75
VSCODE='com.microsoft.vscode'
86

9-
OPEN_WITH_VSCODE=(
7+
OPEN_UTI_WITH_VSCODE=(
108
'net.daringfireball.markdown'
119
'public.c-header'
1210
'public.objective-c-source'
@@ -21,24 +19,25 @@ OPEN_WITH_VSCODE=(
2119
'public.html'
2220
)
2321

24-
OPEN_WITH_DUMB_BROWSER=(
25-
'public.url'
22+
OPEN_EXT_WITH_VSCODE=(
23+
'gn'
24+
'gni'
25+
'meta'
26+
'toml'
27+
'props'
2628
)
2729

2830
# PDF
29-
$PWD/association_changer_public_uti.swift com.adobe.pdf com.readdle.pdfexpert-mac
31+
$PWD/association_changer.swift pub com.adobe.pdf com.readdle.pdfexpert-mac
3032

3133
# Open with VSCode
32-
for ty in $OPEN_WITH_VSCODE do
33-
$PWD/association_changer_public_uti.swift $ty $VSCODE
34+
for ty in $OPEN_UTI_WITH_VSCODE do
35+
$PWD/association_changer_public_uti.swift pub $ty $VSCODE
3436
done
3537

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
38+
for ty in $OPEN_EXT_WITH_VSCODE do
39+
$PWD/association_changer_public_uti.swift ext $ty $VSCODE
40+
done
4241

4342
# Restart LaunchServices
4443
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain system -domain user

0 commit comments

Comments
 (0)