Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old Jan 13, 2013, 01:59 AM   #1
moonman239
macrumors 6502a
 
Join Date: Mar 2009
Trying to separate items into categories, but can't.

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

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.
__________________
The funny thing about this signature is that by the time you get to the end of it, you will have spent between five and ten seconds doing so.
moonman239 is offline   0 Reply With Quote

 
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 10:20 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC