plat_osx.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 __future__ import unicode_literals
  6. from ctypes import cdll, byref, Structure, c_char, c_char_p
  7. from ctypes.util import find_library
  8. from .compat import binary_type
  9. Foundation = cdll.LoadLibrary(find_library('Foundation'))
  10. CoreServices = cdll.LoadLibrary(find_library('CoreServices'))
  11. GetMacOSStatusCommentString = Foundation.GetMacOSStatusCommentString
  12. GetMacOSStatusCommentString.restype = c_char_p
  13. FSPathMakeRefWithOptions = CoreServices.FSPathMakeRefWithOptions
  14. FSMoveObjectToTrashSync = CoreServices.FSMoveObjectToTrashSync
  15. kFSPathMakeRefDefaultOptions = 0
  16. kFSPathMakeRefDoNotFollowLeafSymlink = 0x01
  17. kFSFileOperationDefaultOptions = 0
  18. kFSFileOperationOverwrite = 0x01
  19. kFSFileOperationSkipSourcePermissionErrors = 0x02
  20. kFSFileOperationDoNotMoveAcrossVolumes = 0x04
  21. kFSFileOperationSkipPreflight = 0x08
  22. class FSRef(Structure):
  23. _fields_ = [('hidden', c_char * 80)]
  24. def check_op_result(op_result):
  25. if op_result:
  26. msg = GetMacOSStatusCommentString(op_result).decode('utf-8')
  27. raise OSError(msg)
  28. def send2trash(path):
  29. if not isinstance(path, binary_type):
  30. path = path.encode('utf-8')
  31. fp = FSRef()
  32. opts = kFSPathMakeRefDoNotFollowLeafSymlink
  33. op_result = FSPathMakeRefWithOptions(path, opts, byref(fp), None)
  34. check_op_result(op_result)
  35. opts = kFSFileOperationDefaultOptions
  36. op_result = FSMoveObjectToTrashSync(byref(fp), None, opts)
  37. check_op_result(op_result)