trash 698 B

1234567891011121314151617181920212223242526272829303132
  1. #
  2. # Moves files to the macOS trash.
  3. #
  4. # function trash {
  5. emulate -L zsh
  6. setopt LOCAL_OPTIONS EXTENDED_GLOB
  7. local file
  8. local -a files=()
  9. for file in $@; do
  10. if [[ -e $file ]]; then
  11. # ':a' gets the full path (do not use ':A', which would resolve symlinks)
  12. files+=("the POSIX file \"${file:a}\"")
  13. else
  14. print "trash: No such file or directory '$file'." >&2
  15. return 1
  16. fi
  17. done
  18. if (( $#files == 0 )); then
  19. print 'usage: trash <files...>' >&2
  20. return 64 # Match rm's return code.
  21. fi
  22. # Join file list with commas, and tell Finder to trash that list.
  23. local file_list="${(pj., .)files}"
  24. osascript 2>&1 > /dev/null -e "tell app \"Finder\" to move { "${file_list}" } to trash"
  25. # }