1
# Copyright 2010 Hardcoded Software (http://www.hardcoded.net)
3
# This software is licensed under the "BSD" License as described in the "LICENSE" file,
4
# which should be included with this package. The terms are also available at
5
# http://www.hardcoded.net/licenses/bsd_license
7
from ctypes import cdll, byref, Structure, c_char, c_char_p
8
from ctypes.util import find_library
10
Foundation = cdll.LoadLibrary(find_library(u'Foundation'))
11
CoreServices = cdll.LoadLibrary(find_library(u'CoreServices'))
13
GetMacOSStatusCommentString = Foundation.GetMacOSStatusCommentString
14
GetMacOSStatusCommentString.restype = c_char_p
15
FSPathMakeRefWithOptions = CoreServices.FSPathMakeRefWithOptions
16
FSMoveObjectToTrashSync = CoreServices.FSMoveObjectToTrashSync
18
kFSPathMakeRefDefaultOptions = 0
19
kFSPathMakeRefDoNotFollowLeafSymlink = 0x01
21
kFSFileOperationDefaultOptions = 0
22
kFSFileOperationOverwrite = 0x01
23
kFSFileOperationSkipSourcePermissionErrors = 0x02
24
kFSFileOperationDoNotMoveAcrossVolumes = 0x04
25
kFSFileOperationSkipPreflight = 0x08
27
class FSRef(Structure):
28
_fields_ = [(u'hidden', c_char * 80)]
30
def check_op_result(op_result):
32
msg = GetMacOSStatusCommentString(op_result).decode(u'utf-8')
36
if not isinstance(path, str):
37
path = path.encode(u'utf-8')
39
opts = kFSPathMakeRefDoNotFollowLeafSymlink
40
op_result = FSPathMakeRefWithOptions(path, opts, byref(fp), None)
41
check_op_result(op_result)
42
opts = kFSFileOperationDefaultOptions
43
op_result = FSMoveObjectToTrashSync(byref(fp), None, opts)
44
check_op_result(op_result)