Running fine now, down to 15 seconds, from 28. I donβt think my clients will mind waiting about a minute for searching i million + items.
use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property mailManagerDesktopFolderPath : ""
property searchString : ""
property theFoundList : {}
property paragraphCount : 0
set my searchString to "ozsanta" as text
set my mailManagerDesktopFolderPath to ((path to desktop) & "Mail Manager Folder" as text)
my gatherInfo()
on gatherInfo()
with timeout of 1000 seconds
set theStart to current date
set my paragraphCount to 0
ignoring case
tell application "Finder"
set my theFoundList to {} as list
set theDataList to (folders of folder mailManagerDesktopFolderPath whose name starts with "Mail Data") as list
repeat with eachFolder in theDataList
set fileList to files of eachFolder as alias list
repeat with eachFile in fileList
set theParas to ""
set theString to (current application's NSString's stringWithContentsOfFile:(POSIX path of eachFile) encoding:(current application's NSUTF8StringEncoding) |error|:(missing value))
-- make sure match string is regex "safe"
set thePattern to (current application's NSRegularExpression's escapedPatternForString:(my searchString)) as text
-- put a unique character at start of matching lines
try
set theString to (theString's stringByReplacingOccurrencesOfString:(".*" & thePattern & ".*") withString:"π$0" options:(current application's NSRegularExpressionSearch) range:{0, theString's |length|()})
-- delete all lines that don't start with the unique character
set theString to (theString's stringByReplacingOccurrencesOfString:"(?m)^[^π].*(\\n|\\r)*" withString:"" options:(current application's NSRegularExpressionSearch) range:{0, theString's |length|()})
-- delete the unique character at the beginning of lines
set theString to (theString's stringByReplacingOccurrencesOfString:"(?m)^π" withString:"" options:(current application's NSRegularExpressionSearch) range:{0, theString's |length|()})
-- strip trailing returns/linefeeds
set theString to (theString's stringByTrimmingCharactersInSet:(current application's NSCharacterSet's whitespaceAndNewlineCharacterSet()))
-- convert to paras of AS text
set theParas to paragraphs of (theString as text)
end try
if theParas β "" then
repeat with eachPara in theParas
set end of my theFoundList to eachPara as list
end repeat
end if
end repeat
end repeat
end tell
end ignoring
tell application "System Events" to display dialog (current date) - theStart & " seconds to process" & return & return & (count of my theFoundList) & " matches" as text giving up after 600
set temp to item 1 of my theFoundList as text
set {tild, my text item delimiters} to {AppleScript's text item delimiters, ","}
set storedReference to text item 1 of temp as text
set storedEmailAddress to text item 2 of temp as text
set storedEmailName to text item 3 of temp as text
set AppleScript's text item delimiters to tild
tell application "System Events" to display dialog storedReference & return & storedEmailAddress & return & storedEmailName
end timeout
end gatherInfo
And what, you ask, was the beginning of it all?
And it is this......
Existence that multiplied itself
For sheer delight of being
And plunged with numberless trillions of forms
So that it might
find
itself
innumerably
Sri Aurobindo