trying to write text encoded for PC

Hi, I’m trying to write .ini files and .bat files for a windows machine and I’m running into troubles with encoding and line endings. I realize that PC lines end with “\r\n” while mac ends with “\r”. What I can’t figure out is what encoding to use. I’ve tried a few encodings with various unusable results:

This example doesn’t understand the line endings when I open the result in notepad.

write (job & "\r\n") to theFile as «class utf8»

without “as «class utf8»” I get a space between every charactor when I open the file in notepad. But the line endings are correct.

write (job & "\r\n") to theFile

Just thought I’d post the solution here in case someone else fins themselves trying to do the same. It turns out that bat files are utf8 with no BOM and you have to make sure to have the windows CRLF line endings. At least in my testing, that’s what works. It’s simple really if you get the right combination, but I must have tried hundreds of different subtly different combinations and didn’t realize how close I was at the beginning until I got the line endings, BOM and encoding exactly right.

Here’s a snippit from my working script

set crlf to (ASCII character 13) & (ASCII character 10)
	open for access theFile with write permission
	repeat with job in batchList
		write (job & crlf) to theFile as «class utf8»
	end repeat

Hi.

To elaborate on that a little, text within AppleScript is UTF-16 Unicode, so unless you specify ‘as «class utf8»’ or ‘as string’ with the ‘write’ commnd, it’s UTF-16 that gets written to the file. That’s why you were seeing interpolated “spaces” (actually zero-bytes) when you opened the file in an editor that was expecting something else.

Hello

There is a utility named iconv which at least shipped with Tiger and are still here in Snow Leopard, its called
iconv. And the rest of the problem is just making the correct do shell script command.
Excerpt from the man page, which you should get from a terminal window by typing “man iconv” :

The iconv program converts text from one encoding to another encoding.
More precisely, it converts from the encoding given for the -f option
to the encoding given for the -t option. Either of these encodings
defaults to the encoding of the current locale. All the inputfiles are
read and converted in turn; if no inputfile is given, the standard
input is used. The converted text is printed to standard output.

Cheers

McUsr

Hi @ all,
i`m confused about it.

I want to read a text file as MacRoman, convert to and write as Windows Ansi.
The conversion seems to work, but can’t figure out how to write it to file as Windows ansi …

set theFile to quoted form of POSIX path of (path to desktop) & "test.txt"
set MyNewText to do shell script "iconv -f MacRoman -t CP1252 " & theFile

(*
input: ÄäöÖ?%&
=)(
!"§


ouput: "Æ’°Ë†Ã·?%&
=)(
!\"ß"
*)

One example which doesn’t work … seems to write standard Mac ouput

set theFile to quoted form of POSIX path of (path to desktop) & "test.txt"
do shell script "cat " & theFile & " | iconv -f MacRoman -t CP1252 " & theFile

May be this one with using AS write works.
Have to test it on my windows PC at work …

set theFile to (path to desktop) & "test.txt" as text
set MyNewText to do shell script "iconv -f MacRoman -t CP1252 " & quoted form of POSIX path of theFile
set theFile to theFile as alias
open for access theFile with write permission
set eof of the theFile to 0
write MyNewText to the theFile starting at eof
close access the theFile

Hello

I think you are better off with piping the output from the iconv command into a file in the do shell script command, as output from the do shell script command is automagically converted to utf8/utf16 in Tiger/Leopard.

Cheers

McUsr

thx
you’re right