// Extracts each PDF's text page by page, using macOS's built-in PDFKit. // usage: osascript -l JavaScript rag/extract.js out.json a.pdf b.pdf ... ObjC.import('Foundation'); ObjC.import('PDFKit'); function run(argv) { const out = argv[0]; const pages = []; for (const path of argv.slice(1)) { const doc = $.PDFDocument.alloc.initWithURL($.NSURL.fileURLWithPath(path)); if (!doc.js) throw new Error('Could not open ' + path); const book = path.split('/').pop(); const n = doc.pageCount; for (let i = 0; i < n; i++) { const s = doc.pageAtIndex(i).string; pages.push({ book, page: i + 1, label: ObjC.unwrap(doc.pageAtIndex(i).label) || '', text: s.js ? ObjC.unwrap(s) : '' }); } } $.NSString.alloc.initWithUTF8String(JSON.stringify(pages)).writeToFileAtomicallyEncodingError(out, true, $.NSUTF8StringEncoding, null); return pages.length + ' pages'; }