Skip to main content
Inspiring
June 19, 2012
Question

how to save a UTF-8 encoded text file ?

  • June 19, 2012
  • 2 replies
  • 6052 views

hi People

I have a little script which reads the source text from a layer and saves it to a .txt file. This is on a Mac and all was good until recently when I tried opening the .txt file on a PC in Notepad and found my ˚ degree symbols all whack.

Resaving the .txt file in TextEdit as Unicode (UTF-8) encoding solved the problem, now opens fine in Notepad.

But ideally I'd like the script to output the .txt as UTF-8 in the first place. It's currently Western (Mac OS Roman). I've tryed adding in myfile.encoding = "UTF8" but the resulting file is still Western (and the special charaters have wigged out again)

any help greatly appreciated../daniel

{

    var theComp = app.project.activeItem;

    var dataRO = theComp.layer("dataRO").sourceText;

   

    // prompt user to save file

    var theFile = new File ("~/Desktop/"+ theComp.name + "_output.txt");

    theFile = theFile.saveDlg("Save an ASCII export file.");

    if (theFile != null) {          // check user didn't cancel dialog

        theFile.lineFeed = "windows";

        //theFile.encoding = "UTF8";

        theFile.open("w","TEXT","????");

        theFile.writeln("move details:");

        theFile.writeln(dataRO.value.toString());

        }

    theFile.close();

}

2 replies

Inspiring
June 20, 2012

Hi,

I remember working hard two years ago on creating a correct text file on OSX, but did not remember if it was a utf-8 case or anything. As my home computer is not a mac, I have no mean to test it tonight, but anyway, here is the big line of it. :

var theFile= new File(.........);

theFile.open("w", "TEXT");

theFile.encoding = "BINARY"

theFile.linefeed = "Unix"

theFile.writeln("éàçËôù")

theFile.close();

Let me know if it is working.

Inspiring
June 20, 2012

hi

thanks for the suggestion but still no joy.

I was thinking it might have something to do with the Creator type but no joy there either.

at this stage it seems like the only option is stick with the saveas in Textedit which is a little dull.

Inspiring
June 20, 2012

Hi, I was just looking at how a  text software knows what is the text encoding of a file is and I found that on wikipedia. http://en.wikipedia.org/wiki/Byte_order_mark

So I created a utf8 file in notepad, and look at the binary. At the start of the file, there is those caracters : 0xEF,0xBB,0xBF or 

So you should try to add those characters at the start of the  file.

var theFile= new File(.........);

theFile.open("w", "TEXT");

theFile.encoding = "BINARY"

theFile.linefeed = "Unix"

theFile.write("");//or theFile.write(String.fromCharCode (0xEF) + String.fromCharCode (0xEB) + String.fromCharCode (0xBF)

theFile.write("Your stuff éàçËôù");

theFile.close();

Dan Ebberts
Braniac
June 19, 2012

Have you tried setting the encoding after you open the file?

Dan

Inspiring
June 19, 2012

hi Dan

Thanks for the suggestion but dang it, no joy. I tried setting encoding straight after opening the file and also just before closing but both had the same effect as above... the .txt file is still a Western (Mac OS Roman) file and the special characters have wigged out...