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

Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old Jan 8, 2013, 11:36 PM   #1
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:
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 14, 2013 at 11:33 PM.
moonman239 is offline   0 Reply With Quote

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 6, 2013 11:57 PM
App to to create a compilation of audio snippets healdy iPhone and iPod touch Apps 0 Jul 5, 2013 04:34 AM
All iPads: App to play audio snippets healdy iPad 0 Jul 5, 2013 04:32 AM
Safari webpage snippets firmansolutions Mac Applications and Mac App Store 2 Nov 4, 2012 09:40 AM

Forum Jump

All times are GMT -5. The time now is 05:41 AM.

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

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