On the previous page we looked at some full-length examples of scripts that interacted with Quickbooks.
QuickBooks 2009 Mac can sometime be slow to open a window or load. As a result, if you send emulated keystrokes to QB in a script and QB it is still busy, the keystrokes will arrive before QB is ready and things can go a bit haywire. The following scriptlet can be used to check that QB is ready before you start sending it commands.
-- Is QuickBooks running?
set QBReady to false
tell application "Finder"
if the name of every process contains "Quickbooks 2009" then set QBReady to true
end tell
if not (QBReady) then -- launch Quickbooks
tell application "QuickBooks 2009" to activate
end if
-- Tricky part, fabulous solution. Check CPU cycles of QuickBooks to ensure it is done loading or working.
set QBReady to check_CPU_Usage(30, "QuickBooks")
if not (QBReady) then -- QB is off in la-la-land
display dialog "Sorry, QuickBooks is not responding" with icon note buttons ["Cancel"] default button 1
end if
-- End of main script
on check_CPU_Usage(threshold, thisProcess)
-- reports the CPU usage
-- inspired by http://discussions.apple.com/message.jspa?messageID=6564005
set myUsage to 100
repeat with myCount from 1 to 60
delay 1
tell application "System Events"
set pid to unix id of process thisProcess
set myUsage to do shell script "ps aux | grep " & pid & " | grep -v grep | awk '{print $3}'"
end tell
if (myUsage as integer) < threshold then return true -- QuickBooks is ready
end repeat
return false
end check_CPU_Usage
In the subroutine, the 1 to 60 loop is aribitrary, but you definitely want it to be several seconds longer than it takes for QB to launch on your system. The value of 30 *sent* to the subroutine is an empirically determined level of CPU cycles that we came up with based on watching what QB was doing while Apple's Activity Monitor window was open. You can adjust that level up or down depending on how QB behaves on your system.
QuickBooks 2011 Note: - We're told that QB 2011 has added an AppleScript call named QBIsIdle that can be used for the same purpose as the check_CPU_Usage.
Oddly, AppleScript lacks a basic Find/Replace command. Which is a royal pain if you want to do something like replace "Street" with "St" in addresses. Luckily, Apple has published a general subroutine for Find/Replace:
--
-- Search/replace subroutine
-- Direct from http://www.apple.com/applescript/guidebook/sbrt/pgs/sbrt.07.htm, which is now missing, of course...
--
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as text
set AppleScript's text item delimiters to""
return this_text
end replace_chars
We can test this subroutine out by typing the following lines above the subroutine in Script Editor and looking at the results in the Event Log window.
set shippingAddress to "Jimmy Jones" & return & "Some Random Company, Incorporated" & return & "1313 Random Boulevard" & return & "Funkodelphia, California 90210"
log shippingAddressset shippingAddress to replace_chars(shippingAddress, "Incorporated", "Inc.")
log shippingAddressset shippingAddress to replace_chars(shippingAddress, "Boulevard", "Blvd")
log shippingAddressset shippingAddress to replace_chars(shippingAddress, "California", "CA")
log shippingAddressBut an even easier way to handle Find/Replace is to utilize an OSAX as discussed on the first page of this tutorial. Satimage OSAX contains powerful find/replace commands as well a whole host of other very powerful features such as expanded mathematical functions, arrays, list utilities, and text manipulation capabilities. The speed boost when processing large text files with the OSAX installed can be 10x or more!
On the next page we'll see some really nice and powerful techniques.
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.