-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup-dock.py
222 lines (191 loc) · 6.16 KB
/
setup-dock.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python
# encoding: utf-8
"""
setup-dock.py
This script configures the Dock content the
way I like it. Use with caution...
Hannes Juutilainen <[email protected]>
Update to python3 by
Fabiano Frizzo <[email protected]>
"""
import io
import sys
import os
import subprocess
import plistlib
import getpass
# =======================================
# Standard Applications
# =======================================
appleApps = [
"/System/Applications/Launchpad.app",
"/System/Applications/Mission Control.app",
"/System/Applications/System Settings.app",
"/Applications/Safari.app"
]
# =======================================
# Optional Applications
# Add these if they exist or "forced"==True
# =======================================
thirdPartyApps = [
{
"path": "/Applications/Visual Studio Code.app",
"args": ["--after", "System Settings"],
"forced": True
},
{
"path": "/Applications/Warp.app",
"args": ["--after", "System Settings"],
"forced": True
},
{
"path": "/Applications/Spotify.app",
"args": ["--after", "Safari"],
"forced": True
},
{
"path": "/Applications/Slack.app",
"args": ["--after", "Safari"],
"forced": True
},
{
"path": "/Applications/Discord.app",
"args": ["--after", "Safari"],
"forced": True
},
{
"path": "/Applications/Firefox Developer Edition.app",
"args": ["--after", "Safari"],
"forced": True
},
{
"path": "/Applications/Google Chrome Canary.app",
"args": ["--after", "Safari"],
"forced": True
},
{
"path": "/Applications/Brave Browser.app",
"args": ["--after", "Safari"],
"forced": True
}
]
dockutilPath = ""
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def dockutilExists():
whichProcess = ["which", "dockutil"]
p = subprocess.Popen(whichProcess, bufsize=1,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(path, err) = p.communicate()
if os.path.exists(path.strip()):
global dockutilPath
dockutilPath = path.strip()
return True
else:
return False
def removeEverything(restartDock=False):
dockutilProcess = [dockutilPath, "--remove", "all"]
if not restartDock:
dockutilProcess.append("--no-restart")
p = subprocess.Popen(dockutilProcess, bufsize=1,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, err) = p.communicate()
pass
def dockutilAdd(aPath, args):
dockutilProcess = [dockutilPath, "--add", aPath]
if args:
dockutilProcess = dockutilProcess + args
dockutilProcess.append("--no-restart")
p = subprocess.Popen(dockutilProcess, bufsize=1,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, err) = p.communicate()
pass
def localDisks():
"""Run diskutil list -plist """
diskutilProcess = ["diskutil", "list", "-plist"]
p = subprocess.Popen(diskutilProcess, bufsize=1,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, err) = p.communicate()
if output != "":
outputPlist = plistlib.load(io.BytesIO(output))
return outputPlist['VolumesFromDisks']
else:
return None
def addFolders():
"""
Loop through all local disks and check if they contain:
<Disk>/Users/username/Documents
<Disk>/Users/username/Downloads
or
<Disk>/home/username/Documents
<Disk>/home/username/Downloads
Add everything that exists.
"""
username = getpass.getuser()
pathsToCheckInDisks = ["Users", "home"]
for aDisk in localDisks():
diskPath = os.path.join("/Volumes", aDisk)
for aPath in pathsToCheckInDisks:
homePath = os.path.join(diskPath, aPath, username)
homePath = os.path.realpath(homePath)
documents = os.path.join(homePath, "Documents")
downloads = os.path.join(homePath, "Downloads")
if os.path.exists(documents):
label = "Documents"
args = [
"--view", "grid",
"--display", "stack",
"--sort", "name",
"--label", label
]
dockutilAdd(documents, args)
print("Added %s" % documents)
if os.path.exists(downloads):
label = "Downloads"
args = [
"--view", "grid",
"--display", "stack",
"--sort", "dateadded",
"--label", label
]
dockutilAdd(downloads, args)
print("Added %s" % downloads)
def main(argv=None):
if argv is None:
argv = sys.argv
try:
if not dockutilExists():
print("dockutil not found")
print("Get it from https://github.com/kcrawford/dockutil")
print("or run \"git clone https://github.com/kcrawford/dockutil.git\"")
return 1
confirmation = input("Are you sure? y/n: ").lower()
if confirmation == 'y':
print("Continuing...")
elif confirmation == '' or confirmation == 'n':
raise Usage("Exiting...")
else:
print('Please enter y or n.')
return 1
# Start with an empty Dock
removeEverything(restartDock=False)
# Add standard Apple apps
for anApp in appleApps:
dockutilAdd(anApp, None)
print("Added %s" % anApp)
# Add 3rd party apps
for anApp in thirdPartyApps:
if os.path.exists(anApp["path"]) or anApp["forced"]:
dockutilAdd(anApp["path"], anApp["args"])
print("Added %s" % anApp["path"])
else:
print("Skipped %s" % anApp["path"])
# Add folders
addFolders()
print("Done. You might want to restart Dock by running \"killall Dock\"")
except Usage as err:
print(">> %s\n" % err, file=sys.stderr)
return 2
if __name__ == "__main__":
sys.exit(main())