Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old Aug 14, 2013, 05:03 PM   #1
macrumors member
Join Date: Oct 2008
AppleScript - Alternatives for associative array?

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
cjoy is offline   0 Reply With Quote
Old Aug 15, 2013, 12:22 PM   #2
macrumors 603
Join Date: Aug 2009
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:

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.
chown33 is offline   0 Reply With Quote
Old Aug 15, 2013, 02:37 PM   #3
Thread Starter
macrumors member
Join Date: Oct 2008
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 ), then fetch the result as a list again.
cjoy is offline   0 Reply With Quote
Old Aug 15, 2013, 06:35 PM   #4
Red Menace
macrumors 6502
Join Date: May 2011
Location: Littleton, Colorado, USA
The filter reference form ("whose clause") only applies to application objects - you can't use it with regular AppleScript records or lists.
MacBook Pro / OS X Yosemite (10.10.3) / Xcode 6.3.1 / RubyMotion FTW
Red Menace is offline   1 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
AppleScript: How do I save multiple Excel sheets to one pdf using Applescript sbtaylor1 Mac Programming 0 Oct 28, 2013 11:38 PM
AppleScript: How do I save multiple Excel sheets to one pdf using Applescript sbtaylor1 Mac Applications and Mac App Store 0 Oct 28, 2013 11:32 PM
iWeb alternatives are really not alternatives! the7th1 Web Design and Development 30 Dec 8, 2012 08:19 PM
Array Containing Dictionaries ahan.tm iPhone/iPad Programming 2 Nov 18, 2012 10:31 AM

Forum Jump

All times are GMT -5. The time now is 10:24 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2015, MacRumors.com, LLC