|
-
Jan 23rd, 2013, 09:07 PM
#1
Thread Starter
Addicted Member
AppleScript: separating items into categories
After at least a week, I have finally made enough progress on a script I am working on that categorizes items. It's not finished, and the code thus far is clunky and could maybe use some cleaning up, but here's my progress thus far:
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)
if (X > 2) then
set theIndex to theIndex + (length of theSubstring)
end if
copy theIndex to end of theIndexes
end repeat
return theIndexes
end indexesOf
on separateItemsIntoCategories(theContent, theCategories, categoryID, 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
set delim to ""
set delim to (item X of theCategories) & categoryID
copy delim to end of theDelims
end repeat
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
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
set expandedIndexes to {}
set expandedCategories to {}
repeat with X from 1 to count of categoryIndexes
set theIndexes to item X of categoryIndexes
repeat with I from 1 to count of theIndexes
copy item I of theIndexes to end of expandedIndexes
copy item X of theCategories & categoryID to end of expandedCategories
end repeat
end repeat
repeat with X from 1 to ((count of splitContent) - 1)
set delim to ""
if (categoryNameBeforeContent is false) then
set theString to theString & item X of splitContent
end if
repeat with I from 1 to count of expandedIndexes
log "expandedIndexes item I: " & item I of expandedIndexes
if (item I of expandedIndexes is ((length of theString) + 1)) then
set delim to item I of expandedCategories
end if
end repeat
if (categoryNameBeforeContent is true) then
log item X of splitContent
set theString to theString & delim & item X of splitContent
else
set theString to theString & delim
end if
set comparisonString to ""
repeat with Y from 1 to count of theCategories
set comparisonString to item Y of theCategories & categoryID
log "comparison string: " & comparisonString
if (comparisonString is delim) then
exit repeat
end if
end repeat
log "category is " & item Y of theCategories
log "length of theString: " & length of theString
log theString
end repeat
log X
end if
set AppleScript's text item delimiters to oldDelims
return categorizedContent
on error errMsg
log ("Error in separateItemsIntoCategories: " & errMsg)
end try
end separateItemsIntoCategories
So the script now knows which parts of the string belong under which categories. You pass along a string to be categorized for "theString," and a list of the categories for "theCategories." Now I just need it to apply that knowledge to the task at hand.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|