Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Annoying white spaces

Community Beginner ,
Feb 18, 2019 Feb 18, 2019

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.

Screen Shot 2019-02-19 at 1.15.04 PM.png

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.

Screen Shot 2019-02-19 at 1.15.13 PM.png

Actually, any space other than the normal space and the highlighted "Nonbreaking Space" would lead to a blank text file.

Screen Shot 2019-02-19 at 1.20.45 PM.png

Anybody knows why?

Thanks heaps!

TOPICS
Scripting
962
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 21, 2019 Feb 21, 2019

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

...
Translate
Community Expert ,
Feb 18, 2019 Feb 18, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 21, 2019 Feb 21, 2019

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?

Screen Shot 2019-02-22 at 2.04.48 PM.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 21, 2019 Feb 21, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 23, 2019 Feb 23, 2019

Didn't try with bullets but with other Unicode characters and it works on Windows 10, Excel 2007.

William Campbell
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 24, 2019 Feb 24, 2019
LATEST

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:

Screen Shot 2019-02-25 at 2.49.01 PM.png

Thanks heaps for your help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 21, 2019 Feb 21, 2019

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

William Campbell
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines