Skip to content

Commit 026eed7

Browse files
committed
First commit.
0 parents  commit 026eed7

5 files changed

+304
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# macOS Initialization Scripts
2+
3+
> Modify some system preferences
4+
>
5+
> Change default file associations

association_changer.swift

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/xcrun swift
2+
3+
import Foundation
4+
5+
func main() -> Void {
6+
let args = CommandLine.arguments
7+
8+
if args.count != 3 {
9+
print("Invalid arguments.")
10+
return
11+
}
12+
13+
let ty = args[1]
14+
let handler = args[2]
15+
16+
if LSSetDefaultRoleHandlerForContentType(
17+
ty as CFString,
18+
LSRolesMask.all,
19+
handler as CFString
20+
) != 0 {
21+
print("Failed.")
22+
return
23+
}
24+
25+
print("Succeeded.")
26+
}
27+
28+
main()

change_file_associations.sh

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/zsh
2+
3+
set -xe
4+
5+
VSCODE='com.microsoft.vscode'
6+
7+
OPEN_WITH_VSCODE=(
8+
'net.daringfireball.markdown'
9+
'public.c-header'
10+
'public.objective-c-source'
11+
'public.plain-text'
12+
'public.patch-file'
13+
'public.python-script'
14+
'public.c-plus-plus-source'
15+
'com.netscape.javascript-source'
16+
'public.xml'
17+
'public.yaml'
18+
'public.protobuf-source'
19+
'public.html'
20+
)
21+
22+
OPEN_WITH_DUMB_BROWSER=(
23+
'public.url'
24+
)
25+
26+
# PDF
27+
./change_file_association.swift com.adobe.pdf com.readdle.pdfexpert-mac
28+
29+
# Open with VSCode
30+
for ty in $OPEN_WITH_VSCODE do
31+
./change_file_association.swift $ty $VSCODE
32+
done
33+
34+
# TODO: prevent duplications
35+
function register_extension {
36+
CONTENT="<dict>
37+
<key>LSHandlerContentTag</key>
38+
<string>$1</string>
39+
<key>LSHandlerContentTagClass</key>
40+
<string>public.filename-extension</string>
41+
<key>LSHandlerRoleAll</key>
42+
<string>$2</string>
43+
</dict>"
44+
# echo $CONTENT
45+
defaults write com.apple.LaunchServices/com.apple.launchservices.secure LSHandlers -array-add $CONTENT
46+
}
47+
48+
# For some custom exension names
49+
register_extension "gn" $VSCODE
50+
register_extension "gni" $VSCODE
51+
register_extension "meta" $VSCODE
52+
register_extension "toml" $VSCODE
53+
register_extension "props" $VSCODE
54+
55+
# Restart LaunchServices
56+
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain system -domain user

set_default_browser.swift

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/xcrun swift
2+
3+
import Cocoa
4+
5+
struct ProgramOption {
6+
var list : Bool = false
7+
var set : String? = nil
8+
var help : Bool = false
9+
}
10+
11+
public func checkError(_ error: OSStatus) -> Void
12+
{
13+
if (error == noErr) { return }
14+
15+
let count = 5
16+
let stride = MemoryLayout<OSStatus>.stride
17+
let byteCount = stride * count
18+
19+
var error_ = CFSwapInt32HostToBig(UInt32(error))
20+
var charArray: [CChar] = [CChar](repeating: 0, count: byteCount )
21+
withUnsafeBytes(of: &error_) { (buffer: UnsafeRawBufferPointer) in
22+
for (index, byte) in buffer.enumerated() {
23+
charArray[index + 1] = CChar(byte)
24+
}
25+
}
26+
27+
let v1 = charArray[1], v2 = charArray[2], v3 = charArray[3], v4 = charArray[4]
28+
29+
if (isprint(Int32(v1)) > 0 && isprint(Int32(v2)) > 0 && isprint(Int32(v3)) > 0 && isprint(Int32(v4)) > 0) {
30+
charArray[0] = "\'".utf8CString[0]
31+
charArray[5] = "\'".utf8CString[0]
32+
let errStr = NSString(bytes: &charArray, length: charArray.count, encoding: String.Encoding.ascii.rawValue)
33+
print("Error: (\(errStr!))")
34+
}
35+
else {
36+
print("Error: \(error)")
37+
}
38+
39+
}
40+
41+
func showInstalledBrowsers() {
42+
if let array = LSCopyApplicationURLsForURL(URL(string: "https:")! as CFURL, .all)?.takeRetainedValue() as? [URL] {
43+
for i in 0..<array.count {
44+
let bundleId = array[i]
45+
46+
if let bundle = Bundle(url: bundleId) {
47+
print(bundle.bundleIdentifier!, bundle.bundlePath)
48+
}
49+
}
50+
}
51+
}
52+
53+
func setDefaultBrowser(bundleId: String) -> Bool {
54+
let httpResult = LSSetDefaultHandlerForURLScheme("http" as CFString, bundleId as CFString)
55+
// let httpsResult = LSSetDefaultHandlerForURLScheme("https" as CFString, bundleId as CFString)
56+
57+
if httpResult == noErr /*&& httpsResult == noErr*/ {
58+
return true
59+
} else {
60+
checkError(httpResult)
61+
// checkError(httpsResult)
62+
return false
63+
}
64+
}
65+
66+
var option = ProgramOption()
67+
68+
for var i in 0..<CommandLine.arguments.count {
69+
let arg = CommandLine.arguments[i]
70+
switch arg {
71+
case "-h", "--help":
72+
option.help = true
73+
case "-l", "--list":
74+
option.list = true
75+
case "-s", "--set":
76+
i += 1
77+
option.set = CommandLine.arguments[i]
78+
default:
79+
break
80+
}
81+
}
82+
83+
if option.help {
84+
print("-h, --help: Show help (this)")
85+
print("-l, --list: Show installed browser list")
86+
print("-s [bundleId], --set [bundleId]: Set default browser to specified browser")
87+
exit(0)
88+
}
89+
90+
if option.list {
91+
showInstalledBrowsers()
92+
}
93+
94+
if let bundleId = option.set {
95+
if setDefaultBrowser(bundleId: bundleId) {
96+
print("Default browser was set to", bundleId)
97+
exit(0)
98+
} else {
99+
print("Error occured when setting default browser.")
100+
exit(1)
101+
}
102+
}
103+
104+
exit(0)

set_preferences.sh

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/zsh
2+
3+
OS_USER=`whoami`
4+
5+
# default input methods
6+
read -r -d '' DEFAULT_INPUT_METHODS << EOF
7+
[{
8+
"InputSourceKind": "Keyboard Layout",
9+
"KeyboardLayout ID": "252",
10+
"KeyboardLayout Name": "ABC"
11+
},
12+
{
13+
"Bundle ID": "com.apple.inputmethod.SCIM",
14+
"Input Mode": "com.apple.inputmethod.SCIM.ITABC",
15+
"InputSourceKind": "Input Mode"
16+
},
17+
{
18+
"Bundle ID": "com.apple.CharacterPaletteIM",
19+
"InputSourceKind": "Non Keyboard Input Method"
20+
},
21+
{
22+
"Bundle ID": "com.apple.inputmethod.EmojiFunctionRowItem",
23+
"InputSourceKind": "Non Keyboard Input Method"
24+
},
25+
{
26+
"Bundle ID": "im.rime.inputmethod.Squirrel",
27+
"Input Mode": "im.rime.inputmethod.Squirrel",
28+
"InputSourceKind": "Input Mode"
29+
},
30+
{
31+
"Bundle ID": "im.rime.inputmethod.Squirrel",
32+
"InputSourceKind": "Keyboard Input Method"
33+
}]
34+
EOF
35+
36+
set -xe
37+
38+
# disable Gatekeeper
39+
sudo spctl --master-disable
40+
41+
# set display sleep timer
42+
sudo pmset -a displaysleep 60
43+
sudo pmset -b displaysleep 15
44+
45+
# automatically hide menu bar
46+
defaults write NSGlobalDomain _HIHideMenuBar -bool YES
47+
48+
# fastest key repeat & shortest delay until repeat
49+
defaults write NSGlobalDomain InitialKeyRepeat -int 35
50+
defaults write NSGlobalDomain KeyRepeat -int 12
51+
52+
# Dock magnification
53+
# defaults write com.apple.dock magnification -bool YES
54+
# Dock tile size
55+
# defaults write com.apple.dock tilesize -int 16
56+
57+
# Prevent Time Machine from Prompting to Use New Hard Drives as Backup Volume
58+
sudo defaults write /Library/Preferences/com.apple.TimeMachine DoNotOfferNewDisksForBackup -bool YES
59+
60+
# Add a spcace to Dock
61+
# defaults write com.apple.dock persistent-apps -array-add '{"tile-type"="spacer-tile";}'
62+
63+
# Lock Dock size
64+
defaults write com.apple.Dock size-immutable -bool YES
65+
66+
# Disable Dock bouncing to get attention
67+
# defaults write com.apple.Dock no-bouncing -bool YES
68+
69+
# disable disk image verification
70+
defaults write com.apple.frameworks.diskimages skip-verify -bool YES
71+
defaults write com.apple.frameworks.diskimages skip-verify-locked -bool YES
72+
defaults write com.apple.frameworks.diskimages skip-verify-remote -bool YES
73+
74+
# Expand Save Panel by Default
75+
defaults write -g NSNavPanelExpandedStateForSaveMode -bool YES
76+
defaults write -g NSNavPanelExpandedStateForSaveMode2 -bool YES
77+
78+
# Save to Disk by Default
79+
defaults write -g NSDocumentSaveNewDocumentsToCloud -bool NO
80+
81+
# Finder prefereneces
82+
defaults write com.apple.finder ShowPathbar -bool YES
83+
defaults write com.apple.finder FXDefaultSearchScope -string "SCcf"
84+
defaults write com.apple.finder FXEnableExtensionChangeWarning -bool NO
85+
defaults write com.apple.finder FXEnableRemoveFromICloudDriveWarning -bool YES
86+
defaults write com.apple.finder NewWindowTarget PfHm
87+
defaults write com.apple.finder NewWindowTargetPath "file:///Users/$OS_USER/"
88+
defaults write com.apple.finder PreferencesWindow.LastSelection "ADVD"
89+
defaults write com.apple.finder ShowExternalHardDrivesOnDesktop -bool NO
90+
defaults write com.apple.finder ShowRecentTags -bool NO
91+
defaults write com.apple.finder ShowRemovableMediaOnDesktop -bool NO
92+
defaults write com.apple.finder _FXSortFoldersFirst -bool YES
93+
defaults write com.apple.finder _FXSortFoldersFirstOnDesktop -bool YES
94+
95+
# Disable Creation of Metadata Files on Network Volumes
96+
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool YES
97+
98+
# Disable Creation of Metadata Files on USB Volumes
99+
defaults write com.apple.desktopservices DSDontWriteUSBStores -bool YES
100+
101+
# hide icons in menu bar
102+
defaults write com.apple.Spotlight 'NSStatusItem Visible Item-0' -bool NO
103+
defaults write com.apple.airplay 'NSStatusItem Visible Item-0' -bool NO
104+
defaults write com.apple.controlcenter 'NSStatusItem Visible Battery' -bool NO
105+
defaults write com.apple.Siri "StatusMenuVisible" -bool NO
106+
107+
# block ocsp.apple.com
108+
# sudo echo "0.0.0.0 ocsp.apple.com" >> /etc/hosts
109+
110+
# set default input methods
111+
sudo plutil -replace AppleEnabledInputSources -json $DEFAULT_INPUT_METHODS /Library/Preferences/com.apple.HIToolbox.plist

0 commit comments

Comments
 (0)