• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to make a table?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to make a table?


  • Subject: Re: How to make a table?
  • From: Christopher Stone <email@hidden>
  • Date: Sun, 12 Jun 2011 20:20:57 -0500

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
------------------------------------------------------------------------
Christopher Stone                          email@hidden
Joe Winters                                email@hidden
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 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
------------------------------------------------------------------------
Christopher,Stone,email@hidden
Joe,Winters,email@hidden
Joeseph,Peterson,email@hidden
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, 
matchResult:"Joe,Winters,email@hidden"
}, 
{
matchPos:239, 
matchLen:33, 
matchResult:"Joeseph,Peterson,email@hidden"
}, 
{
matchPos:310, 
matchLen:29, 
matchResult:"Joe,Colossus,email@hidden"
}
}
------------------------------------------------------------------------------------------------

This method is seriously fast even with very large tables.

--
Best Regards,
Chris

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >How to make a table? (From: Andreas Mixich <email@hidden>)
 >Re: How to make a table? (From: Michelle Steiner <email@hidden>)
 >Re: How to make a table? (From: Andreas Mixich <email@hidden>)

  • Prev by Date: Re: How to make a table?
  • Next by Date: Re: AppleEvent timeout error
  • Previous by thread: Re: How to make a table?
  • Next by thread: AppleEvent timeout error
  • Index(es):
    • Date
    • Thread