separateItemsIntoCategories returns blank list

Discussion in 'Mac Programming' started by moonman239, Jan 22, 2013.

  1. macrumors 65816

    Joined:
    Mar 27, 2009
    #1
    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
    
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    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.
     

Share This Page