Happy Saturday everyone. I have another seemingly rookie question for the group: How do I find the match string for a CONTAINS clause that uses considering/ignoring?
In otherwords, if I scan a title for a search string, I’d like to know what the actual match string was, including those characters that I’ve ignored, e.g.:
set companyID to “jonesinc”
set title to “Welcome to Jones Inc Manufacturing”
considering application responses but ignoring case, punctuation, white space, diacriticals and hyphens
if (title contains companyID) then
– we did find the ID in the title
beep
– now set fullCompanyID to the match string (e.g. “Jones Inc”)
end if
end considering
I found lots of search and replace Applescript routines (thanks everyone), but couldn’t find this variation, which leads me to believe it’s one of those Fundamental Applescript Concepts that I slept through in school.
FYI, this is the kludge that I came up with… massively inelegant, but if this be the only way, just say so and I’ll consider this code a win. I can already see ways that this is going to blow up, but this is an assitance routine to help the user, so it need not fire perfectly every time as long as it fires correctly most of the time.
on findFuzzy(fullString, searchString)
– finds matches in a string, regardless of spaces, punctuation, etc.
local tempMatchString, matchString
set matchString to ""
considering application responses but ignoring case, punctuation, white space, diacriticals and hyphens
if (fullString contains searchString) then
-- we did find the searchString inside the fullString
set titleWords to (words of fullString)
set startFound to false
set endFound to false
set tempMatchString to ""
repeat with theWord in titleWords
-- does searchString start with theWord?
if (searchString begins with theWord) then
-- we found the first word, add it to the final match string
copy (theWord as string) to tempMatchString
set startFound to true
-- is it a single word match?
if (searchString is equal to tempMatchString) then
set endFound to true
exit repeat
end if
else if (searchString ends with theWord) then
-- we found the first word, add it to the final match string and exit
copy tempMatchString & " " & (theWord as string) to tempMatchString
set endFound to true
exit repeat
else if (searchString contains theWord) then
if (startFound) then
-- we found the first word, but not yet the final word, add it to the final match string
copy tempMatchString & " " & (theWord as string) to tempMatchString
end if
else if (searchString does not contain theWord) then
-- current word is not in the searchString, reset the tempMatchString (every word must be included)
set tempMatchString to ""
set startFound to false
end if
end repeat
if (startFound and endFound) then
-- we found both the beginning and end of the string
set matchString to tempMatchString
end if
end if
end considering
return matchString