I am converting my old-style Applescript script libraries (used with the load script command) to the new Mavericks’ Script Libraries. Learning from the most useful video tutorial on macosxautomation.com I have, of course, seen huge performance gains when using ASObjC in these libraries. It’s heartening to see Apple breathe new life into Applescript this way (if you’re reading this, Tim…).
I have progressed from basic string manipulation and am now trying to harness the power of ASObjC in sorting lists (and later, I hope, lists of records). I know it’s almost a trivial matter to sort a one-dimensional list with something like this (no error-checking):
on sort list inList
set anNSArray to current application's NSArray's arrayWithArray:inList
set sortedArray to anNSArray's sortedArrayUsingSelector:"compare:"
return sortedArray as list
end sort list
But I now want to sort a list of lists, based on the value of item x in each sublist. How can I stipulate my own selector instead of the built-in “compare:” above? Perhaps theMutableArray below should be subclassed? Is that even possible with Applescript libraries?
What I have so far:
on sort list of lists inList by sortElement
-- attempt to use ASobjC to sort a list of records... WIP, very WIP...
set theMutableArray to current application's NSMutableArray's alloc's init()
set eSort to sortElement
set listCount to count of inList
repeat with i from 1 to listCount
-- convert dates to NSDates, etc? In progress...
(theMutableArray's addObject:(item i of inList))
end repeat
-- Array seems to be set up as expected:
log (theMutableArray's objectAtIndex:sortElement)'s item 1 as text
-- this fails
set theSortedArray to theMutableArray's sortedArrayUsingSelector:"compareItem:"
return theSortedArray as list
end sort list of lists
on compareItem:anItem
return
end compareItem:
I know I could just carry on using the excellent ASObjC Runner, but I’m trying to learn to roll my own…
Any help/pointers gratefully received!
Quentin