set the_file to (((path to desktop) as string) & "test.txt") as file specification
set record_1 to POSIX path of {choose folder with prompt "Please locate the apache2 folder."}
set the_data to {record_1}
my write_to_file(the_file, the_data)
set read_data to read the_file as list
on write_to_file(the_file, the_data)
try
open for access the_file with write permission
set eof of the_file to 0
write (the_data) to the_file starting at eof as list
close access the_file
on error
try
close access the_file
end try
end try
end write_to_file
This is to write it, but there’s an issue with it. I want only the path to be written.
But then i want a script to read that file. (to know thw path of something.)
I assume that you want more than one item in the list.
set the_file to (((path to desktop) as string) & "test.txt") as file specification
set record_1 to POSIX path of {choose folder with prompt "Please locate the apache2 folder."}
set the_data to {record_1, "Some other text", "A third chunk of text"}
my write_to_file(the_file, the_data)
set read_data to read the_file as list
display dialog (item 1 of read_data) & return & (item 2 of read_data) & return & (item 3 of read_data)
on write_to_file(the_file, the_data)
try
open for access the_file with write permission
set eof of the_file to 0
write the_data as list to the_file starting at eof
close access the_file
on error
try
close access the_file
end try
end try
end write_to_file
It still has text in front of the text and i don’t need the text afterwards. i need applescript to read the path of where somethings located so that it can do multiple command without havin to type in the path, instead a variable.
The path is there, but you need to use the quoted form of it to avoid problems with spaces in folder/file names.
set the_file to (((path to desktop) as string) & "test.txt") as file specification
set record_1 to POSIX path of {choose folder with prompt "Please locate the apache2 folder."}
set the_data to {record_1, "Some other text", "A third chunk of text"}
my write_to_file(the_file, the_data)
set read_data to read the_file as list
display dialog (item 1 of read_data) & return & (item 2 of read_data) & return & (item 3 of read_data)
set the_path to quoted form of (item 1 of read_data)
display dialog the_path
on write_to_file(the_file, the_data)
try
open for access the_file with write permission
set eof of the_file to 0
write the_data as list to the_file starting at eof
close access the_file
on error
try
close access the_file
end try
end try
end write_to_file
http://ebaursolutions.info/test.txt
Here’s an example of what i’m talking about.
I can get rid of the junk you added “Some other text”, “A third chunk of text” but the part infront of the location, that’s the part i need to get rid of. you know how to read files?
That’s part of the list. If you don’t want them there, don’t save as a list, save as text.
set the_file to (((path to desktop) as string) & "test.txt") as file specification
set record_1 to POSIX path of {choose folder with prompt "Please locate the apache2 folder."}
set the_data to {record_1, "Some other text", "A third chunk of text"}
set the_text to (item 1 of the_data)
repeat with i from 2 to (count of the_data)
set the_text to the_text & return & (item i of the_data)
end repeat
my write_to_file(the_file, the_text)
set read_data to read the_file as text
display dialog read_data
on write_to_file(the_file, the_data)
try
open for access the_file with write permission
set eof of the_file to 0
write the_data to the_file starting at eof
close access the_file
on error
try
close access the_file
end try
end try
end write_to_file
A POSIX path (or any text in Leopard) is Unicode text and will be written to the file as such unless you specify otherwise. To read it back properly, you need to read the file ‘as Unicode text’:
set the_file to ((path to desktop as string) & "test.txt") as file specification
set record_1 to POSIX path of (choose folder with prompt "Please locate the apache2 folder.") -- A POSIX path is Unicode text.
write_to_file(the_file, record_1)
set read_data to read the_file as Unicode text -- Interpret the file contents as Unicode text.
on write_to_file(the_file, the_data)
try
set file_ref to (open for access the_file with write permission)
set eof file_ref to 0
write (the_data) to file_ref -- Write the_data as presented.
close access file_ref
on error
try
close access the_file
end try
end try
end write_to_file
I hope that’s what’s wanted. The query isn’t at all clear.
That’s the effect you get when you write a list to a file, but read it back as text (or without specifying how it’s to be read). I can’t see off-hand which of the above scripts would get that result.
I don’t understand. I’ve given you two versions of the script: one that writes and then reads a list to and from the file, and another that writes and then reads text to and from the file.
Actually, cwtnospam, your “text” script writes Unicode text to the file (because that’s what the POSIX path is) but reads it back ‘as text’. Even in Leopard, it has to be read ‘as Unicode text’, because the File Read/Write commands still differentiate between ‘text’ (ie. ‘string’) and ‘Unicode text’.
It’s possible that slacker doesn’t actually mean to write a list to the file, even though his posted script goes to some length to do so and to read it back as such. (He did say “I want only the path to be written.”) I notice that his ‘choose folder’ command is in braces too. Maybe he doesn’t realise that these aren’t just another kind of bracket. This is the possibility I was trying to work round above in my version of the script.