Skip to content

Instantly share code, notes, and snippets.

@anthonyjsmith
Created September 10, 2018 13:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonyjsmith/e0d482bc3fe8ef9e0eb698757efc7624 to your computer and use it in GitHub Desktop.
Save anthonyjsmith/e0d482bc3fe8ef9e0eb698757efc7624 to your computer and use it in GitHub Desktop.
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"
@dabrahams
Copy link

Use sys.argv[0] instead of rem in this call and I can call it "trash" instead ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment