Mac Sort a text file in Applescript

SomeoneElse

macrumors newbie
Original poster
Oct 22, 2009
5
0
I have a problem with formatting a text file that can be read into an Applescript variable. Below is my script. When I try to sort the file the format is off somehow and it gives an error. If I enter the data locally the script works fine. Quoted out line above the list is reference to the file names.txt of the desktop
Can anyone tell me what's wrong here? I don't want to have to use database events as I am running this script on pre OSX 10.4 machines.

Thanks
-SomeoneElse



set namez to text returned of (display dialog "What's your name?" default answer "names")
-- gets username and uses it as filename
set theFilePath to (path to desktop) & namez & ".txt" as string
set quote to ASCII character of 34
set cr to ASCII character of 13
--set the_data to read file theFilePath
set the_data to {"Mike", "Rita", "frank", "fred", "frodo", "fritz", "Ed", "Dave"}
set the_list to the_data
repeat (length of the_list) times
repeat with j from 1 to (length of the_list) - 1
if item j of the_list > item (j + 1) of the_list then
set {item j of the_list, item (j + 1) of the_list} to {item (j + 1) of the_list, item j of the_list}
end if
end repeat
end repeat
the_list
 

chown33

Moderator
Staff member
Aug 9, 2009
8,652
4,894
vertical
Assuming your sort algorithm works with the builtin data, it suggests the problem lies in the code that reads the file into a list, or the text of the file being read.

Unfortunately, you haven't shown any actual code that reads a file into a list, nor have you shown what text is in the file. You also haven't said what error message appears, if any. "It gives an error" is too vague.

Remember, we can't look in your files, we can't see code that you removed, and we can't see your screen. If you need something to run the program, such as an input file or a file-reading piece of code, then we need those same things, too. You have to give us enough that we can run the actual program and see the actual failure.
 

SomeoneElse

macrumors newbie
Original poster
Oct 22, 2009
5
0
Script format and contents of the data string file

I thought you might ask here is the format script and string is commented out in the previous script. as it appears on the file :

"dave","buster","fred",


-- Add Content to file
set namez to text returned of (display dialog "What's your name?" default answer "")

repeat 3 times
set theFilePath to (path to desktop) & namez & ".txt" as string
set quote to ASCII character of 34

set cr to ASCII character of 13
set TheText to text returned of (display dialog "Please enter some animal." default answer "")
set theFileReference to open for access theFilePath with write permission

set the_format to "" & quote & TheText & quote & ","
write (the_format) to theFileReference starting at eof
close access theFileReference

end repeat

set the_data to read file theFilePath as string

set the_list to the_data
repeat (length of the_list) times
repeat with j from 1 to (length of the_list) - 1
if item j of the_list > item (j + 1) of the_list then
set {item j of the_list, item (j + 1) of the_list} to {item (j + 1) of the_list, item j of the_list}
end if
end repeat
end repeat
the_list





Assuming your sort algorithm works with the builtin data, it suggests the problem lies in the code that reads the file into a list, or the text of the file being read.

Unfortunately, you haven't shown any actual code that reads a file into a list, nor have you shown what text is in the file. You also haven't said what error message appears, if any. "It gives an error" is too vague.

Remember, we can't look in your files, we can't see code that you removed, and we can't see your screen. If you need something to run the program, such as an input file or a file-reading piece of code, then we need those same things, too. You have to give us enough that we can run the actual program and see the actual failure.
 

SomeoneElse

macrumors newbie
Original poster
Oct 22, 2009
5
0
BTW, "quote" is an AppleScript property which by default is set at Ascii 34, so this line isn't needed.

mt
Well, That is true of Mac OSX 10.4 and greater but I wrote this on a 10.3.9 system and that & quote is required. I still haven't been able to figure out the whole "\ variable "\ thing yet. I'm trying though. Thanks.

-SomeoneElse
 

SomeoneElse

macrumors newbie
Original poster
Oct 22, 2009
5
0
The solution

I have figured it out. Thanks for the help. The problem was I needed to make Set the_list to Every Word of the_data. not excluding the Every word part.
here is the complete script.




set namez to text returned of (display dialog "What's your name?" default answer "names")

set theFilePath to (path to desktop) & namez & ".txt" as string
set quote to ASCII character of 34
set cr to ASCII character of 13
set the_data to read file theFilePath
--set the_data to {"Mike", "Rita", "frank", "fred", "frodo", "fritz", "Ed", "Dave"}
set the_list to every word of the_data
repeat (length of the_list) times
repeat with j from 1 to (length of the_list) - 1
if item j of the_list > item (j + 1) of the_list then
set {item j of the_list, item (j + 1) of the_list} to {item (j + 1) of the_list, item j of the_list}
end if
end repeat
end repeat
the_list
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.