I have a string of items that I need to separate according to their respective categories.
Here's my code so far:
It's a huge block of code, but I figured I'd post everything that might be relevant.
Here's the problem I'm having. After calling "separateItemsIntoCategories", AppleScript tells me it "can't get item 1 of {}." I can't figure out why there's a blank list. Nothing's wrong with the file, and nothing's wrong with the searchForSubstring handler, as I have tested that with different parameters.
Here's my code so far:
Code:
on searchForSubstring(theString, theSubstring)
try
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to theSubstring
set itemsOfString to every text item of theString
set indexes to {}
set theIndex to 0
repeat with X from 1 to ((count of itemsOfString) - 1)
set theIndex to theIndex + (length of item X of itemsOfString) + 1
copy theIndex to end of indexes
end repeat
set AppleScript's text item delimiters to oldDelims
return indexes
on error errMsg
log errMsg
end try
end searchForSubstring
to switchText from t to r instead of s
set d to text item delimiters
set text item delimiters to s
set t to t's text items
set text item delimiters to r
tell t to set t to item 1 & ({""} & rest)
set text item delimiters to d
t
end switchText
on separateItemsIntoCategories(theContent, theCategories, categoryID)
set newContent to theCategories
log (count of theCategories)
repeat with X from 1 to (count of theCategories)
log X
set categoryDelimiters to (item X of theCategories) & categoryID
set categoryIndex to item 1 of searchForSubstring(theContent, categoryDelimiters)
set newContent to switchText from newContent to "ijkl" instead of categoryIndex
end repeat
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "ijkl"
return every text item of newContent
end separateItemsIntoCategories
try
set theCourses to {"Appetizers", "Breakfast", "Entrees", "Soups and Salads", "Desserts"}
set theCuisines to {"American", "Mediterranean", "Mexican", "Asian"}
set thefile to POSIX file "/Users/Montana/Desktop/recipes.txt"
set fref to (open for access thefile)
set theContent to read fref
close access fref
set contentByCourses to separateItemsIntoCategories(theContent, theCourses, ":")
on error msg
log msg
end try
It's a huge block of code, but I figured I'd post everything that might be relevant.
Here's the problem I'm having. After calling "separateItemsIntoCategories", AppleScript tells me it "can't get item 1 of {}." I can't figure out why there's a blank list. Nothing's wrong with the file, and nothing's wrong with the searchForSubstring handler, as I have tested that with different parameters.