Some while back I wrote a script that places the content of QFX/OFX files on the clipboard for easy import into Numbers 3.x. Tested with Citibank files. It may need minor modification for files produced by other banks.
Usage:
- Copy-paste into Script Editor (in Applications > Utilities
- Run, and choose the file at the prompt.
- Click once in a Numbers cell, and command-v to paste.
SG
--beginning of script
(*
Purpose: Place contents of OFX or QFX financial file to clipboard for pasting into Numbers 3.
Usage: Right click a file in Finder, choose OFX to Clipboard in services, paste when done in an existing table.
Note: May need adjusting to a particular bank's format.
Another note: For big files it needs a few minutes to process.
--SGIII, v1.0b, 201402
*)
set f to choose file with prompt "Choose OFX or QFX file" of type {"OFX", "QFX"}
set tt to read f as «class utf8»
try
--remove unneeded parts and empty lines that complicate parsing
set cTxt to chopText(tt) --remove header and footer stuff
if text of cTxt contains "><" then set cTxt to addLR(cTxt) -- add line returns if ofx file omitted them
set xmlList to stripEmpties(cTxt) -- remove empty lines
--convert ofx to "true" xml format by adding closing tags
repeat with i from 1 to count of xmlList
set theItem to item i of xmlList
set itemi of xmlList to closeTag(theItem) --add closing tag
end repeat
on error
display alert "Check format of OFX or QFX file. May need to adjust script" buttons "Cancel"
end try
--write xml to temporary file -- System Events xml parser will read it from there
set xmlData to printList(xmlList)
set xmlFile to ((path todesktopastext) & "temp.xml") -- "<Computer>:Users:<username>:Desktop:temp.xml"
try
set f to open for accessfilexmlFile with write permission
writexmlDatatof
close accessf
on error
try
close accessfilexmlFile
end try
end try
display notification "Parsing " & length of xmlList & " pieces of data. May take a few minutes. There will be another notice when done." with title "System Events"
--parse the xml file with the xml parser built into System Events
set tsvString to "Amount" & tab & "Date" & tab & "Type" & tab & "Check Number" & tab & ¬
"Payee Name" & tab & "Memo" & return--header row --> set to "" if don't need headers
try
tell application "System Events"
tell XML element "BANKTRANLIST" of contents of XML file xmlFile
repeat with thisElement from 3 to (count of XML elements) -- 1 ,2 are DTSTART, DTEND - so start with 3
tell XML elementthisElement
set amount to value of its (XML elements whose name is "TRNAMT") as string
set trdate to my dateIfy(value of its (XML elements whose name is "DTPOSTED") as string)
set trtype to value of its (XML elements whose name is "TRNTYPE") as string
set checkno to value of its (XML elements whose name is "CHECKNUM") as string
set payee to value of its (XML elements whose name is "NAME") as string
set memo to value of its (XML elements whose name is "MEMO") as string
--add new line to the tsvString:
set tsvString to tsvString & amount & tab & trdate & tab & trtype & tab & checkno & tab & ¬
payee & tab & memo & return
end tell
end repeat
end tell
end tell
on error
error "System Events parser couldn't handle this one. Check format of OFX or QFX source file and adjust script. Check temp.xml (in Trash) for clues of what might have gone wrong."
end try
set the clipboard totsvString
display notification "(Finally) ready to paste " & (count of paragraphs of tsvString) - 1 & " rows into Numbers" with title "Numbers"
--finally trash the temporary file
tell application "Finder"
deletefilexmlFile
end tell
----------------handlers -----------------
to dateIfy(s) --insert - and trim so Numbers can recognize as date
set s to text 1 thru 4 of s & "-" & text 5 thru 6 of s & "-" & text 7 thru 8 of s
return s
end dateIfy
to printList(aList) -- convert list to text for writing to file
set text item delimiters to return
set printOut to aList as string
set text item delimiters to ""
return printOut
end printList
to chopText(tt) --keep only between <BANKTRANLIST> AND </BANKTRANLIST>
set oTid to AppleScript'stext item delimiters
set AppleScript'stext item delimiters to "<BANKTRANLIST>"
set tt2 to "<BANKTRANLIST>" & text item 2 of tt --chop off head
set AppleScript'stext item delimiters to "</BANKTRANLIST>"
set tt2 to text item 1 of tt2 & "</BANKTRANLIST>" --chop off tail
set AppleScript'stext item delimiters to oTid
return tt2
end chopText
to addLR(tt) --add returns if omitted in compressed ofx
set tt to findReplace(tt, "<", "
<")
end addLR
to stripEmpties(tt) -- text in, list out
set ttParas to paragraphs of tt--loads each line as a list item
set xmlList to {}
repeat with i from 1 to count of ttParas
set thisPara to itemi of ttParas
set isNotEmpty to text of thisPara is not ""
if isNotEmpty then set end of xmlList to thisPara
end repeat
return xmlList
end stripEmpties
to closeTag(s) --add closing tag omitted in ofx
if item -1 of s is not ">" then --if already closed
set oTid to AppleScript'stext item delimiters
set AppleScript'stext item delimiters to ">"
set oTag to text item 1 of s & ">"
set tagValue to text item 2 of s
set cTag to "</" & text 2 thru -1 of oTag
set s to oTag & tagValue & cTag
set AppleScript'stext item delimiters to oTid
return s
else
return s
end if
end closeTag
on findReplace(tt, f, r)
set oTid to AppleScript'stext item delimiters
considering case
set AppleScript'stext item delimiters to f
set lst to every text item of tt
set AppleScript'stext item delimiters to r
set tt to lst as string
end considering
set AppleScript'stext item delimiters to oTid
return tt
end findReplace
--end script