Results 1 to 2 of 2
  1. #1
    AppleScript - I get a {} when trying to separate items into categories.

    Member Since
    May 29, 2009
    Posts
    2
    AppleScript - I get a {} when trying to separate items into categories.
    I'm just going to copy and paste my posts from the MacRumors Mac development forum to save me time.

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

    Here's my code so far:

    Code:
    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 file looks something like this:

    Appetizers:

    (A bunch of recipes here)

    Breakfast:

    (Another set of recipes here)

    etc.

    The script should give me something like this:
    {"(All the appetizer recipes)","(All the breakfast recipes)",...}

  2. #2
    AppleScript - I get a {} when trying to separate items into categories.
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    I see you resolved your problem here.

    Trying to separate items into categories, but can't. - MacRumors Forums

    Well Done!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. iCal edit/ modify Todo items with applescript
    By wild4life in forum OS X - Operating System
    Replies: 1
    Last Post: 09-08-2013, 10:24 AM
  2. Replies: 1
    Last Post: 09-05-2012, 11:37 AM
  3. Question about Numbers categories and sub-categories
    By Lindamood in forum OS X - Apps and Games
    Replies: 1
    Last Post: 07-05-2011, 09:29 PM
  4. Categories
    By schweb in forum OS X - Operating System
    Replies: 7
    Last Post: 03-05-2006, 05:06 PM
  5. New Categories
    By JAdams in forum Community Suggestions and Feedback
    Replies: 1
    Last Post: 11-27-2005, 01:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •