Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cjoy

macrumors member
Original poster
Oct 24, 2008
83
3
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

Code:
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:

Code:
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

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
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

macrumors member
Original poster
Oct 24, 2008
83
3
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.