Skip to content

Instantly share code, notes, and snippets.

@anthonyjsmith anthonyjsmith/rem
Created Sep 10, 2018

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Sep 10, 2018

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
You can’t perform that action at this time.