Go Back   The macosxhints Forums > OS X Help Requests > AppleScript



Reply
 
Thread Tools Rate Thread Display Modes
Old 01-22-2013, 04:46 PM   #1
moonman239
Registered User
 
Join Date: Jan 2013
Posts: 2
Separating items into categories

I have this script. So far, separateItemsIntoCategories is supposed to rebuild the string I give it, just so it knows when it's hit the category identifier, which is a string that goes before or after the category name, so the script knows where to split the string.

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
moonman239 is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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:31 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.