Last active
September 5, 2024 19:30
-
-
Save dabrahams/14fedc316441c350b382528ea64bc09c to your computer and use it in GitHub Desktop.
Script that sends files to trash on MacOS, based on https://apple.stackexchange.com/a/162354/25276 and https://gist.github.com/anthonyjsmith/e0d482bc3fe8ef9e0eb698757efc7624
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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: | |
sys.stderr.write( | |
"%s: %s: No such file or directory\n" % (sys.argv[0], arg)) | |
if len(files) > 0: | |
cmd = ['osascript', '-e', | |
'tell app "Finder" to move {' + ', '.join(files) + '} to trash'] | |
r = subprocess.call(cmd, stdout=open(os.devnull, 'w')) | |
sys.exit(r if len(files) == len(sys.argv[1:]) else 1) | |
else: | |
sys.stderr.write( | |
'usage: %s file(s)\n' | |
' move file(s) to Trash\n' % os.path.basename(sys.argv[0])) | |
sys.exit(64) # matches what rm does on my system |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've been known to combine
trash
withfind
, so 1000 files could happen, but I agree it's going to be rare...This is the version I ended up using (I modernized Python a bit, reduced array copying, cleaned up the exit codes, and removed the help because I start the python script from my more general
trash
shell script only when I detect a MacOS system):