A useful Script to Process Paystubs + a question about how to improve it

I am responsible for payroll at the small organization that I work at, and one of the related tasks is distributing paystubs to all of our employees. The way that my predecessor would do this is by printing off all of the paystubs, handwriting the Employee’s name on the back of the paper it was printed on, and then folding the paystubs and taking them to the internal mailboxes that they needed to be distributed to.

Naturally, the automation addict in me was allergic to this solution… so I wrote up this little script to a) insert a blank page in between every paystub in the document b) read the employees name from the paystub c) insert the employee’s name on the blank page immediately following the paystub. I’ll Insert the text of the script below if you are interested in adapting it for your own uses.

on findAndReplaceInText(theText, theSearchString, theReplacementString)
	set AppleScript's text item delimiters to theSearchString
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to theReplacementString
	set theText to theTextItems as string
	set AppleScript's text item delimiters to ""
	return theText
end findAndReplaceInText

tell application "PDFpenPro"
	if (count documents) > 0 then
		set pageCount to count (pages of document 1)
		set pageCount to pageCount * 2
		repeat with pageNumber from 1 to pageCount
			if pageNumber mod 2 = 0 then
				tell document 1
					try
						set pageToSelect to (get page pageNumber)
						
						duplicate page 1 to after pageToSelect
						
					end try
				end tell
			end if
		end repeat
		repeat with pageNumber from 1 to (pageCount - 1)
			if pageNumber mod 2 is 0 then
				tell document 1
					set imprintCount to 0
					try
						set currentPage to (get page pageNumber)
						set content to text content of currentPage
						set wordCount to count words of content
						repeat with theWordNumber from 1 to wordCount
							tell content
								set theWord to word theWordNumber of content
								if theWord = "Name" then
									set firstName to word (theWordNumber + 1) of content
									set lastName to word (theWordNumber + 3) of content
									if lastName = "xxx" or "SS" then
										set lastName to word (theWordNumber + 2) of content
									end if
								end if
								
							end tell
						end repeat
						
					end try
					
				end tell
				set theName to firstName & " " & lastName
				set AppleScript's text item delimiters to "SS"
				set theTextItems to every text item of theName
				set AppleScript's text item delimiters to ""
				set theName to theTextItems as string
				set AppleScript's text item delimiters to ""
				tell document 1
					
					set imprintCount to 0
					set newPageNumber to pageNumber + 1
					set thePage to (get page newPageNumber)
					set theImprint to make new text imprint with properties {rich text:theName} at the end of imprints of thePage
					set font of attribute run 1 of rich text of theImprint to "LucidaGrande-Bold"
					set size of attribute run 1 of rich text of theImprint to 30
					set color of theImprint to {1, 0, 0}
				end tell
			end if
		end repeat
	end if
end tell

I only have 2 issues I’m still trying to fix

  1. when I try to create a new page in pdfPen Pro, the program hangs so I have to just create a blank page at the start of the document before I run the script and delete it after, any ideas as to how I could create a blank page and avoid this step altogether?
  2. For some wacky reason, If a person’s last name ends with an “S” it is always capitalized. for example my name comes out as expected “Chad Wollenberg” but if Miles Morales was on my payroll, his name would come out as “Miles MoraleS” does anyone have any idea why that would happen?!

altogether this takes a 30 minute task and makes it a 5 minute task, which is wonderful. I hope you find a use for an adapted version in your workflow.

2 Likes

In regards to your outstanding issues, would you be able to:

  1. Share a suitably anonymised and salary zeroed example file to check the script with?

    • This will ensure we have a solid example that you know doesn’t work and people can test potential fixes against.
  2. Note which version of PDFpen Pro you are using.

    • It is entirely possible that the version may have something to do with your issues, so knowing that means that there may be some latitude for folks to test against multiple versions, and know which is the baseline that you are working with.
1 Like

I’ve created a dropbox folder with 2 PDF’s one is the file that my system outputs, one is the product of my script. here is a link to the shared folder
I’m running PDFPen Pro 10.2.2
remember that you have to insert a blank page at the front of the document before you run the script with the way that the script is currently written.