-
Notifications
You must be signed in to change notification settings - Fork 0
/
empty-trashes
executable file
·277 lines (259 loc) · 7.8 KB
/
empty-trashes
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/zsh
# shellcheck shell=bash
# empty-trashes
# v0.9.17
# macOS
#
# Empty Trashes - part of Trash Tools
# Copyright (c) 2021 Joss Brown (pseud.)
# License: MIT / place of jurisdiction: Berlin, Germany / German laws apply
#
# keyboard shortcut: CMD-SHIFT-DEL | CMD-OPT-SHIFT-DEL (force)
#
# (force) empty all trashes: user trash, data volume trash & trashes on mounted volumes
force=false
if [[ $1 == "--force" ]] ; then
force=true
shift
fi
uiprocess="Empty Trashes"
account=$(id -u)
tempdir="$HOME/.cache/Finder"
configdir="$HOME/.config/TrashTools"
undoloc="/tmp/local.lcars.TrashTools.$account.trashes"
icloud="$HOME/Library/Mobile Documents/com~apple~CloudDocs"
fman="$configdir/file_manager.txt"
fman_name=$(head -1 < "$fman" 2>/dev/null)
! [[ $fman_name ]] && fman_name="Finder"
_beep () {
osascript -e 'beep' -e 'delay 0.5' &>/dev/null
}
_notify () {
osascript &>/dev/null << EOT
tell application "System Events"
display notification "$2" with title "$uiprocess [" & "$account" & "]" subtitle "$1"
end tell
EOT
}
_all-trashes () {
othertrashes=$(df | tail -n +2 | grep -v -e "/$" -e "/dev$" -e "/home$" -e "/System/Volumes/" | awk '{print substr($0, index($0,$9))}' | sed 's-$-/\.Trashes/'"$account"'-')
echo -e "$HOME/.Trash\n$icloud/.Trash\n$othertrashes\n/System/Volumes/Data/.Trashes/$account"
}
_list-trashed () {
alltrashed=""
while read -r scanloc
do
if [[ $scanloc == "$HOME/.Trash" ]] ; then
locinfo="Home"
elif [[ $scanloc == "$icloud/.Trash" ]] ; then
locinfo="iCloud"
elif [[ $scanloc == "/System/Volumes/Data/Trashes/$account" ]] ; then
locinfo="Data Volume Root"
else
locinfo=$(echo "$scanloc" | sed 's-/\.Trashes/'"$account"'$--')
locinfo=$(basename "$locinfo")
fi
trashed=$(find "$scanloc" -mindepth 1 -maxdepth 1 2>/dev/null | sed "s-^$scanloc/--g" | sort -f | awk -v len=17 '{ if (length($0) > len) print substr($0, 1, len-3) "…" substr($0, length($0) - len, length($0)); else print; }')
if [[ $trashed ]] ; then
trashed=$(echo "$trashed" | grep -v "^\.DS_Store$")
! [[ $trashed ]] && trashed=".DS_Store"
alltrashed="$alltrashed\n\n🗑 $locinfo\n$trashed"
fi
done < <(_all-trashes)
alltrashed=$(echo -e "$alltrashed" | tail -n +3)
lchoice=$(osascript 2>/dev/null << EOL
tell application "System Events"
activate
set theTrashIcon to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/FullTrashIcon.icns"
set theButton to button returned of (display dialog "$alltrashed" ¬
buttons {"Cancel", "Continue & Empty Trashes"} ¬
cancel button 1 ¬
default button 2 ¬
with title ¬
"Empty Trashes - All Contents" ¬
with icon theTrashIcon ¬
giving up after 180)
end tell
EOL
)
if [[ $lchoice != "Continue & Empty Trashes" ]] ; then
exit
fi
}
_all-volumes () {
othervols=$(df | tail -n +2 | grep -v -e "/$" -e "/dev$" -e "/home$" -e "/System/Volumes/" | awk '{print substr($0, index($0,$9))}')
echo -e "$icloud\n$othervols\n/System/Volumes/Data"
}
_totalsize () {
usersize=$(osascript -e 'set theTrashSize to size of (info for (path to trash folder))' 2>/dev/null)
othersize=0
vol_list=$(_all-volumes | grep -v "^$")
if [[ $vol_list ]] ; then
while read -r volume
do
if ! [[ -w "$volume" ]] ; then
sizeadd=0
else
trashesloc="$volume/.Trash"
if ! [[ -d "$trashesloc" ]] ; then
trashesloc="$volume/.Trashes/$account"
fi
if [[ -d "$trashesloc" ]] ; then
sizeadd=$(osascript 2>/dev/null << EOS
set theTrashPath to "$trashesloc"
try
set theTrashAlias to (POSIX file theTrashPath) as alias
tell application "System Events"
set theTrashSize to size of theTrashAlias
end tell
return theTrashSize
on error
return 0
end try
EOS
)
if ! [[ $sizeadd ]] || [[ $sizeadd == "false" ]] || [[ $sizeadd == "missing value" ]] ; then
sizeadd=0
fi
else
sizeadd=0
fi
fi
othersize=$((othersize+sizeadd))
done < <(echo "$vol_list")
fi
finalsize=$((usersize+othersize))
echo -n "$finalsize"
}
_round () {
echo $(printf %.$2f $(echo "scale=$2;(((10^$2)*$1)+0.5)/(10^$2)" | bc))
}
_bhr () {
bytes="$1"
mib_raw=$(bc -l <<< "scale=6; $bytes/1048576")
mibytes=$(_round "$mib_raw" 2)
[[ $mibytes == "0.00" ]] && mibytes="< 0.01"
echo "$mibytes MiB ($bytes B)"
}
_rm-dsstore () {
rm -f "$HOME/.Trash/.DS_Store" "$icloud/.Trash/.DS_Store" 2>/dev/null
vol_list=$(_all-volumes | grep -v "^$")
if [[ $vol_list ]] ; then
while read -r volume
do
rm -f "$volume/.Trashes/$account/.DS_Store" 2>/dev/null
done < <(echo "$vol_list")
fi
}
rmblock=false
flaunch=false
trashfull=false
trashes_size1=$(_totalsize | sed 's/\.$//')
if [[ $trashes_size1 -gt 0 ]] ; then
trashfull=true
trashes_hrsize1=$(_bhr "$trashes_size1" 2>/dev/null)
trashstring="currently occupy a total of:"
else
emptydirs=""
while read -r trashloc
do
trasheddirs=$(find "$trashloc" -mindepth 1 -maxdepth 1 -type d 2>/dev/null)
[[ $trasheddirs ]] && emptydirs="$emptydirs\n$trasheddirs"
done < <(_all-trashes)
emptydirs=$(echo -e "$emptydirs" | grep -v "^$")
if [[ $emptydirs ]] ; then
trashfull=true
trashstring="are currently occupied by:"
emptydircount=$(echo "$emptydirs" | wc -l | xargs)
if [[ $emptydircount -eq 1 ]] ; then
trashes_hrsize1="$emptydircount empty folder"
else
trashes_hrsize1="$emptydircount empty folders"
fi
fi
fi
if $trashfull ; then
if ! $force ; then
frontmost=$(osascript -e 'tell application "System Events"' -e 'set theFrontmostApp to name of application processes whose frontmost is true' -e 'end tell' 2>/dev/null)
! [[ $frontmost ]] && frontmost="$fman_name"
choice=$(osascript 2>/dev/null << EOG
tell application "$frontmost"
set theButton to button returned of (display alert ¬
"Are you sure you want to empty all of your system's Trashes?" ¬
buttons {"Cancel", "List Files", "Empty Trashes"} ¬
as informational ¬
message "Your Trashes (including the iCloud Trash) $trashstring" & return & return & "$trashes_hrsize1" ¬
cancel button 1 ¬
default button 3 ¬
giving up after 180)
end tell
EOG
)
if ! [[ $choice ]] || [[ $choice == "false" ]] ; then
exit
fi
[[ $choice == "List Files" ]] && _list-trashed
fi
if ! pgrep -x "Finder" &>/dev/null ; then
open -j -g -b com.apple.finder && sleep 0.5
flaunch=true
touch "$tempdir/manual"
rmblock=true
else
if ! [[ -e "$tempdir/manual" ]] ; then
touch "$tempdir/manual"
rmblock=true
fi
fi
osascript -e 'tell application "Finder"' -e 'empty trash' -e 'end tell' -e 'delay 0.5' 2>/dev/null
$flaunch && osascript -e 'tell application "Finder" to quit' 2>/dev/null
$rmblock && rm -f "$tempdir/manual" 2>/dev/null
trashfull=false
emptydirs=""
alltrashes=$(_all-trashes)
while read -r trashloc
do
filesremain=false
dirsremain=false
if [[ $(find "$trashloc" -mindepth 1 -type f ! -name '.DS_Store' -print -quit 2>/dev/null) ]] ; then
filesremain=true
fi
trasheddirs=$(find "$trashloc" -mindepth 1 -maxdepth 1 -type d 2>/dev/null)
if [[ $trasheddirs ]] ; then
emptydirs="$emptydirs\n$trasheddirs"
dirsremain=true
fi
if ! $filesremain && ! $dirsremain ; then
rm -f "$trashloc/.DS_Store" 2>/dev/null
fi
done < <(echo -e "$alltrashes")
trashes_size2=$(_totalsize | sed 's/\.$//')
trashes_hrsize2=$(_bhr "$trashes_size2" 2>/dev/null)
if [[ $trashes_size2 -gt 0 ]] ; then
trashfull=true
else
emptydirs=$(echo -e "$emptydirs" | grep -v "^$")
if [[ $emptydirs ]] ; then
trashfull=true
trashstring="are currently occupied by:"
emptydircount=$(echo "$emptydirs" | wc -l | xargs)
if [[ $emptydircount -eq 1 ]] ; then
trashes_hrsize2="$emptydircount empty folder"
else
trashes_hrsize2="$emptydircount empty folders"
fi
fi
fi
if $trashfull ; then
_beep &
_notify "⚠️ Trash not emptied" "$trashes_hrsize2 remaining"
else
rm -f "$undoloc" 2>/dev/null
_rm-dsstore
fi
else
_notify "ℹ️ Trashes are empty"
_rm-dsstore
rm -f "$undoloc" 2>/dev/null
fi
exit