Applescript – Delimit records and lists

Please, of course I know that.

I ran the script inside a repeat loop a few thousand times and got always the same string.

Not so fast…

use framework "Foundation"

set mylist to {a4:4, a10:10, a7:7, a2:2, a5:5, a8:8, a3:3, a6:6, a1:1, a9:9}
set myDict to (current application's NSDictionary's dictionaryWithDictionary:mylist)
set theResult to (myDict's allValues()'s componentsJoinedByString:tab) as text 
--> "4	1	7	2	5	8	3	10	6	9"

:lol: This topic could have been reduced to three posts if #2 had been:

Hi. Yes”easily.

set myRecord to {CustomerID:"Customer 55555", OrderID:"55555", CustomerName:"Mr Robot"}
myRecord's OrderID & tab & myRecord's CustomerID & tab & myRecord's CustomerName

Then I suspect that Script Debugger uses some backing array to keep the order of the keys

I don’t think so. I think it’s a question of hashing, and a matter of the length of the keys and the number of items.

Here’s the script I used to generate my odd-looking record:

use framework "Foundation"

set x to {}
set y to {}
repeat with i from 1 to 10
	set end of x to "a" & i
	set end of y to i
end repeat
-- make AS record
set theRecord to (current application's NSDictionary's dictionaryWithObjects:y forKeys:x) as record
-- make it into a dictionary and convert it back to a record
set newRecord to (current application's NSDictionary's dictionaryWithDictionary:theRecord) as record
return {theRecord, newRecord}

If you change “a” to “ab”, so the keys are “ab1” and so on, the orders match. But if you then change the repeat loop to 100 times, they gain do not match. You had only three longer keys, so you didn’t see the problem. Extrapolating from examples is a risky business :slight_smile:

OTOH, with AppleScript, we know how records are implemented, and we can be confident that they’ll keep their order under specified conditions – indeed, we can’t delete an item even if we want to.

But the idea of saving a few keystrokes and relying on it just strikes me as writing a bug waiting to bite you when something about the record changes at a future date, not to mention the sort of habit that will end up having one do it under the wrong conditions.

And we can always dream the AppleScript will implement records properly one day…