-
Notifications
You must be signed in to change notification settings - Fork 2
/
nosx.hta
227 lines (197 loc) · 7.33 KB
/
nosx.hta
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
223
224
225
226
227
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
nosx.hta version 1.1
Clean up junk files left behind by OS X
by Mark Shroyer - http://markshroyer.com/
Wed Oct 31 10:10:18 EDT 2012
-->
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>nosx</title>
<hta:application
applicationname="nosx"
windowstate="normal"
scroll="no"
/>
<style type="text/css">
body {
font-family: "Verdana";
font-size: 10pt;
background-color: #eee;
}
div#controls {
text-align: center;
margin-top: 20px;
}
div#controls input[type="text"] {
width: 240px;
}
div#controls input[type="button"] {
width: 70px;
}
div#controls div#group3 {
margin-top: 12px;
}
div#statusline {
text-align: center;
font-style: italic;
margin-top: 22px;
color: #444;
}
</style>
<script type="text/javascript">
/* <![CDATA[ */
var BIF_EDITBOX = 0x00000010;
var BIF_VALIDATE = 0x00000020;
var BIF_NEWDIALOGSTYLE = 0x00000040;
var BIF_USENEWUI = BIF_EDITBOX | BIF_NEWDIALOGSTYLE;
var sa = new ActiveXObject('Shell.Application');
var fs = new ActiveXObject('Scripting.FileSystemObject');
window.resizeTo(480, 260);
function StatusReport() {
this.removedCount = 0;
this.errorItems = [];
}
StatusReport.prototype.logRemoved = function (item) {
this.removedCount++;
}
StatusReport.prototype.getRemoved = function () {
return this.removedCount;
}
StatusReport.prototype.logError = function (item) {
this.errorItems.push(item);
}
StatusReport.prototype.getErrors = function () {
return this.errorItems.length;
}
StatusReport.prototype.notifyErrors = function () {
var note = "Was unable to remove the following items:\n\n";
for ( var i = 0; i < this.errorItems.length; i++ ) {
note = note + this.errorItems[i] + "\n";
}
window.alert(note);
}
function cleanFolderTree(folder, report, recurse) {
var fileEnum = new Enumerator(folder.Files);
while ( ! fileEnum.atEnd() ) {
var file = fileEnum.item();
if ( file.Name == '.DS_Store' || file.Name.match(/^\._/) ) {
try {
file.Delete(true);
report.logRemoved(file);
} catch (err) {
report.logError(file);
}
}
fileEnum.moveNext();
}
var folderEnum = new Enumerator(folder.SubFolders);
while ( ! folderEnum.atEnd() ) {
var folder = folderEnum.item();
if ( folder.Name == '.Trashes' ) {
try {
folder.Delete(true);
report.logRemoved(folder);
} catch (err) {
report.logError(folder);
}
} else if ( recurse ) {
cleanFolderTree(folder, report, true);
}
folderEnum.moveNext();
}
}
// Browse button event handler.
function browse() {
var target = document.getElementById('target');
var selection = sa.BrowseForFolder(
0,
'Please select a folder to clean...',
BIF_VALIDATE
);
if ( selection ) {
var path = selection.Self.Path;
// Some non-physical items exist in the folder selection
// dialog, e.g. "Computer", etc., and choosing one of these
// results in a GUID beginning with "::". Don't set the
// target input field if the user selects one of these...
if ( ! path.match(/^::/) )
target.value = path;
}
}
function nify(n, singular, plural) {
if ( n == 1 )
return n.toString() + " " + singular;
else
return n.toString() + " " + plural;
}
// Clean button event handler; entry point for the cleanFolderTree()
// recursive function.
function clean() {
var statusline = document.getElementById('statusline');
var recurse = document.getElementById('recurse');
var path = document.getElementById('target').value;
if ( fs.FolderExists(path) ) {
// If the user entered a valid folder path...
var folder = fs.GetFolder(path);
var report = new StatusReport();
cleanFolderTree(folder, report, recurse.checked);
// folder.Name doesn't work for drive root directories.
var name = folder.Name || path;
if ( name.match(/^[a-zA-Z]:\\?$/) )
name = name.substr(0, 1);
// Good grammar. Its importent.
if ( report.getErrors() == 0 ) {
if ( report.getRemoved() == 0 ) {
statusline.innerHTML = 'Cleaned '
+ name
+ ': Nothing to remove!';
} else {
statusline.innerHTML = 'Cleaned '
+ name
+ ': Removed '
+ nify(report.getRemoved(), 'item', 'items')
+ '.';
}
} else {
statusline.innerHTML = 'Cleaned '
+ name
+ ': Removed '
+ nify(report.getRemoved(), 'item', 'items')
+ ', encountered <a href="#" id="errorlink">'
+ nify(report.getErrors(), 'error', 'errors')
+ '</a>.';
document.getElementById('errorlink').onclick = function () {
report.notifyErrors();
}
}
} else {
// Or if the user-provided folder path was less valid...
statusline.innerHTML = "That's not a valid folder. "
+ "Try the Browse button...";
}
}
/* ]]> */
</script>
</head>
<body>
<p>Select a folder from which to remove the junk files
(<code>.DS_Store</code>, <code>.Trashes</code>, <code>._*</code>) left
on shared media by OS X.</p>
<div id="controls">
<div id="group1">
<input type="text" id="target" value="" />
<input type="button" id="browse" value="Browse" onclick="browse();" />
</div>
<div id="group2">
<input type="checkbox" id="recurse" value="recurse" checked="checked" />
<label for="recurse">Subfolders too</label>
</div>
<div id="group3">
<input type="button" id="clean" value="Clean!" onclick="clean();" />
</div>
</div>
<div id="statusline"></div>
</body>
</html>