I'm using python to write a bunch of scripts that dothings with CoreGraphics. I'm getting stuck when the API documentation uses pointers.
For instance:
for filename in sys.argv[1:]:
pdf = CGPDFDocumentCreateWithURL(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, filename, len(filename), False)) pages = CGPDFDocumentGetNumberOfPages(pdf) info = CGPDFDocumentGetInfo(pdf) print pages, info
pages = CGPDFDocumentGetNumberOfPages(pdf)
info = CGPDFDocumentGetInfo(pdf)
The pages variable comes out as an integer, with the number of pages. But info gives me:
PyObjCPointer created: at 0x7f8944f2c200 of type {CGPDFDictionary=}^{CGPDFDocument=}
Similarly, the Swift documentation for CGPDFDocumentGetVersion has this:
func CGPDFDocumentGetVersion(
_ document: CGPDFDocument?,
_ majorVersion: UnsafeMutablePointer<Int32>,
_ minorVersion: UnsafeMutablePointer<Int32>
)
and I have no idea how to do this in python.
There’s two questions here, somewhat related. Let’s take each in turn.
Once you remove all the guff the Objective-C declaration for
CGPDFDocumentGetInfo
is as follows.
CGPDFDictionaryRef CGPDFDocumentGetInfo(CGPDFDocumentRef document);
It returns an opaque
CGPDFDictionaryRef
object. To get information from that, you have to call the various routines in
<CoreGraphics/CGPDFDictionary.h>
. To continue your example:
>>> CGPDFDictionaryGetCount(info)
9
Now, this isn’t directly helpful because most of CGPDFDictionary routines return values by reference. And that brings us to your second question.
The Objective-C declaration for
CGPDFDocumentGetVersion
is as follows.
void CGPDFDocumentGetVersion(
CGPDFDocumentRef cg_nullable document,
int * majorVersion,
int * minorVersion
);
majorVersion
and
minorVersion
are ‘out’ parameters. However, it seems that the bridge is treating them as general pointer parameters.
>>> CGPDFDocumentGetVersion.__metadata__()['arguments'][1]['type']
'^i'
>>> CGPDFDocumentGetVersion.__metadata__()['arguments'][2]['type']
'^i'
That means that the usual trick of passing in
None
for an ‘out’ parameter doesn’t work.
I’m not sufficiently au fait with PyObjC’s metadata infrastructure to offer any suggestions for how to fix this. I recommend you raise this via the support channel associated with that tool.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"