Quick Alphabetical

Here is a very quick little script to enable ultra-fast alphabetical sorts of text-lists, Stickies or whatever. As stated below, it is recommended as a keyboard shortcut service. One simply copies the list to be sorted, and then engages the script. Once done, a dialog displays the finished sort, which is automatically put on the clipboard - that can next be pasted right-over the (theoretically still selected list) on the original document.

Note: Special-Paste may be required to maintain original style/formatting, as the script strips these away to sort.


----
-- "QuickSort" by Adam Albrec (game_creator@hotmail.com)
-- An Applescript to allow the copying and alphabetizing of lists (Recommended to be used as a Hotkey Service).
----

----
-- Handlers.
----

-- Simple Sort Handler (from http://www.macosxautomation.com/applescript/sbrt/sbrt-05.html)
on simple_sort(my_list)
	set the index_list to {}
	set the sorted_list to {}
	repeat (the number of items in my_list) times
		set the low_item to ""
		repeat with i from 1 to (number of items in my_list)
			if i is not in the index_list then
				set this_item to item i of my_list as text
				if the low_item is "" then
					set the low_item to this_item
					set the low_item_index to i
				else if this_item comes before the low_item then
					set the low_item to this_item
					set the low_item_index to i
				end if
			end if
		end repeat
		set the end of sorted_list to the low_item
		set the end of the index_list to the low_item_index
	end repeat
	return the sorted_list
end simple_sort

-- Replace Chars Handler (from http://www.macosxautomation.com/applescript/sbrt/sbrt-05.html)
on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

----
-- Script.
----

-- Prevents timeout if user walks away during long sort.
with timeout of 86400 seconds
	
	
	-- Brings in text list from clipboard. and removes extra line-breaks if present.
	set incomingList to (the clipboard) as text
	set the incomingList to replace_chars(incomingList, "

", "")
	set incomingList to the paragraphs of incomingList as list
	set linebreakCheck to the number of items in incomingList
	set linebreakCheckRun to "1"
	set newBreakFreeList to {}
	repeat linebreakCheck times
		if item linebreakCheckRun of incomingList is not "" then
			set newBreakFreeList's end to item linebreakCheckRun of incomingList
		end if
		set linebreakCheckRun to linebreakCheckRun + 1
	end repeat
	set incomingList to newBreakFreeList
	
	
	-- Sorts items into new alphabetized list and copies it to the clipboard to be pasted in the original's place.  Brings up dialog to show completion.
	set the message_text to (simple_sort(the incomingList))
	set the message_text to replace_chars(message_text, ", ", "
")
	beep
	set the clipboard to message_text as text
	display dialog "                   Everything is Sorted-Out!

" & message_text
	

end timeout

----
-- End of Script.
----