Safety Emporium - Lab and Safety Supplies

AppleScripting QuickBooks 2009 Mac

Script Examples and Techniques: Page 3

On the previous page we looked at some handy routines that can help you with Quickbooks. Here we examine some additional methods and tips.

Data Dump Example

Ever need to grab the customer data for manipulation or export? Let's assume that that we have this customer info page open in QB 2009 Mac:

customer info page screen shot

We can use the method described earlier to probe the text fields, or even get them all at once!

If we log the value of the variable customerData, we see that it is:

{"Big One Surf Shop", "Mr", "Big", "E", "Kahuna", "", "", "", "(856) 000-1234", "(856) 555-1212", "Big E Kahuna", "Mr. Big E Kahuna
Big One Surf Shop
PO Box 40
Surf City, NJ 08008", "Mr. Big E Kahuna
Big One Surf Shop
40 Foot Tall Waves St.
Surf City, NJ 08008"}

Which is an AppleScript list. Lists in AppleScript are denoted by the curly brackets. We can manipulate any of the extracted fields in this fashion:

Important note: the data returned is a list of lists. Specifically, the variable customerData contains a single item list, the first item of which has 13 text items. That's why we have that rather funky looking text item of text item construction.

You can also pull out individual bits of data from the page directly if you prefer:

tell application "System Events"

set customerPhone to (value of every text field of tab group 1 of window "Kahuna, Big" of application process "QuickBooks 2009" whose value of attribute "AXDescription" is "Phone") as text

end tell

Using Try Blocks For Error Trapping

Whenever you GUI script an application, you are bound to run into an error at some point. The cursor will be on the wrong field, a page may not be open, etc. Luckily, AppleScript provides error trapping. Just as we use the tell...end tell construction we can use one called try. In fact, try blocks can optionally include an "on error" subsection which excecutes if there is an error in the code. As you can see from this generic construction, it is similar to an if/then/else format:

Here's an example. Suppose our script thinks it opened the Receive Payments window in QuickBooks and we are going to attempt to script an action on that open window. However, what if we accidentally closed that window while the script was running? This construction gives us a chance to correct the issue rather than have the script end with an error message. Try running it with the Receive Payments window open and not open to test it:

This is powerful stuff as you can almost idiot-proof your scripts using Try constructions. You can trap an error and prompt the user to fix the problem before continuing. All of the code we have used in the previous pages would normally be used inside try constructions, but we have omitted that for simplicity.

That last example also gives us a glimpse of the display dialog function of Applescript. Notice that we use the variable theReply to record which button the user clicked. If the user clicks Abort Script, then AppleScript issues a return which stops execution. You can prove this to yourself by inserting a line with 'log "blah"' before the end try statement. If you click on Abort Script, you will not record *blah* in the Script Editor Event Log, but if you click on OK, you will.

On the next page we'll see how to import data from QuickBooks 2009 into Apple Mail using AppleScript.

Table of Contents

Safety Emporium - Lab and Safety Supplies

Copyright 2008-2023 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.