echo problems with £ sign

Hi all,
I have a script that’s echoing out the names of items in a folder to a text file, this part works great but for some reason if I have a filename with a £ sign in it the output text file inserts a ¬ symbol before the £ sign i.e.
myfolder name £20 becomes
myfolder name £20
Anybody have any idea why this is happening and how to prevent it?

set thefolder to choose folder --> choose a folder with a £ sign in the name
set ptdt to path to desktop as string
tell application "Finder"
	set folder_name to name of thefolder
	do shell script "echo " & quoted form of folder_name & "> " & POSIX path of (ptdt & "test_text2.txt")
end tell

You are seeing a text encoding issue.

When the name of the folder is saved by the shell script, it is saved as UTF-8. This is because that is the format that do shell script uses when generating the command for the shell. The shell just passes this along to its internal echo command and then saves that command’s output into the file. So the shell is just saving the data that do shell script provided as UTF-8.

Then whatever program you are using to view the resulting text file is not reading the file as UTF-8. It looks to me like it is reading it as MacRoman. When encoded as UTF-8, ‘£’ is 0xC2 0xA3. When encoded as MacRoman, ‘¬’ is 0xC2 and ‘£’ is 0xA3. So if the actual text is ‘£’ and it is encoded in UTF-8 but decoded as MacRoman it will come out as ‘¬£’.

You can tell TextEdit to open a file using a specific encoding in the Open. dialog (or change the default in the Preferences. Tell it that the file is UTF-8 and you will not see the “extra character”.

Or if you want the file to be encoded in something other than UTF-8, you can use a tool like iconv or textutil to change the encoding after all the data has been written out as UTF-8.

Hi, blend3.

The text file appears to be written as UTF-8 Unicode. I don’t know how to stop that except by using the File Read/Write commands instead of a shell script.

You can read a UTF-8 file by script thus:

read file (ptdt & "test_text2.txt") as «class utf8»

It’ll also display properly in TextEdit if you use TextEdit’s “Open.” dialog and select UTF-8 in the dialog’s pop-up menu. If you can arrange for the first three bytes of the file to be a UTF-8 BOM, TextEdit will interpret the text as UTF-8 automatically.

set thefolder to choose folder --> choose a folder with a £ sign in the name
set ptdt to path to desktop as Unicode text
set folder_name to name of (info for thefolder)

set fref to (open for access file (ptdt & "test_text2.txt") with write permission)
try
	set eof fref to 0
	-- Write plain text to the file.
	write folder_name as string to fref
	
	(* -- Or write a UTF-8 BOM and text to the file.
	write «data rdatEFBBBF» to fref
	write folder_name as «class utf8» to fref *)
end try
close access fref

Hi Chrys/Nigel,
Thank you both for your precise responses. I had been using text item delimiters to strip the character out of each paragraph of the echoed text file, but using Nigel’s

read file (ptdt & "test_text2.txt") as «class utf8»

is all that’s necessary and makes my code a lot more efficient which in turn makes me feel a lot happier:D.
Many Thanks,
Nik