How to compile a list from a list in text edit with returns?

I’ll be honest, I know there are lots of posts on sorting alphabetically. But I looked at many and still can’t seem to make it work for me.

I am trying to take a text edit document that contains a bunch of lines separated by a return, and eventually sort it alphabetically based on it’s first character. For example:

badad.2c

goodad.2c

TransferSuccessful

UploadHomePage.png

VGraphicsFontLogo.eps

VBBSwoosh.4C

VLogo_4C.eps

Map_DD.pdf

Header.ai

Logo_WT.eps

HeaderJustBlend.ai

I realize I have to create a list first, and then sort. But this is where my problem lies. I don’t understand text delimiters well enough to strip the returns and compile the list from the text edit document. If I could, I assume I could maybe use the script below to return the sorted list. So I suppose my primary question is how to make a list from a list in text edit? Can anybody give me a hand so I can finally grasp the concept of text item delimiters and compiling lists? (I would post what I tried doing, but I think it would only make matters worse) :frowning:

on ASCII_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 ASCII_Sort
tell application "Finder"
	set mylist to {}
	set x to read file "Macintosh HD:Users:username:path:to:textfile.txt"
	set n to count of paragraphs in x
	repeat with i from 1 to n
		if paragraph i of x is not "" then
			set end of mylist to paragraph i of x
		end if
	end repeat
end tell
mylist

This is fantastic!!! :slight_smile:

That if statement:
if paragraph i of x is not “”
was the piece of the puzzle I needed.

This is working like a charm. Many thanks once again.

-Jeff

Every time you use an unnecessary repeat loop or consideration, God kills a kitten. :wink:

tell application "Finder" to set theFile to (read (choose file))
set mylist to theFile's paragraphs

Hey, Jeff
TextEdit has pretty poor AppleScript implementation. Are you just using it as a simple text editor, or for rich text formatting? If the former, might I recommend downloading the free TextWrangler editor from Bare Bones? IMHO, it’s a lot easier to code than TE, and has a built in sorting feature for any quick and dirty sorting”outside of a pure AppleScript solution”that you might need.

That also applies to unnecessary “tell Finder” blocks. :mad:

Also, I think the point is that Jeffkr wants to remove the empty paragraphs from the text.

Aye, so it does, Bruce, mea culpa. Poor kitty. :rolleyes:

My obligatory shell-based alternative:

tell application "TextEdit"
	set inputPath to path of front document
	
	try
		-- inputPath is not set if the document hasn't been saved
		set inputPath to quoted form of POSIX path of inputPath
	on error
		display alert "The front document has not been saved." message "You need to save the front document before using this script." buttons {"Cancel"} cancel button 1
	end try
end tell

do shell script "/usr/bin/grep --invert-match '^$' " & inputPath & ¬
	" | /usr/bin/sort --ignore-case"

LOL, You do realize that if this were true, there would be no more cats in the world”my scripts are a mess and full of unnecessary code.

I just downloaded TextWrangler”thank you for the suggestion. Quite honestly, this is the first time I needed to use TextEdit to work in conjunction with a script. But who’s to say this will not change if I ever learn and dabble more with scripting.

As for the shell script. It seems to error on the ( --ignore-case ). – I obviously know nothing about shell scripting, but removing that part seems to work well. FWIW, it is a great short alternative to use for sorting.

Thanks for all of your advice and help.

-Jeff

Here’s a short tutorial for you. :smiley:

-- to change a string to a list you get the text items of the string
-- to change a list to a string you coerce it using "as string" (or as text or as unicode text etc.)
-- the character that determines how the string is broken up (or put back together) is the value of applescript's text item delimiters (tids)
-- by default tids are set to ""

-- example
set theString to "regulus6633"
set theList to text items of theString
--> {"r", "e", "g", "u", "l", "u", "s", "6", "6", "3", "3"}
-- notice the default tids value "" is used to separate the string into a list

-- you can change it back by coercing the list to a string, and again it's based on tids
set theString to theList as string
--> "regulus6633"

-- now suppose we wanted to use something other than "" as the separation value
-- so we first change the tids to something else, then get text items
-- you must always change tids back to default values because other parts of a script may depend on it
set AppleScript's text item delimiters to "gu"
set theList to text items of theString
set AppleScript's text item delimiters to "" -- we're done with tids so we reset them to ""
theList
--> {"re", "lus6633"}
-- notice that the tids value "gu" was removed from the original string and used as the separator

-- again we can put that list back into a string
set AppleScript's text item delimiters to "gu"
set theString to theList as string
set AppleScript's text item delimiters to ""
theString
--> "regulus6633"
-- notice how when we coerced it back to a string the tids were inserted back into the string
-- so between every list value the tids are inserted when you coerce it into a string

-- this removing and inserting of the tids is handy
-- for example it makes a nice way to have a find/replace function
-- because you can remove a value and then replace it with something else
set AppleScript's text item delimiters to "66"
set theList to text items of theString -- remove the 66
set AppleScript's text item delimiters to "33"
set theString to theList as string -- replace it with 33
set AppleScript's text item delimiters to ""
theString
--> "regulus3333"

(* so that's the basic way to go from string->list->string and the role of tids *)
(* I hope that clears it up for you *)

You all are the best!
Text Item delimiters were making sense. but now I have seen a whole new light on additional functions Tids can do. Thank you so much regulus6633 for taking the time to post this excellent tutorial. I bought two books on AppleScript, and neither explained as much as you just did in one post.

Thank you,
-Jeff

:wink: No problem. It took me awhile to grasp it too. The relationship between lists, strings, and tids is easy once you figure it out. I just learned by trial-and-error myself (and lots of help from people on this board) so I’m glad I could save you some of that pain.