-
Notifications
You must be signed in to change notification settings - Fork 0
/
put-back
executable file
·276 lines (254 loc) · 7 KB
/
put-back
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
#!/bin/zsh
# shellcheck shell=bash
# put-back
# v0.9.17
# macOS
#
# Put Back - part of Trash Tools
# Copyright (c) 2021 Joss Brown (pseud.)
# License: MIT / place of jurisdiction: Berlin, Germany / German laws apply
#
# keyboard shortcut: CMD-DEL
#
# move individual trashed files back to their original location
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="Put Back"
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
}
if [[ $1 == "-x" ]] ; then
shift
else
regular=false
trashed=false
for filepath in "$@"
do
if echo "$filepath" | grep -q -e "/\.Trash/" -e "/\.Trashes/" &>/dev/null ; then
trashed=true
else
regular=true
fi
if $trashed && $regular ; then
_beep &
_notify "❌ Mixed content!" "Trashed & untrashed files"
exit
fi
done
if $regular ; then
trashes -x "$@" &
exit
fi
fi
_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
}
errors=false
rmblock=false
flaunch=false
destdirs=""
atrashes=""
for trashfile in "$@"
do
trashname=$(basename "$trashfile")
if [[ $trashname == ".DS_Store" ]] || [[ $trashname == "._"* ]] ; then
continue
fi
if ! [[ -e $trashfile ]] && ! [[ -h "$trashfile" ]] ; then
errors=true
_notify "❓ Trashed file missing!" "$trashname"
continue
fi
icloudloc=false
[[ $trashfile == "$icloud.Trash/"* ]] && icloudloc=true
testpath="$trashfile"
addpath="/$trashname"
nested=false
while true
do
tfparent=$(dirname "$testpath")
parentbase=$(basename "$tfparent")
if [[ $tfparent != *"/.Trash" ]] && [[ $tfparent != *"/.Trashes/$account" ]] ; then
if [[ $tfparent == *"/.Trash/$parentbase" ]] || [[ $tfparent == *"/.Trashes/$account/$parentbase" ]] ; then
testpath="$tfparent"
nested=true
continue
else
addpath="/$parentbase$addpath"
testpath="$tfparent"
nested=true
continue
fi
fi
destsubpath=$(xattr -ps "$trashxa" "$testpath" 2>/dev/null)
if ! [[ $destsubpath ]] ; then
if ! $icloudloc ; then
destsubpath=$(_dsstore "$testpath")
else
destsubpath=$(xattr -pxs "com.apple.trash.put-back.path#PS" "$testpath" 2>/dev/null | xxd -r -p 2>/dev/null | xargs)
if ! [[ $destsubpath ]] ; then
destsubpath=$(_dsstore "$testpath")
fi
fi
fi
break
done
if ! [[ $destsubpath ]] ; then
errors=true
_notify "⚠️ Unknown file destination" "$trashname"
continue
fi
if $icloudloc ; then
trashlocs="$icloud.Trash"
fvol="$icloud"
else
fvol=$(df "$trashfile" | tail -n +2 | awk '{print substr($0, index($0,$9))}')
if ! [[ $fvol ]] || [[ $fvol == "/System/Volumes/Data" ]] ; then
fvol="/"
trashlocs="$HOME/.Trash\n/System/Volumes/Data/.Trashes/$account"
else
fvol=$(echo "$fvol/" | sed 's-//$-/-')
trashlocs="$fvol.Trashes/$account"
fi
fi
destpath="$fvol$destsubpath"
$nested && destpath="$destpath$addpath"
destdir=$(dirname "$destpath")
destname=$(basename "$destpath")
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
else
if $nested ; then
if ! [[ -d "$destdir" ]] ; then
if ! mkdir -p "$destdir" ; then
errors=true
_notify "⚠️ Unable to prepare destination" "$trashname"
continue
fi
fi
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
atrashes="$atrashes\n$trashlocs"
else
errors=true
shortdestpath="${destpath/#$HOME/~}"
_notify "⚠️ File restore failed" "$shortdestpath"
fi
done
$errors && _beep &
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"
fi
done < <(echo -e "$atrashes" | grep -v "^$" 2>/dev/null | sort | awk '!a[$0]++')
exit