Copy link to clipboard
Copied
How can save a UTF-8 file?
This is my code:
var file = new File("C:\\tmp\\test.html");
file.open("w");
file.write('<?xml version="1.0" encoding="utf-8"?>\r\n');
file.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n');
file.write('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">\r\n');
file.write('<meta http-equiv="Content-Language" content="ja" />\r\n');
file.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\r\n');
file.write('<body>\r\n');
file.write('送信致しますので、宜しくご検討のほどお願い致します');
file.write('\r\n</body>\r\n');
file.write('</html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">\r\n');
file.write('</?xml version="1.0" encoding="utf-8"?>\r\n');
file.close();
result of file is utf8, but text can"t output correct
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<meta http-equiv="Content-Language" content="ja" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<body>
‘—M’v‚µ‚܂·‚̂ŁA‹X‚µ‚‚²ŒŸ“¢‚̂قǂ¨Š肢’v‚µ‚܂·
</body>
</html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
</?xml version="1.0" encoding="utf-8"?>
You can set the encoding of the file using the encoding property of the File object. Something like the following
file.encoding = "UTF-8";
See the following
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#File.html
-Manan
Copy link to clipboard
Copied
You can set the encoding of the file using the encoding property of the File object. Something like the following
file.encoding = "UTF-8";
See the following
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#File.html
-Manan
Copy link to clipboard
Copied
Thank you.
Copy link to clipboard
Copied
I used the following code and it seems to be fine, see the attached screenshot showing the encoiding of the file. The special characters also look fine to me.
var file = new File("/Users/manan/Downloads/test.html");
file.encoding="UTF-8";
file.open("w");
file.write('<?xml version="1.0" encoding="utf-8"?>\r\n');
file.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n');
file.write('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">\r\n');
file.write('<meta http-equiv="Content-Language" content="ja" />\r\n');
file.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\r\n');
file.write('<body>\r\n');
file.write('送信致しますので、宜しくご検討のほどお願い致します');
file.write('\r\n</body>\r\n');
file.write('</html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">\r\n');
file.write('</?xml version="1.0" encoding="utf-8"?>\r\n');
file.close();
See at the bottom right to see the encoding deciphered by the editor and also the characters are shown properly.
-Manan
Copy link to clipboard
Copied
Syntactically the XML declaration "<?xml"... from the first line is no element but a processing instruction, therefor it does not need a balancing end tag (your last file.write).
Better also omit the attributes on the html end tag.
For details, see https://www.w3.org/TR/xhtml1/ and https://validator.w3.org