AppleScript - I get a {} when trying to separate items into categories.

Joined
May 29, 2009
Messages
2
Reaction score
0
Points
1
I'm just going to copy and paste my posts from the MacRumors Mac development forum to save me time.

I have a string of items that I need to separate according to their respective categories.

Here's my code so far:

Code:
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.

The file looks something like this:

Appetizers:

(A bunch of recipes here)

Breakfast:

(Another set of recipes here)

etc.

The script should give me something like this:
{"(All the appetizer recipes)","(All the breakfast recipes)",...}
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top