Records vs. Single Value elements


I would be grateful for some advice of a general nature“i.e., a level above the script syntax.“i don’t mind stumbling slowly forward getting the latter right, as long as i have conceived the project correctly, which i am afraid i haven’t.

I apologise for the length of this post“but i didn’t want anyone nice enough to offer assistance to be foreced to waste their time trying to deciper what i was talking about so i’ve tried to make this complete and clear.

Here’s what i’m trying to do (a common task for AS i suspect):

(i) get some data via XML-RPC;

(ii) read the output to a file (in e.g., TextEdit);

(iii) clean it up (parse out the XML and delimited in accord with my DB structure); and

(iv) put that data into a table in a spreadsheet (OmniOutliner, but it could be excel or an SQL client“doesn’t really matter) on its way to a proper databse.

There’s absolutely nothing idiosyncratic about my DB structure“to frame the issue here you can just Imagine if it were, e.g., a Table whose title was “A List of OO Programs” and the column headers where “Name,” “Creator,” “Date of First Use” “Initial Application,” and so forth. And of course my data (what’s in the file referred to above that i’ve performed the string manipulation on) consists of the data i’ve found to populate it“e.g., SmallTalk, Alan Kay,…

No real problems with (i) through (iii)“in fact, AS does these things brilliantly (and rather tolerant of scripter error i noticed).

I am struggling with step 4.

Initially that was because i had to decipher OO’s dictionary to find get the command syntax correct, now that i think i’ve got that squared away enough to move on.

Now i’m stuck on the best (most reliable, most flexible) way to get the data in the spreadsheet. My first attempt involved copying my cleaned-up records as list items into a variable (e.g., data_List) using a flow-control statement and then using another FCS to extract each item and put it into its proper column.

Simple enough, but it seems not very clean and more importantly, i would like my code to be flexible enough to account for changes in my Database structure (e.g., new tables added, column order switched around, and so forth).

Therefore, i researched the AS Manuals on the ADC Site, as well as the tutorials, script examples, etc. on This Site, and it seems i want to use Records“which if i’ve got it right, are just AS’s term for name/value pairs so that my list items are two-string pairs, e.g., {Creator: Alan Kay}, {Program Name: SmallTalk}.

So I guess my first question is: is this correct (i.e., is this structure a good way, w/in AS’s repertoire, to achieve the flexibility i’m after)?

Second, i’m having difficultly in finding the syntax to copy my data (once in record format) to the Application container (it’s not on the App side i’m struggling with this time, but the Applescript syntax itself).

Grateful for any thoughts.


I assume you’re putting your data in this form:

set Data_1 to {Author:"Alan Kay", Creator:"SmallTalk", Prog_Name:"Turtle", Created:"Oct 1, 2003", First_Use:"Nov 2, 2003"}

-- note that you can't name a field "Name"; that's an AppleScript reserved term and that the names of the categories must be contiguous characters

set Col_1 to Data_1's Creator --> SmallTalk

More complex, if you want to build your list of records from the paragraphs of your parsed text, then:

set Data_Rows to "{Author:\"Alan Kay\", Creator:\"SmallTalk\", Prog_Name:\"Turtle\", Created:\"Oct 1, 2003\", First_Use:\"Nov 2, 2003\"}
{Author:\"George Jones\", Creator:\"JavaScript\", Prog_Name:\"Platypus\", Created:\"Oct 9, 2005\", First_Use:\"Nov 22, 2005\"}"

-- Get a list of the text's paragraphs.
set mainList to Data_Rows's paragraphs

-- Turn each paragraph in the list into a record
repeat with thisPara in mainList
	set thisPara's contents to (run script (text of thisPara))
end repeat

mainList --> {{Author:"Alan Kay", Creator:"SmallTalk", Prog_Name:"Turtle", Created:"Oct 1, 2003", First_Use:"Nov 2, 2003"}, {Author:"George Jones", Creator:"JavaScript", Prog_Name:"Platypus", Created:"Oct 9, 2005", First_Use:"Nov 22, 2005"}}

-- get one back:
mainList's (item 1)'s Creator --> SmallTalk

Perfect Adam, that’s exactly what i was after (particularly the last snippet).

Thanks very much indeed.


Glad I hit upon it - sometimes it’s hard to tell exactly what the “customer” wants. :wink: