-
Notifications
You must be signed in to change notification settings - Fork 0
/
unlink
executable file
·221 lines (206 loc) · 5.09 KB
/
unlink
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
#!/bin/zsh
# shellcheck shell=bash
# unlink
# v0.9.8
# macOS
#
# Unlink - part of Trash Tools
# Copyright (c) 2021 Joss Brown (pseud.)
# License: MIT / place of jurisdiction: Berlin, Germany / German laws apply
#
# keyboard shortcut: CMD-OPT-DEL
#
# permanently remove (unlink) files
export LANG=en_US.UTF-8
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/local/bin:/opt/homebrew/bin:/opt/sw/bin:"$HOME"/.local/bin:"$HOME"/bin:"$HOME"/local/bin
uiprocess="Unlink"
account=$(id -u)
configdir="$HOME/.config/TrashTools"
protectlist="$configdir/protections.txt"
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
}
if [[ $# -gt 10 ]] ; then
_notify "⏳ Please wait!" "Checking files for protection..."
fi
rmlist=""
rmcount=0
for filepath in "$@"
do
filename=$(basename "$filepath")
if xattr -p "local.lcars.fpsr#PS" "$filepath" &>/dev/null ; then
_beep &
_notify "🔒 File is protected!" "$filename"
continue
fi
if grep -q "^$filename$" "$protectlist" &>/dev/null ; then
_beep &
_notify "🔏 File is protected!" "config: $filename"
continue
fi
if ! [[ -e "$filepath" ]] && ! [[ -h "$filepath" ]] ; then
_beep &
_notify "❌ File missing or inaccessible!" "$filename"
continue
fi
if [[ $filename == ".DS_Store" ]] ; then
if rm -f "$filepath" &>/dev/null ; then
continue
fi
fi
rmlist="$rmlist\n$filepath"
((rmcount++))
done
if [[ $rmcount -eq 0 ]] ; then
exit
fi
rmlist=$(echo -e "$rmlist" | grep -v "^$")
if [[ $rmcount -gt 1 ]] ; then
fstr="files"
rmnamelist=$(echo "$rmlist" | awk -F"/" '{print $NF}')
else
fstr="file"
rmnamelist=$(basename "$rmlist")
fi
choice=$(osascript 2>/dev/null << EOG
tell application "$fman_name"
set theButton to button returned of (display alert ¬
"Are you sure you want to permanently delete the following $fstr?" ¬
buttons {"Cancel", "Permanently Delete"} ¬
as warning ¬
message "$rmnamelist" ¬
cancel button 1 ¬
default button 2 ¬
giving up after 180)
end tell
EOG
)
if [[ $choice != "Permanently Delete" ]] ; then
exit
fi
errors=false
errorcount=0
rootlist=""
while read -r rmpath
do
rmname=$(basename "$rmpath")
if ! rm -rf "$rmpath" &>/dev/null ; then
errors=true
if ! [[ -w "$rmpath" ]] ; then
_notify "⚠️ File could not be removed!" "Not writable: $rmname"
else
_notify "⚠️ File could not be removed!" "$rmname"
fi
rootlist="$rootlist\n$rmpath"
((errorcount++))
fi
done < <(echo "$rmlist")
! $errors && exit
_beep &
rootlist=$(echo -e "$rootlist" | grep -v "^$")
if [[ $errorcount -gt 1 ]] ; then
fstr="files"
rootnamelist=$(echo "$rootlist" | awk -F"/" '{print $NF}')
else
fstr="file"
rootnamelist=$(basename "$rootlist")
fi
choice=$(osascript 2>/dev/null << EOA
tell application "$fman_name"
set theButton to button returned of (display alert ¬
"Some files could not be removed." & return & return & "Are you sure you want to continue and permanently delete the following $fstr with root privileges?" ¬
buttons {"Cancel", "Permanently Delete"} ¬
as warning ¬
message "$rootnamelist" ¬
cancel button 1 ¬
default button 1 ¬
giving up after 180)
end tell
EOA
)
if [[ $choice != "Permanently Delete" ]] ; then
exit
fi
icon_loc="/System/Library/PreferencePanes/Security.prefPane/Contents/Resources/SystemPreferences_Security.tiff"
password=$(osascript 2>/dev/null << EOR
tell application "System Events"
activate
set theIconPath to POSIX file "$icon_loc"
repeat
display dialog "To start the file removal, you need to enter your administrator password." ¬
with icon file theIconPath ¬
default answer "" ¬
with title "Unlink" ¬
with hidden answer ¬
buttons {"Cancel", "Enter"} ¬
default button 2
if button returned of the result is "Cancel" then
return 1
exit repeat
else if the button returned of the result is "Enter" then
set pswd to text returned of the result
set usr to short user name of (system info)
try
do shell script "echo test" user name usr password pswd with administrator privileges
return pswd
exit repeat
end try
end if
end repeat
end tell
EOR
)
if [[ $password == 1 ]] ; then
exit
fi
fcount=0
scount=0
errors=false
while read -r rootfile
do
echo -n "$password" | sudo -S rm -rf "$rootfile" 2>/dev/null
if [[ $? == 1 ]] ; then
rootname=$(basename "$rootfile")
_notify "⚠️ Removal error!" "$rootname"
((fcount++))
errors=true
else
((scount++))
fi
done < <(echo "$rootlist")
if $errors ; then
_beep &
if [[ $scount -eq 0 ]] ; then
if [[ $fcount -eq 1 ]] ; then
fstr="file"
else
fstr="files"
fi
_beep &
_notify "⚠️ Removal error!" "$fcount $fstr remaining"
else
if [[ $fcount -eq 1 ]] ; then
fstr="file"
else
fstr="files"
fi
if [[ $scount -eq 1 ]] ; then
sstr="file"
else
sstr="files"
fi
_beep &
_notify "☑️ $scount $sstr removed" "$fcount $fstr remaining"
fi
fi
exit 0