Working with ideas from red_menace and using a search routine in Applescript I found at http://applescript.bratis-lover.net/library/string/ I cobbled together this as a test bed for you to try out some ideas.
Basically the important parts are on searchText(itemToSearchFor, stringToSearch,colorToSet) and on findAll(str, findString)
you call searchText with the string to search for, the string to search and the color (as an NSColor) you want the matches colored. It returns an attributed string with the matches colored. The search is case sensitve but that could be easily changed.
If no match is found the original string is returned.
This test bed has tge sting to search hard coded in. You enter the search term in a text filed in the applications window.
Thank's to red_menace for pointing out the attributed string. This wasn't as bad to do as I first thought (finding the search rountine really helped speed things up) and adding it to your code should not require to many changes.
regards
Complete project at:
https://www.dropbox.com/sh/2oolboxo1ckhyut/AAALgW5dwh2ji1BcBiwCQ-6Sa
--
-- TMAppDelegate.applescript
-- testMAt
--
-- Created by Frank Caggiano on 7/17/14.
-- Copyright (c) 2014 Frank Caggiano. All rights reserved.
--
script TMAppDelegate
propertyparent : class"NSObject"
-- IBOutlets
property window : missing value
property searchField : missing value
property textField : missing value
property noMatch : missing value
property ss : "04/07/2014 Mrs Brenda Bird 27 Somewhere Street Somewhere Town"
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
textField's setStringValue:ss
noMatch's setHidden:true
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
on applicationShouldTerminateAfterLastWindowClosed_(sender)
returntrue
end applicationShouldTerminateAfterLastWindowClosed_
on buttonClicked_(sender)
noMatch's setHidden:true
set searchForItem to searchField's stringValue() as text
set displayString to searchText(searchForItem, ss, current application's NSColor's redColor)
textField's setAttributedStringValue:displayString
end buttonClicked_
on searchText(itemToSearchFor, stringToSearch,colorToSet)
set lenOfSearch to count of itemToSearchFor
--set searchItem to "04/07/2014 Mrs Brenda Bird 27 Somewhere Street Somewhere Town"
set foundPositions to findAll(stringToSearch, itemToSearchFor)
if foundPositions is {} then
noMatch's setHidden:false
return stringToSearch
end if
set attributedSearchItem to current application's NSMutableAttributedString's alloc's initWithString:stringToSearch
repeat with i in foundPositions
set attributeRange to {i - 1 , lenOfSearch}
attributedSearchItem's addAttribute:(current application's NSForegroundColorAttributeName) value: colorToSet range:attributeRange
end
return attributedSearchItem
end searchText_
--
-- Wonderful search rountine found at the URL listed
--
on findAll(str, findString)
-- ljr (http://applescript.bratis-lover.net/library/string/)
local ASTID, str, findString, tmp, len, len2, pos, res
set ASTID to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to findString
if str does not contain findString then return {}
consideringcase
script k
property res : {}
property tmp : str's text items
endscript
endconsidering
set len to count k's tmp
set len2 to count findString
set pos to 0
repeatwith i from1to len - 1
set thisPos to (count k's tmp's item i)
set thisPos to thisPos + pos
set pos to thisPos + len2
set end of k's res to (thisPos + 1)
endrepeat
set AppleScript's text item delimiters to ASTID
return k's res
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't findAll: " & eMsg number eNum
end try
end findAll
endscript