On the previous page we looked at some handy routines that can help you with Quickbooks. Here we examine some additional methods and tips.
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:
We can use the method described earlier to probe the text fields, or even get them all at once!
tell application "System Events"
set customerData to get value of every text field of every tab group of window "Kahuna, Big" of process "QuickBooks 2009" as text
If we log the value of the variable customerData, we see that it is:
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:
set customerEmail to text item 8 of text item 1 of customerData
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
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:
-- a block of code to execute
-- execute this block of code instead
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:
tell application "QuickBooks 2009"
set theCoordinates to the bounds of window "Receive Payments"
tell me to set theReply to display dialog "Dude, please open the Receive Payments window and then click OK" with icon note buttons ["Abort Script", "OK"] default button 2
if (button returned of theReply is "Abort Script") then return
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
Copyright 2008-2021 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.