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

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 22, 2013, 12:44 PM   #1
moonman239
macrumors 65816
 
Join Date: Mar 2009
separateItemsIntoCategories returns blank list

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
__________________
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
Old Jan 22, 2013, 07:42 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
The simplest reason your script isn't working is that you're not actually changing the categorizedContent list, which is returned from the separateItemsIntoCategories handler.

If you add some well-placed log statements, and spend a little time breaking that handler down into observable sub-sequences, the ongoing emptiness of categorizedContent will become obvious. Or you can do what I did, which was search for the uses of categorizedContent, see only two of them, and conclude that nothing useful was happening to categorizedContent.

If you see puzzling results from a log statement while debugging, post the output, along with the current version of the code. You have several bugs or inscrutable code sections, so if you change something you need to show what you changed.

Here's one example of inscrutable code:
Code:
			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
What is this code expected to do? How would you confirm it's actually doing what's expected of it?


You should also tell us exactly what you expect the result of separateItemsIntoCategories() to be. Because some code doesn't make sense, and some code simply isn't working, it's difficult to guess exactly what you expect the separated output to be.

If I knew what the expected output was, I could better guess what the appropriate code to produce that output should be. Since you only gave the broken code, and no explanation or comments in the code, it's a lot harder to figure out where the code is actually wrong.
chown33 is offline   0 Reply With Quote

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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
How to insert a numbered list or bullet list in Note.app on iPad? sza iPad Apps 1 Feb 12, 2013 05:07 PM
List+, a simple todo list + reminder app (promotion codes) bnfan iPhone and iPod touch Apps 12 Dec 24, 2012 03:02 PM
In iMessage , contact sends me a texts that is blank and only shows the blank bubble. Jooohhn243 iPhone Tips, Help and Troubleshooting 0 Dec 3, 2012 08:56 PM
List of Apple Stores Accepting iPad 3 Returns Medic311 iPad 27 Nov 2, 2012 06:12 PM
List Buddy 2.4: The best list making app receives a major update Holosynthetic iPhone and iPod touch Apps 0 Jun 13, 2012 12:19 PM

Forum Jump

All times are GMT -5. The time now is 04:29 PM.

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

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