modern.py 968 B

12345678910111213141516171819202122232425
  1. # Copyright 2017 Virgil Dupras
  2. # This software is licensed under the "BSD" License as described in the "LICENSE" file,
  3. # which should be included with this package. The terms are also available at
  4. # http://www.hardcoded.net/licenses/bsd_license
  5. from Foundation import NSFileManager, NSURL
  6. from send2trash.util import preprocess_paths
  7. def check_op_result(op_result):
  8. # First value will be false on failure
  9. if not op_result[0]:
  10. # Error is in third value, localized failure reason matchs ctypes version
  11. raise OSError(op_result[2].localizedFailureReason())
  12. def send2trash(paths):
  13. paths = preprocess_paths(paths)
  14. paths = [path.decode("utf-8") if not isinstance(path, str) else path for path in paths]
  15. for path in paths:
  16. file_url = NSURL.fileURLWithPath_(path)
  17. fm = NSFileManager.defaultManager()
  18. op_result = fm.trashItemAtURL_resultingItemURL_error_(file_url, None, None)
  19. check_op_result(op_result)