Send files to Trash on Mac, imitating shell command rm as closely as possible. Based on https://apple.stackexchange.com/a/162354/105139 and https://gist.github.com/dabrahams/14fedc316441c350b382528ea64bc09c
#!/usr/bin/env python | |
import os | |
import sys | |
import subprocess | |
if len(sys.argv) > 1: | |
files = [] | |
for arg in sys.argv[1:]: | |
if os.path.exists(arg): | |
p = os.path.abspath(arg).replace('\\', '\\\\').replace('"', '\\"') | |
files.append('the POSIX file "' + p + '"') | |
else: | |
print "rem:", arg + ": No such file or directory" | |
if len(files) > 0: | |
cmd = ['osascript', '-e', | |
'tell app "Finder" to move {' + ', '.join(files) + '} to trash'] | |
sys.exit(subprocess.call(cmd, stdout=open(os.devnull, 'w'))) | |
else: | |
print "usage: rem file(s)" | |
print " move file(s) to Trash" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Use
sys.argv[0]
instead ofrem
in this call and I can call it "trash" instead ;-)