How would I take a txt file, open it as a list where each item is a line of the file, write a new item into that list, alphabetise the list, then write it back to the file? The text file looks like:
I set the file to:
set theFile to path to resource "highscores.txt"
And I want to use this code to alphabetize the list:
set the_list to {"c", "b", "e", "a", "d"}
set old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {ASCII character 10} -- always a linefeed
set list_string to (the_list as string)
set new_string to do shell script "echo " & quoted form of list_string & " | sort -f"
set new_list to (paragraphs of new_string)
set AppleScript's text item delimiters to old_delims
return new_list
How would I do this?!?! Please help.
Model: iBook G4
AppleScript: 2.1
Browser: Safari 533.19.4
Operating System: Mac OS X (10.4)
set pathToHighscores to ((path to desktop as text) & "highscores.txt")
repeat
set newListItem to text returned of (display dialog "Enter an integer" default answer "")
try
newListItem as integer
exit repeat
on error
display dialog "I said 'Integer'" buttons {"Cancel", "Try Again"} default button 2
end try
end repeat
try
set dataStream to open for access file pathToHighscores with write permission
if (get eof of dataStream) > 0 then
set scoreList to read dataStream as list
set end of scoreList to newListItem
bubblesort(scoreList)
else
set scoreList to {newListItem}
end if
write scoreList as list to dataStream starting at 0
close access dataStream
on error
try
close file pathToHighscores
end try
end try
on bubblesort(theList)
script o
property lst : theList
end script
repeat with i from (count theList) to 2 by -1
set a to beginning of o's lst
repeat with j from 2 to i
set b to item j of o's lst
if ((a as integer) > (b as integer)) then
set item (j - 1) of o's lst to b
set item j of o's lst to a
else
set a to b
end if
end repeat
end repeat
end bubblesort
The text file ends up saying “listutxt(number here)utxt(number here)utxt(number here)utxt(number here)” etc. where (number here) is a number… Is this on my side or is it a bug?
Any property declarations need to be at the top of the script, before real business begins.
Otherwise, just switch to:
set strPath to POSIX path of (path to resource "highscores.txt")
tell (display dialog "Enter new high score" default answer "")
set strNum to (text returned as integer) as string
end tell
do shell script "echo " & strNum & " >> " & strPath & "; sort -n " & strPath & " -o " & strPath
Now I have yet ANOTHER (somewhat related) problem! I try to do:
set pathToHighscores to ((path to desktop as text) & "highscores.txt")
open for access pathToHighscores
set prefsContents to read pathToHighscores
close access pathToHighscores
set txtvar1 to paragraph 1 of prefsContents
return txtvar1
However, it says “Could not make ‘Macintosh HD:Users:user:Desktop:highscores.txt’ into type file.” What am I doing now…?
If you want to read only the contents of the file, open and close for access is not needed
set pathToHighscores to ((path to desktop as text) & "highscores.txt")
set prefsContents to read file pathToHighscores
set txtvar1 to paragraph 1 of prefsContents
return txtvar1
set pathToHighscores to ((path to desktop as text) & "highscores.txt")
set prefsContents to read file pathToHighscores
set txtvar1 to paragraph 1 of prefsContents
return txtvar1
The end-of-file error also occurs when you try to read a file that’s already been read or written through to the end and you haven’t closed it or reset the file mark, or when you’ve written data to a file in one format and are trying to read them back in another.
The ‘read’ command in your post (#12) reads the data as 8-bit text characters, which won’t cause the error you’re getting, even if the data are a different kind. So the likelihood is that the file’s still open from one of your previous errors.
If you’ve been running the script(s) in AppleScript Editor, save your work and then quit AppleScript editor. That’ll close any files opened by AppleScript Editor which have been left open. Reopen AppleScript Editor and try running the script again. If it still doesn’t work, tell us definitively what script you’re using to write the data to the file.