Last active
May 30, 2024 19:49
-
-
Save igstan/52cb97ce0d22c623984b4b457b37ed5f to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env osascript -l JavaScript | |
ObjC.import('stdlib') | |
ObjC.import('PDFKit'); | |
function reverse(opts) { | |
// Load the PDF document | |
const pdfURL = $.NSURL.fileURLWithPath(opts.src); | |
const pdfDoc = $.PDFDocument.alloc.initWithURL(pdfURL); | |
// Check if the PDF document was successfully loaded | |
if (!pdfDoc.js) { | |
console.log('Failed to load the PDF document.') | |
$.exit(1); | |
} | |
// Create a new PDF document for the reversed pages | |
const revDoc = $.PDFDocument.alloc.init; | |
for (let i = 0; i < pdfDoc.pageCount; i++) { | |
revDoc.insertPageAtIndex(pdfDoc.pageAtIndex(i), 0); | |
} | |
revDoc.writeToURL($.NSURL.fileURLWithPath(opts.dst)); | |
} | |
function scriptName() { | |
return $.NSProcessInfo.processInfo.arguments.objectAtIndex(3).js; | |
} | |
function run(args) { | |
if (!args.length) { | |
console.log(`Usage: ${scriptName()} -o dst.pdf src.pdf`); | |
$.exit(1); | |
} | |
const opts = {}; | |
for (let i = 0; i < args.length; i++) { | |
if (args[i] == '-o') { | |
opts.dst = args[++i]; | |
} else { | |
opts.src = args[i]; | |
} | |
} | |
reverse(opts); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment