Here's my code. So far, it's basically supposed to rebuild the original string. I can't simply use the original string, because the script has to figure out which part of the string should go under which category.
Code:
on indexesOf(theSubstring, theString)
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to theSubstring
set stringList to every text item of theString
set AppleScript's text item delimiters to oldDelims
set theIndex to 1
set theIndexes to {}
repeat with X from 2 to count of stringList
set theIndex to theIndex + (length of item (X - 1) of stringList)
copy theIndex to end of theIndexes
end repeat
return theIndexes
end indexesOf
on separateItemsIntoCategories(theContent, theCategories, categoryID, categoryIDPrecedesName, contentOrganized, splitStringDelimiter)
try
set oldDelims to AppleScript's text item delimiters
-- Separate the items
set theDelims to {}
repeat with X from 1 to count of theCategories
if (categoryIDPrecedesName is false) then
set delim to (item X of theCategories) & categoryID
else
set delim to categoryID & (item X of theCategories)
end if
copy delim to end of theDelims
end repeat
log theDelims
set AppleScript's text item delimiters to theDelims
set splitContent to every text item of theContent
set currentIndex to 1
set categorizedContent to {}
set categoryNames to {}
set categoryContent to {}
if (contentOrganized is false) then
-- Content is not organized.
set theString to ""
set categoryNameBeforeContent to true
try
repeat with X from 1 to length of splitContent
get item X of theDelims
end repeat
on error errMsg
set categoryNameBeforeContent to false
end try
log categoryNameBeforeContent
set categoryIndexes to {}
repeat with X from 1 to length of theCategories
set theIndexes to indexesOf(item X of theCategories, theContent)
copy theIndexes to end of categoryIndexes
copy item X of theCategories to end of categoryNames
end repeat
repeat with X from 1 to count of splitContent
repeat with I from 1 to count of theCategories
if (item I of categoryIndexes contains length of theString) then
set delim to item I of theCategories & categoryID
exit repeat
end if
end repeat
log delim
if (categoryNameBeforeContent is true) then
set theString to theString & delim & item X of splitContent
else
set theString to theString & item X of splitContent & delim
log theString
end if
end repeat
end if
set AppleScript's text item delimiters to oldDelims
return categorizedContent
on error errMsg
log ("Error in separateItemsIntoCategories: " & errMsg)
end try
end separateItemsIntoCategories
on replaceText(find, replace, subject)
set originalString to subject
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
set oldDelims to AppleScript's text item delimiters
try
set testString to "Hi, John: How are you, Mary: Good, John: Hello, Gunter:"
set test to separateItemsIntoCategories(testString, {"John", "Mary", "Gunter"}, ":", false, false, "")
on error msg
log msg
end try