On the previous page we saw how to use AppleScript to export QuickBooks data to Mail (or other programs) as well as how to print statements and reports. On this page, we will use AppleScript and JavaScript to do simple payroll for free. And don't worry, you don't need to know how to do JavaScript for this to work.
The Payroll "feature" of QuickBooks for Mac is not actually part of QuickBooks. It requires an external monthly subscription. While payroll can be complex, most small businesses have only a handful of employees and simple needs, in which case payroll services are extreme overkill and can cost hundreds of dollars per year.
To write your own payroll checks in QuickBooks, you simply set up several accounts and allocate funds to them as line items when you write the check. This is discussed in detail at Intuit and in one of our posts to the QuickBooks forums. While it appears daunting at first, it is really quite easy, and the scripts below will do it all for you once you've set up the accounts!
In this example, we have lumped the Medicare, Social Security, unemployment taxes etc. into the accounts shown. You could make sub-accounts in each of these for each employee to avoid having to tease individual data back out of them in Excel at the end of the year to generate W-2's. We've demonstrated that here under Salary only:
We will also assume that you have the employees listed as "Cunningham, Marion", "Jones, Jimbo", "Malph, Ralph" for check-writing purposes
You might have other Payroll expenses to add, or you might have multiple state payroll taxes (which we have in NJ, but we just lump them as one as we don't care to see the subdivisions). This is just a non-exhaustive example that you can use to learn how to add those yourself.
The big sell of payroll services is that they automatically update when the tax rates and thresholds change. That's a definite advantage, but those change once a year or less in most cases. If you actually read the mailings that your state and the Feds send you, you'll know when the changes are coming.
Another benefit of payroll services is dealing with taxes in which only the first $x of the employee's salary are taxable. So we've created a handy Javascript payroll calculator that does those calculations for you. And, in a moment, we will show you how to automatically import that data into QuickBooks 2009 for Mac to generate a payroll check!
First, a few comments about the calculator. You can use it on-line, of course, but we want you to save a copy of it to your hard drive so you can use a text editor to change the default settings to reflect the thresholds/rates in your state. Once you've done that, you can open the copy on your hard drive in your web browser when it's time to do payroll. Each calculation only requires four pieces of data, and you can hard code in three of them, so it will take you maybe 15 seconds to enter everything you need for the simple case we've set up.
We could have written the entire calculator in AppleScript, but AppleScript has limited abilities for user input. A web page form solves that issue. And then we used JavaScript within that web page rather than rely on AppleScript to do the calculations. This tutorial is about AppleScripting, not JavaScripting, so we won't dwell on the technical reasons for that decision.
Finally, note that we have only run and tested the calculator and AppleScript with the Safari web browser. If you want to figure out how to script other browsers, fine, but please don't ask us to do it.
Once you have run the Javascript payroll calculator to figure out an employee's paycheck, you can run this script to bring the data right into QuickBooks! Simply leave the window with the calculated results open in Safari and then run this script which makes use of AppleScript's ability to read and send JavaScript events to web pages:
Note: We do not show the import of local payroll taxes in this example as we don't have any (yeah, but you should see our property taxes).
-- This script assumes you have your calculated results open in a Safari Window and configured the accounts we specified.
-- We'll import the elements from the Safari page's output table and strip out non-numeric characters on the way in.
set thisDocument to "JavaScript payroll calculator" -- easier than writing that every time.
tell application "Safari"
try
set ssEmployeeAmount to do JavaScript "stripAlphaChars(document.getElementById('ssEmployeeAmount').innerText);" in document thisDocument
set ssCompanyAmount to do JavaScript "stripAlphaChars(document.getElementById('ssCompanyAmount').innerText);" in document thisDocument
set medicareEmployeeAmount to do JavaScript "stripAlphaChars(document.getElementById('medicareEmployeeAmount').innerText);" in document thisDocument
set medicareCompanyAmount to do JavaScript "stripAlphaChars(document.getElementById('medicareCompanyAmount').innerText);" in document thisDocument
set futaCompanyAmount to do JavaScript "stripAlphaChars(document.getElementById('futaCompanyAmount').innerText);" in document thisDocument
set stateUnemployEmployeeAmount to do JavaScript "stripAlphaChars(document.getElementById('stateUnemployEmployeeAmount').innerText);" in document thisDocument
set stateUnemployCompanyAmount to do JavaScript "stripAlphaChars(document.getElementById('stateUnemployCompanyAmount').innerText);" in document thisDocument
set federalWithheldAmount to do JavaScript "stripAlphaChars(document.getElementById('federalWithheldAmount').innerText);" in document thisDocument
set stateWithheldAmount to do JavaScript "stripAlphaChars(document.getElementById('stateWithheldAmount').innerText);" in document thisDocument
set netPaycheckAmount to do JavaScript "stripAlphaChars(document.getElementById('netPaycheckAmount').innerText);" in document thisDocument
--From the input table:
set currentGross to do JavaScript "stripAlphaChars(document.getElementById('currentGross').value);" in document thisDocument
-- JavaScript to get the selected item in a select list is very obtuse:
set employeeName to do JavaScript "document.inputValues.employeeName[document.inputValues.employeeName.selectedIndex].value;" in document thisDocument
on error
display dialog "Sorry, I got an error trying to grab the data." with icon stop
return
end try
end tell
-- Input is complete! Now tell QuickBooks to open a check. We will assume you use an account named "Checking Account"
tell application "QuickBooks 2009"
activate
delay 0.4
-- Write checks
tell application "System Events"
keystroke "k" using {command down} -- keyboard command to Write Checks
delay 0.4
-- Hit up arrow 3 times to get into the Account Selection Field
keystroke (ASCII character 30) & (ASCII character 30) & (ASCII character 30)
delay 0.2
-- Enter the bank account name, then advance to the "Pay to the Order of field":
keystroke "Checking Account" & tab & tab & tab
delay 0.2
-- Fill in employee name then advance down to the Memo line
keystroke employeeName & tab & tab & tab
delay 0.2
-- Fill in a memo then advance to line items.
keystroke "Monthly payroll check" & tab -- change this to your liking
-- You can change the order of the following, of course.
-- Gross salary entry
delay 0.3
keystroke "Payroll Expenses:Salary:" & employeeName & tab & currentGross & tab & "Gross Salary" & tab & tab
delay 1
-- State withholding
keystroke "NJ Withholding" & tab & "-" & stateWithheldAmount & tab & "NJ Withholding" & tab & tab
delay 1
-- Federal withholding
keystroke "Federal Withholding" & tab & "-" & federalWithheldAmount & tab & "Federal Withholding" & tab & tab
delay 1
-- Employee Medicare withholding
keystroke "Medicare" & tab & "-" & medicareEmployeeAmount & tab & "Medicare Withholding" & tab & tab
delay 1
-- Employee Social Security withholding
keystroke "Social Security" & tab & "-" & ssEmployeeAmount & tab & "Social Security Withholding" & tab & tab
delay 1
-- Employee State Unemployment withholding
keystroke "NJ FUTA" & tab & "-" & stateUnemployEmployeeAmount & tab & "NJ State FUTA Withholding" & tab & tab
delay 1
-- Company Federal Unemployment, part 1
keystroke "Federal FUTA" & tab & "-" & futaCompanyAmount & tab & "Company Federal FUTA" & tab & tab
delay 1
-- Company Federal Unemployment, part 2
keystroke "Payroll Expenses" & tab & futaCompanyAmount & tab & "Company Federal FUTA" & tab & tab
delay 1
-- Company State Unemployment, part 1
keystroke "NJ FUTA" & tab & "-" & stateUnemployCompanyAmount & tab & "Company State FUTA" & tab & tab
delay 1
-- Company State Unemployment, part 2
keystroke "Payroll Expenses" & tab & stateUnemployCompanyAmount & tab & "Company State FUTA" & tab & tab
delay 1
-- Company Social Security, part 1
keystroke "Social Security" & tab & "-" & ssCompanyAmount & tab & "Company Social Security" & tab & tab
delay 1
-- Company Social Security, part 2
keystroke "Payroll Expenses" & tab & ssCompanyAmount & tab & "Company Social Security" & tab & tab
delay 1
-- Company Medicare, part 1
keystroke "Medicare" & tab & "-" & medicareCompanyAmount & tab & "Company Medicare" & tab & tab
delay 1
-- Company Medicare, part 2
keystroke "Payroll Expenses" & tab & medicareCompanyAmount & tab & "Company Medicare" & tab & tab
delay 1
end tell
end tell
We haven't scripted hitting the OK button on the check input screen. With 14 lines entered in the check, we figured that you'd want to manually review things at least until you're sure that this script works reliably for you.
If the check is to your liking, click OK, and you're done with payroll entry for that employee. To enter more payroll checks, go back to your JavaScript Payroll Form, change the employee name (and amounts if necessary) and run this AppleScript again. It's that easy.
If you read carefully, you should have noticed that one can use JavaScript to not only get data from a web page form, but to enter or change the data as well. In other words, you can use AppleScript to automate all sorts of tasks, such as creating a shipment on the UPS web site or logging into a supplier's system to order new inventory! Even without forms, you can grab the entire contents of a web page and then parse out data you want such as stock prices, box scores, and more.
All of that goes beyond the scope of this tutorial, but to show you that it can be done, open our Javascript payroll calculator in Safari and then copy and paste this script into Apple's Script Editor. Check out how the indicated items change as you go along!
set thisDocument to "JavaScript payroll calculator" -- easier than writing that every time.
-- Change the value in a form input box
display dialog "Enter a new value for YTD Gross Pay" default answer "425,000"
set newPay to the text returned of the result
tell application "Safari"
do JavaScript "document.inputValues.currentGross.value='" & newPay & "';" in document thisDocument
end tell
-- Select a menu item
-- complicated example; sends a whole bunch of JavaScript to read the menu items and find one that matches!
set thisGuy to choose from list {"Cunningham, Marion", "Malph, Ralph", "Jones, Jimbo"} with prompt "Change the selected employee" default items {"Jones, Jimbo"}
tell application "Safari"
do JavaScript "var thisMenu =document.inputValues.employeeName;var thisEmployee = /" & thisGuy & "/i; for(count=0; count< thisMenu.length; count++) {if (thisEmployee.test(thisMenu[count].text)) thisMenu.selectedIndex=count;}" in document thisDocument
end tell
-- Change text
display dialog "Change the Social Security Withholding amount to: " default answer "Way too much"
set newText to the text returned of the result
tell application "Safari"
do JavaScript "document.getElementById('ssEmployeeAmount').innerText='" & newText & "';" in document thisDocument
end tell
-- Press the calculate button
display dialog "Press the Calculate button on the form" default answer "Do it!"
tell application "Safari"
do JavaScript "document.getElementById('calculate').click();" in document thisDocument
end tell
That's all the examples we have for now. We may add others as time permits, but can't guarantee that.
Copyright 2008-2024 by Interactive Learning Paradigms Incorporated. All rights reserved. Reposting of this material is not permitted and constitutes a violation of US and International copyright law.