PDA

View Full Version : AppleScript - Alternatives for associative array?




cjoy
Aug 14, 2013, 05:03 PM
I need to compare file properties for two sets of files and while this would be a piece of cake with assoc. arrays at hand, I fail to see how to solve this in a halfway decent manner without such an object.

In AppleScript I have lists like these

filesA = {{filename:"filename X.tif",width:"500"},{filename:"filename Z.tif",width:"200"}, ...}
filesB = {{filename:"filename Z.tif",width:"400"},{filename:"filename v.tif",width:"200"},{filename:"filename X.tif",width:"100"}, ...}

I could iterate over every item of list A and for each item iterate over the entire list b to find matching filenames, but that is horribly inefficient. There ought to be a way to do this in AppleScript that is not a complete PITA, right?

What I aim for is something along these lines (pseudocode), which is obviously not the way to do it in AppleScript:

filesA = array(
"filename X.tif" => 500,
"filename Z.tif" => 200
);

filesB = array(
"filename Z.tif" => 400,
"filename V.tif" => 200,
"filename X.tif" => 100
);

foreach (filesA AS key => value)
ratios[key] = value / filesB[key];
end foreach

Any help much appreciated :)



chown33
Aug 15, 2013, 12:22 PM
I would probably use AppleScript to write two text files, containing the data from the lists, then process that data using a language that has associative arrays. This assumes you already know such a language, or can learn one with relative ease.


To stay entirely in AppleScript, I'd probably switch each "list of lists" to a "list of records", where each record had a name property, size property, etc. (I assume you can find a doc or tutorial on making AppleScript record objects with arbitrary property names.)

Then I'd probably use a 'whose' clause on the big list to get every item whose name (a property) matched the name I was working on (the iterating object's name). Or if the big list is presumed to have only one item with a given name, then first item whose name matched.


You didn't say what your experience or skill level with AppleScript or other languages is. If you don't know about 'whose' clauses, but do know awk or perl or python, then try the first approach. If you don't know 'whose' clauses and know no other languages, then try the second approach by learning about whose clauses.

Example briefly describing whose clauses:
http://daringfireball.net/2003/09/applescript_whose_clauses

Found by googling applescript whose clause


I wouldn't worry about the speed unless you have 10's of thousands of items in the lists. Since you said nothing about the scale, I'll assume scale isn't an issue, because most people working with something on a huge scale will say so.

cjoy
Aug 15, 2013, 02:37 PM
Thank you very much for the assist, I'll look into the "whose approach" for sake of understanding it. I don't think AppleScript has good chances of making it to my favorites though ;)

In the meanwhile I had already resorted to writing each list to a temporary csv file, process them with php (yes - another elegant language there :p), then fetch the result as a list again.

Red Menace
Aug 15, 2013, 06:35 PM
The filter reference form (https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_reference_forms.html#//apple_ref/doc/uid/TP40000983-CH4g-BAJJHEFE) ("whose clause") only applies to application objects - you can't use it with regular AppleScript records or lists.