Wow, that’s a lot of data to go through! Let’s see what can be done to help get this done faster.
There is two kinds of arrays. Depends on wether you need to makes modifications in them or not. NSArray and NSMutableArray objects are initiated like this:
tell current application's NSArray to set theArray to arrayWithArray_(((paragraphs of theText) as list))
tell current application's NSMutableArray to set theMutableArray to arrayWithCapacity_(255) --don't worry about the number, it will automatically expand as needed, it's only a start point.
theMutableArray's addObjectsFromArray_(((paragraphs of theText) as list))
For finding things in arrays, there is somethings much more powerful than a repeat loop: NSPredicate.
You first need to create a new NSPredicate object that will define what to find in the array. Like this (with NSArray):
tell current application's NSPredicate to set thePredicate to predicateWithFormat_("SELF contains[cd] %@", stringToFind) --there is different matching options possible, this should do the trick for your needs
set theFilteredArray to theArray's filteredArrayUsingPredicate_(thePredicate)
and with NSMutableArray:
tell current application's NSPredicate to set thePredicate to predicateWithFormat_("SELF contains[cd] %@", stringToFind)
set theFilteredArray to theMutableArray's filterUsingPredicate_(thePredicate)
in both scenarios, the stringToFind variable is, obviously, what you need to look for, thus have it ready in advance. In both cases the result will be an array with only the matching content. You can then count them, sort them, etc. The documentation in Xcode for both methods will list everything you can do to them. NSMutableArray is a subclass of NSArray, so any method that works for the latter will work on the former.
There is also a method in NSArray that only checks if a certain object exists in the array and then returns a true or false. But it’s a bit more tricky because you need to create an NSString object before it can work. So it could go like this (untested)
set myStringToFind to "I want to find this phrase in the array."
tell current application's NSString to set stringToFind to stringWithString_(myStringToFind)
set theResult to theArray's containsObject_(stringToFind)
log theResult
Of course, it has to match an array’s item exactly for this to work. The previous method was a bit more flexible, ignoring case and diacriticals.
If need be, you could also sort the arrays’ content before, if you think that having them in a sorted order would help (either the original arrays or the results arrays). Like this:
theFilteredArray's sortUsingSelector_("compare:") --simple sort, usefull also for number sorting
theArray's sortUsingSelector_("caseInsensitiveCompare:") --more precise and natural sort
But I’m stating all this without knowing the contents of your files, what you need to match against and also what is the desired result. It could totally change, but this is only to give you some directions.
But, 120,000 files is a huge amount of data to go through. It’s not unusual if it takes time considering the amount of data and AppleScript’s nature. I don’t think you’ll be able to bring it down to 30 seconds even with methods like the ones i just showed you, but it should be faster.
Also, there is methods to load the contents of files straight into arrays and strings. Accessing the disk to get the contents of those files takes time too, maybe these methods would help:
tell current application's NSString to set theFileContents to stringWithContentsOfFile_encoding_error_("/Users/me/Desktop/theFile.txt", current application's NSUTF8StringEncoding, missing value) --Encoding should change depending on the file, but UTF8 is a good default I think
tell current application's NSArray to set theFileContentsAsArray to arrayWithContentsOfFile_("/Users/me/Desktop/theFile.plist") --of course, the file's contents has to be already in an array kind of structure like XML for this to work.
All I have posted here is untested and needs to be adapted to your app and your needs.
Let me know if this helps, and have fun!
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)