On Jun 12, 2011, at 10:03 AM, Andreas Mixich wrote: I want to make a table, so I can store the values of a CSV file in it and then do some nifty processing on the table. Since the "Table Suite" is contained in the Scripting Additions, I thought I could simply just use it, as I would use the text class or anything else. ______________________________________________________________________
Hey Andreas,
I routinely use the Satimage.osax and its regular _expression_ find/replace to do this sort of thing.
------------------------------------------------------------------------------------------------ # Find Handler - Satimage.osax MODIFIED 2010-09-25 : 00:00 ------------------------------------------------------------------------------------------------ on fnd(findStr, dataSource, caseSensitive, allOccurrences, stringResult) -- CaseSen, AllOccur, StrRslt try set findResult to find text findStr in dataSource ¬ case sensitive caseSensitive ¬ all occurrences allOccurrences ¬ string result stringResult ¬ with regexp return findResult on error return false end try end fnd ------------------------------------------------------------------------------------------------ # Find-Capture Handler - Returns Specified String - Satimage.osax MODIFIED 2011-04-17 : 16:33 ------------------------------------------------------------------------------------------------ on fndUsing(fndStr, returnStr, dataSource, caseSensitive, regExFlag, wholeWord, allOccurrences, stringResult) try set findResult to find text fndStr ¬ in dataSource ¬ case sensitive caseSensitive ¬ regexp regExFlag ¬ whole word wholeWord ¬ using returnStr ¬ all occurrences allOccurrences ¬ string result stringResult return findResult on error # errMsg number errNum return false end try end fndUsing ------------------------------------------------------------------------------------------------ property lookupTable : " ------------------------------------------------------------------------ NAME EMAIL_ADDRESS ------------------------------------------------------------------------ Glenn Anthony email@hidden ------------------------------------------------------------------------ " ------------------------------------------------------------------------------------------------ set findFirstRecord to fnd("^joe.+", lookupTable, false, false, true) set findAllRecordsStartingWith to fnd("^joe.+", lookupTable, false, true, true) set returnOnlyEmailAddress to fndUsing("^Glenn Anthony\\s+(.+)", "\\1", lookupTable, false, true, false, false, true) ------------------------------------------------------------------------------------------------
You can of course use CSV, but I usually use a format similar to the one above for readability and maintainability. I also usually place the table in a plain text file and read it from the script. That way I can use BBEdit (alternatively TextWrangler) to edit and sort the table conveniently.
With regular expressions you can do all kinds of magic.
Okay. Let's try something similar with a CSV table:
property lookupTable : " ------------------------------------------------------------------------ NAME,EMAIL_ADDRESS ------------------------------------------------------------------------ Glenn,Anthony,email@hidden Joe,Colossus,email@hidden ------------------------------------------------------------------------ " ------------------------------------------------------------------------------------------------ set findFirstRecord to fnd("^joe.+", lookupTable, false, false, true) set findAllRecordsStartingWith to fnd("^joe.+", lookupTable, false, true, true) set findFnameJoeUsingDelimiter to fnd("^joe,.+", lookupTable, false, true, true) set findFnameJoeUsingWordBoundary to fnd("^joe\\b.+", lookupTable, false, true, true) set returnOnlyEmailAddress to fndUsing ("^Glenn,Anthony,(.+)", "\\1", lookupTable , false, true, false, false, true) ------------------------------------------------------------------------------------------------ # Another Satimage.osax function: set {fName, lName, emailAdrs} to splittext findFirstRecord using "," ------------------------------------------------------------------------------------------------
It is easy to find records by any field or combination of fields using field-is, field-starts-with, field ends-with, or field-contains.
By changing a parameter you can produce a more informative record of the query:
------------------------------------------------------------------------------------------------ # The Formatting Here is Script Debugger's Pretty Print Feature: ------------------------------------------------------------------------------------------------ set findAllRecordsStartingWith to fnd("^joe.+", lookupTable, false, true, false) # Note last parameter now false
{ { matchPos:214, matchLen:24, }, { matchPos:239, matchLen:33, }, { matchPos:310, matchLen:29, } } ------------------------------------------------------------------------------------------------
This method is seriously fast even with very large tables.
-- Best Regards, Chris
|