Skip to content

Instantly share code, notes, and snippets.

@rjames86
Created July 11, 2015 13:51
Show Gist options
  • Save rjames86/8ee61652087a2c44802f to your computer and use it in GitHub Desktop.
Save rjames86/8ee61652087a2c44802f to your computer and use it in GitHub Desktop.
app = Application("PdfPenPro")
app.includeStandardAdditions = true;
templatePath = Path("/Users/username/Rent Receipt Template.pdf")
savePath = Path("/Users/rjames/apartment")
var getByName = function(fileName){
return app.documents.byName(fileName);
}
var getDocPage = function(fileName, pageNum) {
return getByName(fileName).pages[pageNum-1];
}
function closeDocument(doc) {
app.close(doc)
}
function prompt(text, defaultAnswer) {
var options = { defaultAnswer: defaultAnswer || '' }
try {
return app.displayDialog(text, options).textReturned
} catch (e) {
return null
}
}
function createNewReceipt() {
/*
Opens the Receipt template
Creates a new document and duplicates
the template into the new doc
*/
app.open(templatePath)
var currentDoc = app.windows[0].document
var currentDocName = currentDoc.name()
doc = app.Document().make()
app.duplicate(getDocPage(currentDocName, 1), {to:doc})
return doc
}
function formatDate(dt) {
month = (dt.getMonth() + 1)
day = dt.getDate()
paddedMonth = month < 10 ? "0" + month : month
paddedDay = day < 10 ? "0" + day : day
return paddedMonth + "/" + paddedDay + "/" + dt.getFullYear()
}
function addMonths(delta) {
today = new Date()
today.setDate(1) // Set the day to be the first
today.setMonth(today.getMonth() + delta)
return today
}
function rentReceiptName(dt) {
year = dt.substring(6)
month = dt.substring(0,2)
return year + "-" + month + " Rent Receipt.pdf"
}
function getFormField(doc, field) {
formField = doc.pages()[0].imprints.whose({fieldName: field})()
if (formField.length) {
return formField[0]
}
return
}
function setFormField(doc, field, value) {
theField = getFormField(doc, field)
if (theField.class() == "button") {
theField.checked = value
} else {
theField.value = value
}
}
dateOptions = [-1, 0, 1].map(addMonths).map(formatDate)
dateOptionsOffset = [0, 1, 2].map(addMonths).map(formatDate)
dateChoice = app.chooseFromList(
dateOptions,
{withTitle: "Start Date",
withPrompt: "Choose Start Date",
defaultItems: dateOptions[1],
multipleSelectionsAllowed: false,
emptySelectionAllowed: false}
)[0]
PeriodTo = dateOptionsOffset[dateOptions.indexOf(dateChoice)]
receivedFrom = app.displayDialog("Who sent the check?", {defaultAnswer: ""})
receivedBy = app.displayDialog("Who received the check?", {defaultAnswer: "Ryan Morehouse"})
receivedAmount = app.displayDialog("How much did they pay?", {defaultAnswer: "695"})
apartmentNumber = app.chooseFromList(
[1,2,3,4],
{withTitle: "Apartment Number",
withPrompt: "Choose Apartment Number",
defaultItems: 1,
multipleSelectionsAllowed: false,
emptySelectionAllowed: false}
)[0]
paymentType = app.chooseFromList(
["Check", "Cash", "Other"],
{withTitle: "Payment Type",
withPrompt: "Choose Payment Type",
defaultItems: "Check",
multipleSelectionsAllowed: false,
emptySelectionAllowed: false}
)[0]
checkNumber = paymentType == "Check" ? app.displayDialog("Check Number?", {defaultAnswer: ""}).textReturned : ""
otherType = paymentType == "Other" ? app.displayDialog("What was the payment type?", {defaultAnswer: ""}).textReturned : ""
validFieldNames = ["ReceivedFrom", "RentAt", "PeriodFrom", "PeriodTo", "ReceivedBy", "Sum"]
fieldsToFill = [
{ value: receivedFrom.textReturned, fieldName: "ReceivedFrom" },
{ value: "724 S Garfield. #" + apartmentNumber, fieldName: "RentAt" },
{ value: checkNumber, fieldName: "CheckNumber" },
{ value: otherType, fieldName: "Other" },
{ value: receivedAmount.textReturned, fieldName: "Sum" },
{ value: receivedBy.textReturned, fieldName: "ReceivedBy" },
{ value: dateChoice, fieldName: "PeriodFrom" },
{ value: PeriodTo, fieldName: "PeriodTo" },
{ value: paymentType == "Check" ? true : false, fieldName: "CheckCheckBox" },
{ value: paymentType == "Other" ? true : false, fieldName: "OtherCheckbox" }
]
newDoc = createNewReceipt()
fieldsToFill.map(function(obj){setFormField(newDoc, obj.fieldName, obj.value)})
saveLocation = app.chooseFileName({defaultName: rentReceiptName(dateChoice), defaultLocation: savePath})
newDoc.save({in: saveLocation})
app.documents().map(closeDocument)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment