Raw text from PDF

Given a path to a PDF file, returns the raw text contents.

( Assumes Keyboard Maestro 11 )

Raw text from PDF.kmmacros (4.2 KB)


Expand disclosure triangle to view JS source
return (() => {
    "use strict";

    ObjC.import("Quartz");

    const main = () => {
        const
            fp = ObjC.unwrap(
                $(kmvar.local_PDFPath)
                .stringByStandardizingPath
            );

        return doesFileExist(fp)
            ? ObjC.unwrap(
                $.PDFDocument.alloc.initWithURL(
                    $.NSURL.fileURLWithPath(fp)
                )
                .string
            )
            : `File not found: '${fp}'`;
    };


    // --------------------- jxa ---------------------


    // doesFileExist :: FilePath -> IO Bool
    const doesFileExist = fp => {
        const ref = Ref();

        return $.NSFileManager
            .defaultManager
            .fileExistsAtPathIsDirectory(
                $(fp).stringByStandardizingPath,
                ref
            ) && !ref[0];
    };

    // MAIN ---
    return main();
})();
3 Likes

For a text rendering of the table of contents (where the PDF has one),
see also: