Copy link to clipboard
Copied
I'm trying to grab contents from text frames and then write the contents to a text file using the below JavaScript:
var txtFile = File("~/Desktop/test.txt");
var a = app.selection[0].contents;
txtFile.open("w");
txtFile.write(a);
txtFile.close();
This script runs smoothly with the below text frame and it writes the contents to the text file.
But, if there is a space in the text frame that is a quarter space, the script still runs smoothly, you won't get an error message, but you end up with a blank text file.
Actually, any space other than the normal space and the highlighted "Nonbreaking Space" would lead to a blank text file.
Anybody knows why?
Thanks heaps!
Excel has issues deciphering UTF-8 text files, a google search comes with lots of hits on such kind of problems. Some suggest that adding the BOM markers to the files makes it work but i could not get the result correct even with that. Next i see the UTF-16 LE encoding works fine with excel. If i use the following
txtFile.encoding = "ISO-10646-UTF-16LE"
it works fine with excel and text editors as well, another option i tried was to convert the UTF8 file we created from the original code to UTF16E
...Copy link to clipboard
Copied
The issue is that by default the file is encoded in system default which does not seem to have a representation for these characters which are outside the purview of ASCII range. If you set an encoding that is capable of handling Unicode code points it should work. Try the following
var txtFile = File("~/Desktop/test.txt");
var a = app.selection[0].contents;
txtFile.encoding = "UTF-8"
txtFile.open("w");
txtFile.write(a);
txtFile.close();
-Manan
Copy link to clipboard
Copied
Thanks Manan.
Yes, by adding this one line code "txtFile.encoding = "UTF-8"", now I do get the data exported. But, this has just created another interesting problem. If there are bullet points in the data file, all those bullet points appear as bullet points if I open up the text file in a text editor app like TextEdit, but in Excel they look funny?
Copy link to clipboard
Copied
Excel has issues deciphering UTF-8 text files, a google search comes with lots of hits on such kind of problems. Some suggest that adding the BOM markers to the files makes it work but i could not get the result correct even with that. Next i see the UTF-16 LE encoding works fine with excel. If i use the following
txtFile.encoding = "ISO-10646-UTF-16LE"
it works fine with excel and text editors as well, another option i tried was to convert the UTF8 file we created from the original code to UTF16E using a text editor and then excel opened it up just fine. So now you have to decide which encoding would work for your workflow.
My test env is as follows
OS :- MAC OS 10.10.5
Excel :- 2008
P.S. :- Make sure the encoding i suggested to use in the code works for both Windows and MAC, i could not test on Windows also all the tests are done only on the bullet character
-Manan
Copy link to clipboard
Copied
Didn't try with bullets but with other Unicode characters and it works on Windows 10, Excel 2007.
Copy link to clipboard
Copied
txtFile.encoding = "ISO-10646-UTF-16LE" doesn't work on my machine, it would create a blank text file.
And, as you suggested, I could just stick to txtFile.encoding = "UTF-8", then convert the exported text file to UTF-16 like this:
Thanks heaps for your help.
Copy link to clipboard
Copied
The characters are Unicode and encoding UTF-8 should fix the problem. However, you may not want these characters and instead would prefer all spaces be regular spaces. For example, if moving content to a website where such characters serve no purpose. I have two scripts that accomplish "Change special spaces to regular" among other fixes to text content. Text Cleanup is for docs that will remain in ID, like those going to print, and Web Prep is similar but targets docs meant to export (or copy and paste) on its way to a website, which I use regularly to transfer magazine content from the print docs to the website articles and remove unwanted formatting, weird characters, and forced capitalization. Run either script first to clean up the content then writing to a file will not have the special space characters with Unicode values.
Text Cleanup https://www.marspremedia.com/software/indesign/text-cleanup
Web Prep https://www.marspremedia.com/software/indesign/web-prep
Find more inspiration, events, and resources on the new Adobe Community
Explore Now