Last active
October 2, 2017 04:49
-
-
Save TGOlson/e227c8d648304ecbe5e7262f1341fc64 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function test() { | |
var ids = getIds(); | |
var batches = createBatches(ids); | |
for (var id in batches) { | |
if (batches.hasOwnProperty(id)) { | |
var pages = batches[id]; | |
var start = pages[0]; | |
var end = pages[pages.length -1]; | |
savePDFSplit(id, start, end); | |
} | |
} | |
app.beep(3); | |
console.println("Done!"); | |
} | |
function savePDFSplit(id, start, end) { | |
var filename = id + ".pdf" | |
this.extractPages(start, end).saveAs(filename); | |
} | |
// list of ids to batches -> {ids: [<pageNums>], ...} | |
function createBatches(ids) { | |
return ids.reduce(function (acc, x, i) { | |
if (acc[x]) { | |
acc[x].push(i) | |
} else { | |
acc[x] = [i] | |
} | |
return acc; | |
}, {}); | |
} | |
function getIds() { | |
var ids = []; | |
var numPages = this.numPages; | |
for (var i = 0; i < numPages; i++) { | |
var txt = pageText(i); | |
var id = parseId(txt); | |
app.beep(i % 2 === 0 ? 1 : 2); | |
ids.push(id); | |
} | |
return ids; | |
} | |
function parseId(txt) { | |
// Tests for LabCorp style ids of the form: | |
// xxxx, ID | |
// Assumes ids will allways start with a digit and be ended with ", ID" | |
var idTest1 = /\d.*(?=, ID)/.exec(txt); | |
if (idTest1 !== null) { | |
return idTest1; | |
} | |
// Tests for LABS style ids of the form: | |
// Additional ID 1 | |
// ... | |
// xx-xx | |
// Assumes ids will be within 100 characters of "Additional ID 1", and will contain at least one dash. | |
var subsection = /Additional ID 1[\s\S]{100}/m.exec(txt); | |
var idTest2 = /\w*\-\S*/.exec(subsection); | |
if (idTest2 !== null) { | |
return idTest2; | |
} | |
throw new Error("Unable to find id"); | |
} | |
function pageText(n) { | |
var numWords = this.getPageNumWords(n) | |
var txt = ""; | |
for (var i = 0; i < numWords; i++) { | |
txt += this.getPageNthWord(n, i, false); | |
} | |
return txt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment