My favorites
|
Sign in
zsh-templates-osx
zsh initialization files and functions specific to Mac OS X
Project Home
Downloads
Wiki
Issues
Source
Checkout
|
Browse
|
Changes
|
r149
Source path:
svn
/
trunk
/
Library
/
init
/
zsh
/
zshrc.d
/
local-functions
/
darwin
/
MacRm
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/bin/zsh -f
#----------------------------------------------------------------------------
# MacRm v0.0.1.1 for use with Darwin-OSX ......
# by Gary Kerbaugh
# Send comments, bugs or money to gkerbaugh@nc.rr.com
#
# MacRm emulates the Gnu FileUtils rm except that it moves file to the user's
# Trash directory, using CpMac. It also unlocks the file if necessary.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# Modified by Wataru Kagawa (05/31/05)
# wkagawa@jota.gsc.riken.go.jp
#
# In the modified version, the Dock Trash Can responds. Instead of moving
# the file directly to ~/.Trash, it is first moved to ~/Trash and then moved
# to ~/.Trash by the Finder application.
#----------------------------------------------------------------------------
mkdir -p "$HOME/Trash"
local dest_root="$HOME/Trash"
local NO_ARGS=0
local OPTERROR=65
local E_BADARGS=65 # Exit value if incorrect number of args passed.
local dest=""
local USAGE_TEXT="`basename $0` $version: options (-dfirv) filename
required arguments: [filename]
Move the FILE(s) to the user's Trash.
-d, --directory
unlink directory, even if non-empty (super-user
only)
-f, --force
ignore nonexistent files, never prompt
-i, --interactive
prompt before any removal
-r, -R, --recursive
remove the contents of directories recursively
-v, --verbose
explain what is being done
--help display this help and exit
--version
output version information and exit"
#####################################################################
# #
# Append "copy" to the filename until a unique name is created. #
# #
#####################################################################
dest_exists () {
#--------------------------------------------------------------------
# NCianca 2003-04-23 <ncianca@lightbulb.com>
# appends number after copy if more then one copy like the OSX Finder
#--------------------------------------------------------------------
si=0
suffix=''
base="${1%.*}"
ext=".${1##*.}"
if [ "$1" != "${base}${ext}" ]; then
ext=''
fi
while [ -e "$dest_root/${base}${suffix}${ext}" ]; do
if [ $si -eq 0 ]; then
suffix=" copy"
else
suffix=" copy $si"
fi
si=$(($si + 1))
done
echo -n "$dest_root/${base}${suffix}${ext}"
}
########################################################
# #
# Unlock file and move it. Be verbose if required. #
# #
########################################################
move_file () {
if [ -n "$verbopt" ]; then
echo "Moving $1 to trash."
chflags -R nouchg "$1"
# /Developer/Tools/CpMac -p "$1" "$dest"
/usr/bin/ditto -rsrc "$1" "$dest"
/bin/rm "$1"
elif [ -n "$forceopt" ]; then
chflags -R nouchg "$1" > /dev/null 2>&1
# /Developer/Tools/CpMac -p "$1" "$dest" > /dev/null 2>&1
/usr/bin/ditto -rsrc "$1" "$dest" > /dev/null 2>&1
/bin/rm "$1" > /dev/null 2>&1
else
chflags -R nouchg "$1"
# /Developer/Tools/CpMac -p "$1" "$dest"
/usr/bin/ditto -rsrc "$1" "$dest"
/bin/rm "$1"
fi
}
######################################################
# #
# Check with user if required and move to Trash. #
# #
######################################################
check_file () {
if [ \( -w "$1" \) -a \( -z "$interopt" \) ] || [ -n "$forceopt" ]
then
move_file "$1"
else
echo -n "Do you wish to move $1 to trash? "
read ans
case $ans in
[yY]*)
move_file "$1"
;;
*)
echo "Not moving $1 to trash."
;;
esac
fi
}
################################################################
# #
# Move link by creating a new one. Be verbose if required. #
# #
################################################################
move_link () {
if [ -n "$verbopt" ]; then
echo "Moving $1 to trash."
chflags -R nouchg "$1"
# /Developer/Tools/CpMac -p "$1" "$dest"
/bin/ln -s "$(readlink "$1")" "$dest"
# target=$(readlink "$1")
/bin/rm "$1"
elif [ -n "$forceopt" ]; then
chflags -R nouchg "$1" > /dev/null 2>&1
# /Developer/Tools/CpMac -p "$1" "$dest" > /dev/null 2>&1
/bin/ln -s "$(readlink "$1")" "$dest" > /dev/null 2>&1
/bin/rm "$1" > /dev/null 2>&1
else
chflags -R nouchg "$1"
# /Developer/Tools/CpMac -p "$1" "$dest"
/bin/ln -s "$(readlink "$1")" "$dest"
/bin/rm "$1"
fi
}
######################################################
# #
# Check with user if required and move to Trash. #
# #
######################################################
check_link () {
if [ \( -w "$1" \) -a \( -z "$interopt" \) ] || [ -n "$forceopt" ]
then
move_link "$1"
else
echo -n "Do you wish to move $1 to trash? "
read ans
case $ans in
[yY]*)
move_link "$1"
;;
*)
echo "Not moving $1 to trash."
;;
esac
fi
}
################################################
# #
# Check file to see if directory is empty. #
# #
################################################
is_dir_empty () {
result=$(command ls "$1")
if echo "$result" | grep "[^[:space:]]">/dev/null; then
if [ -z "$forceopt" ]; then
echo "`basename $0`: `ls -d ${1}`: Directory is not empty"
fi
return 1
else
return 0
fi
}
##########################################################
# #
# Check approval for moving directory or approve it. #
# #
##########################################################
set_dir () {
nicefile=$(echo "$1" | sed 's/\//_/g' | sed 's/[ \.-]//g')
if [ -n "$interopt" ]; then
echo -n "Do you wish to move directory $thisfile to trash? "
read ans
case $ans in
[yY]*)
eval $nicefile="true"
return 0
;;
*)
echo "Not moving $thisfile to trash."
eval $nicefile="false"
return 1
;;
esac
else
eval $nicefile="true"
return 0
fi
}
####################################################
# #
# Check approval for moving directory to Trash #
# #
####################################################
check_dir () {
nicefile=$(echo "$1" | sed 's/\//_/g' | sed 's/[ \.-]//g')
eval testval=\$$nicefile
if [ -n "$interopt" ]; then
if [ $testval = "true" ]; then
# echo "after value of \$$nicefile is: $testval"
return 0
else
return 1
fi
else
return 0
fi
}
#########################################################
# #
# Recursively move directory and contents to Trash. #
# #
#########################################################
lsdir () {
if set_dir "$thisfile" && [ -n "$recopt" ]; then
IFS='
'
for file in $(command ls -A1 "$1"); do
IFS=$oldIFS
thisfile="$thisfile/$file"
dest="$dest/$file"
if [ -L "$thisfile" ]; then
check_link "$thisfile"
elif [ -d "$thisfile" ]; then
if [ -n "$forceopt" ]; then
# mkdir -p "$dest_root/${thisfile#*/}" > /dev/null 2>&1
mkdir -p "$dest" > /dev/null 2>&1
else
mkdir -p "$dest"
fi
lsdir "$thisfile"
else
check_file "$thisfile"
fi
thisfile=${thisfile%/*}
dest=${dest%/*}
done
if check_dir $thisfile && is_dir_empty "$1"; then
if [ -n "$forceopt" ]; then
rmdir "$thisfile" > /dev/null 2>&1
else
if [ -n "$verbopt" ]; then
echo "Moving $thisfile to trash."
fi
rmdir "$thisfile"
fi
fi
else
if check_dir "$1" && is_dir_empty "$1"; then
if [ -n "$forceopt" ]; then
rmdir "$1" > /dev/null 2>&1
else
if [ -n "$verbopt" ]; then
echo "Moving $1 to trash."
fi
rmdir "$1"
fi
fi
fi
}
###########################################################################
# #
# Use getopts to obtain the options passed to the script by the user. #
# #
###########################################################################
while getopts ":dfirRv-:" Option
do
case $Option in
d) local diropt="true";; # Move empty directories
f) local forceopt="true";; # Force move to user's Trash
i) local interopt="true";; # Request before moving
r|R) local recopt="true";; # Recursively move contents of directories
v) local verbopt="true";; # Report all movements to Trash
-) case $OPTARG in
directory) local diropt="true";;
force) local forceopt="true";;
interactive) local interopt="true";;
recursive) local recopt="true";;
verbose) local verbopt="true";;
help) echo "$USAGE_TEXT"
return 0;;
version) echo "$version"
return 0;;
*) echo "$USAGE_TEXT"
return 0;; #DEFAULT
esac;;
? ) echo "Unimplemented option chosen: $OPTARG"
echo "$USAGE_TEXT"
return 0;;
* ) echo "Unimplemented option chosen: $Option"
echo "$USAGE_TEXT"
return 0;; # DEFAULT
esac
done
shift $(($OPTIND - 1))
if [ -n "$forceopt" ]; then
local interopt=""
local verbopt=""
fi
function do_arg {
argu="$1"
IFS=" "
if [ -e "$argu" ]; then
for elem in $(command ls -d "$argu")
do
IFS=$oldIFS
elem=$(echo "$elem" | sed 's/\/$//g')
if [ \( -n "$recopt" \) -o \( -n "$diropt" \) ]; then
if [ -L "$elem" ]; then
filename=$(basename "$elem")
dest=`dest_exists "$filename"`
check_link "$elem"
dest=""
elif [ -d "$elem" ]; then
thisfile="$elem"
temp=$(basename "$elem")
dest=`dest_exists "$temp"`
if [ -n "$forceopt" ]; then
mkdir -p "$dest" > /dev/null 2>&1
else
mkdir -p "$dest"
fi
lsdir "$elem"
dest=""
else
filename=$(basename "$elem")
dest=`dest_exists "$filename"`
check_file "$elem"
dest=""
fi
else
if [ -L "$elem" ]; then
filename=$(basename "$elem")
dest=`dest_exists "$filename"`
check_link "$elem"
dest=""
elif [ ! -d "$elem" ]; then
filename=$(basename "$elem")
dest=`dest_exists "$filename"`
check_file "$elem"
dest=""
elif [ -z "$forceopt" ]; then
echo "`basename $0`: ${elem}: is a directory"
fi
fi
done
elif [ -z "$forceopt" ]; then
echo "`basename $0`: ${argu}: No such file or directory"
fi
}
#################################
# #
# Main program starts here. #
# #
#################################
local oldIFS=$IFS
if [ -z "$*" ]; then
if [ -t 0 ]; then
echo "Usage: $USAGE_TEXT"
return 1
else
IFS='
'
for argu in $( cat $@ )
do
do_arg "$argu"
done
fi
else
for argu in "$@"
do
do_arg "$argu"
done
fi
osascript >/dev/null << eof
set A to "$dest_root"
set B to ( POSIX file A as alias )
tell app "Finder"
delete items of folder B
end tell
eof
Show details
Hide details
Change log
r75
by wgsco...@mac.com on Jan 03, 2009
Diff
moved directory
Go to:
...h/zshrc.d/local-functions/darwin
...nctions/darwin/0-coot-history.py
...unctions/darwin/0-coot.state.scm
...c.d/local-functions/darwin/8bitx
.../local-functions/darwin/CHAR.CHA
...c.d/local-functions/darwin/MacRm
...d/local-functions/darwin/SEQ.CHA
...rc.d/local-functions/darwin/_cdd
.../local-functions/darwin/_chflags
...functions/darwin/_comp_appgetter
...local-functions/darwin/_defaults
...c.d/local-functions/darwin/_edit
...c.d/local-functions/darwin/_fink
...local-functions/darwin/_fink_web
...nctions/darwin/_get_running_apps
.../local-functions/darwin/_guidirs
...ocal-functions/darwin/_init_open
...ocal-functions/darwin/_launchctl
...nctions/darwin/_mac_applications
...arwin/_mac_files_for_application
.../local-functions/darwin/_macmail
...rc.d/local-functions/darwin/_man
...d/local-functions/darwin/_mdfind
...-functions/darwin/_mdfind_pashua
...c.d/local-functions/darwin/_mdls
...d/local-functions/darwin/_mdutil
...c.d/local-functions/darwin/_open
...d/local-functions/darwin/_qtplay
...ctions/darwin/_retrieve_mac_apps
....d/local-functions/darwin/_users
...local-functions/darwin/a2preview
...d/local-functions/darwin/addIcon
.../local-functions/darwin/add_user
.../local-functions/darwin/addgroup
...l-functions/darwin/adduser2group
...rc.d/local-functions/darwin/aeon
...d/local-functions/darwin/airport
...d/local-functions/darwin/battery
...hrc.d/local-functions/darwin/cdd
...hrc.d/local-functions/darwin/cdf
...d/local-functions/darwin/cdright
...hrc.d/local-functions/darwin/cds
...hrc.d/local-functions/darwin/cdt
.../local-functions/darwin/composer
...s/darwin/configure_zsh_templates
...in/configure_zsh_templates_asgui
...c.d/local-functions/darwin/cpbcp
...rc.d/local-functions/darwin/cpos
...ocal-functions/darwin/da2preview
...al-functions/darwin/delete_group
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 11897 bytes, 489 lines
View raw file
Hosted by