Last active
September 7, 2022 11:06
-
-
Save pudquick/8f65bb9b306f91eafdcc to your computer and use it in GitHub Desktop.
Loading a NSString/CFStringRef constant directly from an OS X framework using ctypes - but don't do it this way! Do it the other way!
This file contains 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
# You should really be using this one instead, SERIOUS | |
# https://gist.github.com/pudquick/ac8f22326f095ed2690e | |
from ctypes import Structure, POINTER, CDLL, c_char_p, c_int32, create_string_buffer | |
from ctypes.util import find_library | |
# We build an opaque object pointer class type to safely handle data without inspecting it | |
class OpaqueType(Structure): | |
pass | |
OpaqueTypeRef = POINTER(OpaqueType) | |
# Load up CoreFoundation via C/ctypes to help with string conversion since we don't have | |
# access to pyObjC's nice magic NS/CF bridging | |
CF = CDLL(find_library('CoreFoundation')) | |
CFStringGetLength = CF.CFStringGetLength | |
CFStringGetCString = CF.CFStringGetCString | |
CFStringGetCString.argtypes = [OpaqueTypeRef, c_char_p, c_int32, c_int32] | |
# Define a helper function for converting from the CFStringRef to a python string | |
def pystr(cfstringref): | |
str_length = CFStringGetLength(cfstringref) + 1 | |
str_buff = create_string_buffer(str_length) | |
result = CFStringGetCString(cfstringref, str_buff, str_length, 0) | |
return str_buff.value | |
# Load our target framework via ctypes, in this case the 'Metadata' framework | |
MD = CDLL('/System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Metadata') | |
# The magic: Get a direct reference to the constant within the framework | |
kMDSPreferencesNameRef = (OpaqueTypeRef).in_dll(MD, 'kMDSPreferencesName') | |
# Now convert that back to a string | |
kMDSPreferencesName = pystr(kMDSPreferencesNameRef) | |
# >>> kMDSPreferencesName | |
# 'com.apple.SpotlightServer' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment