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

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 9, 2013, 12:36 AM   #1
moonman239
macrumors 65816
 
Join Date: Mar 2009
AppleScripts snippets thread

The purpose of this thread is for other people to post snippets of AppleScript code that other people might find useful.

Here's a snippet from a script I'm working on:
UPDATE: I just finished it. Here's the final result:
Code:
on separateItemsIntoCategories(theContent, theCategories, categoryID)
	set oldDelims to AppleScript's text item delimiters
	set theDelims to {}
	repeat with X from 1 to count of theCategories
		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 categorizedContent to rest of (every text item of theContent)
	set AppleScript's text item delimiters to oldDelims
	return categorizedContent
end separateItemsIntoCategories
This function takes a string of categorized entries and returns a list, with each entry of the list representing a separate category. categoryID refers to a string following the category name that tells the script that the information below the category header is in a different category. (Warning: The function may return a string with leading and/or trailing spaces, depending on whether there are any spaces between the items and the category names.)

For example, let's say I build a script that sorts today's meals into four categories: breakfast, lunch, dinner, and desert.

I set theContent to "Breakfast: Pancakes with syrup Lunch: A tuna fish sandwich Dinner: A salmon Dessert: Brownies"
I then set theCategories to {"breakfast","lunch","dinner","dessert"} and categoryID to ":" (since the items in each category follow a :.)
The function gives me {" Pancakes with syrup "," A tuna fish sandwich "," A salmon"," Brownies"}

It would be cool if someone could make a snippet that, if AppleScript comes across an error, will cause the script to present the error message to the developer and offer to send the whole code to a Website or something so a human or computer can look at the code and help the developer fix the issue. Perhaps, if a computer gets the code and no typos there, the computer will try to figure out what the developer wants the script to do and offer a solution.
__________________
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.

Last edited by moonman239; Jan 15, 2013 at 12:33 AM.
moonman239 is offline   0 Reply With Quote
Old Jan 15, 2013, 12:37 AM   #2
moonman239
Thread Starter
macrumors 65816
 
Join Date: Mar 2009
I just updated my snippet. It is much simpler and free from any flaws that I care about, one of which being that AppleScript split some text that I did not want it to split. Note: The snippet is not compatible with any version of the Mac OS older than 10.6, because OS versions prior to 10.6 do not support having multiple text item delimiters.
__________________
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

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
Good FREE app to take snippets of video? pkiula Mac Applications and Mac App Store 0 Jul 7, 2013 12:57 AM
App to to create a compilation of audio snippets healdy iPhone and iPod touch Apps 0 Jul 5, 2013 05:34 AM
All iPads: App to play audio snippets healdy iPad 0 Jul 5, 2013 05:32 AM
Safari webpage snippets firmansolutions Mac Applications and Mac App Store 2 Nov 4, 2012 10:40 AM

Forum Jump

All times are GMT -5. The time now is 02:08 AM.

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

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