-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathundo-trashes
executable file
·252 lines (233 loc) · 7.12 KB
/
undo-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
#!/bin/zsh
# shellcheck shell=bash
# undo-trashes
# v0.9.17
# macOS
#
# Undo Trashes - part of Trash Tools
# Copyright (c) 2021 Joss Brown (pseud.)
# License: MIT / place of jurisdiction: Berlin, Germany / German laws apply
#
# keyboard shortcut: CMD-CTRL-DEL
#
# undo the previous move-to-trash operation
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="Undo Trashes"
account=$(id -u)
trashxa="local.lcars.fpsr.loc#PS"
tempdir="$HOME/.cache/Finder"
tmploc="/tmp/local.lcars.TrashTools.$account.trashes"
icloud="$HOME/Library/Mobile Documents/com~apple~CloudDocs/"
_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
}
_dsstore () {
trashedpath="$1"
trashedname=$(basename "$trashedpath")
parentdir=$(dirname "$trashedpath")
dsstore="$parentdir/.DS_Store"
if ! [[ -f "$dsstore" ]] ; then
echo -n ""
else
dsraw=$(xxd -p "$dsstore" | sed 's/00//g' | tr -d '\n' | sed 's/\([0-9A-F]\{2\}\)/0x\1 /g' | xxd -r -p | strings | LC_ALL=C tr -dc '\0-\177' | grep -v -e "^Bud1$" -e "ptbL$" -e "^DSDB$" -e "vSrnlong$" -e "blob$")
trashedinfo=$(echo "$dsraw" | sed -e 's/ptbNustr$/\n$ptbNustr/g' -e 's/ptbLustr-/ptbLustr\n/g' -e 's/ptbLustr /ptbLustr\n/g' -e 's/ptbLustr\!/ptbLustr\n/g' | grep -A3 "^$trashedname\ptbLustr$" 2>/dev/null | tail -n +2 | grep -v "ptbNustr$" | sed 's-^System/Volumes/Data/--g')
if ! [[ $trashedinfo ]] ; then
echo -n ""
else
if [[ $(echo "$trashedinfo" | wc -l) -eq 1 ]] ; then
echo -n "$trashedinfo"
else
destparent=$(echo "$trashedinfo" | head -1)
trashedinfo=$(echo "$trashedinfo" | grep -v "^$destparent$")
if [[ $(echo "$trashedinfo" | wc -l) -gt 1 ]] ; then
echo -n ""
else
echo -n "$destparent$trashedinfo"
fi
fi
fi
fi
}
_finder-move () {
osasource="$1"
osasourcename="$2"
osadestdir="$3"
osadestname="$4"
if [[ -d $osasource ]] ; then
ftype="folder"
else
ftype="file"
fi
if osascript &>/dev/null << EOM
set theSource to POSIX file "$osasource"
set theDestinationFolder to POSIX file "$osadestdir"
set theTrashName to "$osasourcename"
set theFilename to "$osadestname"
set theFiletype to "$ftype"
tell application "Finder"
if theFiletype is equal to "file" then
move theSource to folder theDestinationFolder without replacing
try
set name of file theTrashName of folder theDestinationFolder to theFilename
end try
else if theFiletype is equal to "folder" then
set theRestoredFolder to (move theSource to folder theDestinationFolder without replacing)
try
set name of theRestoredFolder to theFilename
end try
end if
end tell
EOM
; then
echo -n "ok"
else
echo -n "error"
fi
}
recents=$(cat "$tmploc" 2>/dev/null)
if ! [[ $recents ]] ; then
_beep &
_notify "🗑 No Trash history available"
exit
fi
errors=false
rmblock=false
flaunch=false
destdirs=""
alltrashes=""
while read -r recent
do
icloudtrash=false
fvol=$(echo "$recent" | awk -F":" '{print $1}')
filename=$(echo "$recent" | awk -F":" '{print substr($0, index($0,$2))}')
searchname=$(printf '%q\n' "$filename")
if [[ $fvol == "/" ]] ; then
trashlocs="$HOME/.Trash\n/System/Volumes/Data/.Trashes/$account"
trashfile=$(find "$HOME/.Trash" "/System/Volumes/Data/.Trashes/$account" -mindepth 1 -maxdepth 1 -name "$searchname*" 2>/dev/null)
elif [[ $fvol == "$icloud" ]] ; then
trashlocs="$icloud.Trash"
if [[ -d "$trashlocs" ]] ; then
icloudtrash=true
trashfile=$(find "$trashlocs" -mindepth 1 -maxdepth 1 -name "$searchname*" 2>/dev/null)
else
errors=true
_notify "❓ Trash folder missing!" "iCloud (CloudDocs)"
recents=$(echo "$recents" | grep -v "^$recent")
continue
fi
else
trashlocs="$fvol.Trashes/$account"
if [[ -d "$trashlocs" ]] ; then
trashfile=$(find "$trashlocs" -mindepth 1 -maxdepth 1 -name "$searchname*" 2>/dev/null)
else
errors=true
_notify "❓ Trash folder missing!" "$fvol"
recents=$(echo "$recents" | grep -v "^$recent")
continue
fi
fi
alltrashes="$alltrashes\n$trashlocs"
if ! [[ -e $trashfile ]] && ! [[ -h "$trashfile" ]] ; then
errors=true
_notify "❓ Trashed file missing!" "$filename"
recents=$(echo "$recents" | grep -v "^$recent")
continue
fi
if [[ $(echo "$trashfile" | wc -l) -gt 1 ]] ; then
trashfilelist=""
while read -r trashfiletest
do
ctime=$(stat -f %c "$trashfiletest" 2>/dev/null)
trashfilelist="$trashfilelist\n$ctime:$trashfiletest"
done < <(echo "$trashfile")
trashfile=$(echo -e "$trashfilelist" | sort -n -t ':' -r -k1 | head -1 | awk -F":" '{print substr($0, index($0,$2))}')
fi
trashname=$(basename "$trashfile")
destsubpath=$(xattr -ps "$trashxa" "$trashfile" 2>/dev/null | awk -F";" '{print substr($0, index($0,$2))}')
if ! [[ $destsubpath ]] ; then
if ! $icloudtrash ; then
destsubpath=$(_dsstore "$trashfile")
else
destsubpath=$(xattr -pxs "com.apple.trash.put-back.path#PS" "$trashfile" 2>/dev/null | xxd -r -p | xargs)
if ! [[ $destsubpath ]] ; then
destsubpath=$(_dsstore "$trashfile")
fi
fi
fi
if ! [[ $destsubpath ]] ; then
errors=true
_notify "⚠️ Unknown file destination" "$trashname"
recents=$(echo "$recents" | grep -v "^$recent")
continue
fi
destpath="$fvol$destsubpath"
destdir=$(dirname "$destpath")
destname="$filename"
if [[ -e "$destpath" ]] || [[ -h "$destpath" ]] ; then
if [[ $destname == "$trashname" ]] ; then
errors=true
shortdestpath="${destpath/#$HOME/~}"
_notify "⚠️ File exists at restore path" "$shortdestpath"
continue
else
destpath="$destdir/$trashname"
if [[ -e "$destpath" ]] || [[ -h "$destpath" ]] ; then
errors=true
shortdestpath="${destpath/#$HOME/~}"
_notify "⚠️ File exists at restore path" "$shortdestpath"
continue
fi
destname="$trashname"
fi
fi
destdirs="$destdirs\n$destdir"
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
osaresult=$(_finder-move "$trashfile" "$trashname" "$destdir" "$destname")
if [[ $osaresult == "ok" ]] ; then
xattr -ds "$trashxa" "$destpath" 2>/dev/null
recents=$(echo "$recents" | grep -v "^$recent")
else
errors=true
shortdestpath="${destpath/#$HOME/~}"
_notify "⚠️ File restore failed" "$shortdestpath"
fi
done < <(echo "$recents")
if $errors ; then
_beep &
echo -e "$recents" | grep -v "^$" > "$tmploc" 2>/dev/null
else
rm -f "$tmploc" 2>/dev/null
fi
if $flaunch ; then
osascript -e 'tell application "Finder" to quit' 2>/dev/null
while read destdir
do
rm -f "$destdir/.DS_Store" 2>/dev/null
done < <(echo -e "$destdirs" | grep -v "^$")
fi
$rmblock && rm -f "$tempdir/manual" 2>/dev/null
while read -r trashloc
do
if ! [[ $(find "$trashloc" -mindepth 1 -maxdepth 1 ! -name '.DS_Store' -print -quit 2>/dev/null) ]] ; then
rm -f "$trashloc/.DS_Store" 2>/dev/null
fi
done < <(echo -e "$alltrashes" | grep -v "^$" 2>/dev/null)
exit