AppleScriptObjC: Find image in PDF

Hi, I’d like to get the position of images in a PDF.

This Stack Overflow code (Extracting images from a PDF) looks promising, however I couldn’t make CGPDFDocumentGetPage work in AppleScriptObjC.

Shane wrote on MacScripter:

The CGEvent stuff is all C-based APIs. Although ASObjC can call C-based code, it can’t provide the arguments needed in this case.

Is CGPDFDocumentGetPage (and the rest of the stuff that’s needed to get an image’s position) also such a case? If so, is there any other way to get the position?

I’m afraid so – you’re out of luck.

Thanks. A bit background: I get all links to create bookmarks in DEVONthink (to be able to easily capture them as PDF). What I’m trying to do is to automatically group the bookmarks according to the heading on the left side.

First approach was checking the space to the left of each link annotation. If it’s not empty it’s a heading. This almost always works, however some headings only contain one link annotation and in these cases it’s not possible to reliably decide where one heading ends and the next starts. See the page’s AttributedString:

New approach is to get the source, remove all link annotations, get the bounds of everything that’s left, sort them. Then check whether there’s something between one link annotation and the next.

Would that probably work? Or are image bounds also encoded?

I suspect you’re the only person who can answer that…

At least in theory, there’s a way around that. If you can call those APIs in Swift, you can write a Swift script (a text file with your Swift code in it and a #!/usr/bin/swift shebang), then run and talk to your Swift script from your AppleScript.

I can’t remember if you need Xcode CLI tools installed or not to do that, probably.

1 Like