9 Replies Latest reply on Apr 23, 2013 9:43 PM by BenCloutier

    Write a string to text with .write

    BenCloutier Level 1

      Hi, i did this a lot but for some reasont (that i can't obviously see right now), i can't make it work.

       

      1. I collect text from a specific layer from a bunch of InDesign Document and store them in "myDatas"
      2. I convert myData into a string with "join" (i guess that is not necessary but i'm out of ideas)
      3. open/create the text file
      4. (try to) write the string into it
      5. close the text file.

       

      The result is an empty text file but my alert show my all my collected texts!

       

      And if i do

           myStories = "some string text";

       

      this text is in the text file

       

       

      var myTxtFile = File("~/Desktop/" +  myDocumentNumber + "_Texts.txt");

      myStories = myDatas.join("\n");

      myTxtFile.open("w");

      myTxtFile.write(myStories);

      myTxtFile.close();

      alert("myStories: " + myStories);

        • 1. Re: Write a string to text with .write
          -hans- Level 4

          first try to set the encoding and linefeed:

           

          myTxtFile.encoding = "UTF-8";

          myTxtFile.lineFeed = "Unix"; //One of the values "Windows", "Macintosh", or "Unix".

           

          Is this happening on Mac or PC? Think I#ve read about this issue, but would have to google too

          • 2. Re: Write a string to text with .write
            BenCloutier Level 1

            This happens on a Mac. I'll sure try this and give feed back. Thanks

            • 3. Re: Write a string to text with .write
              BenCloutier Level 1

              Thanks alot, it worked!

               

              I don't understand tough why in this script, it worked without the encoding info.

               

              var newPrefs =

                          volumeClientNum + "\n" +

                          myMissingLinks + "\n" +

                          myUpdateLinks + "\n";

               

                          prefsFile.open("w");

                          prefsFile.write(newPrefs);

                          prefsFile.close();

              • 4. Re: Write a string to text with .write
                [Jongware] Most Valuable Participant

                ExtendScript is (quite unusually ;-) very fussy when writing files. If there is anything in your text string that cannot be handled by the encoding of your choice, it refuses to output *anything* at all.

                 

                Other than -hans-, I don't believe the line ending is an issue here. Surely the default is "whatever your current platform is"?

                 

                But his suggestion of setting the encoding to UTF-8 is something else. On a Mac you might have Mac Roman Encoding as a default, which is sufficient for your average plain text file (with a soupçon of various accented letters), but InDesign itself supports the full Unicode repertoire. It's likely you have some character or spacing code in your exported text that's *not* in the tiny range of MacRoman. Even if it's as small as a Thin Space.

                 

                UTF-8 is a good fix because by definition it supports *all* valid Unicode characters.

                 

                If you are interested in attempting to find "the" mischievous character (there might be a single one!), try searching in your ID document for this, using GREP:

                 

                [^[:ascii:]]

                 

                which searches for anything 'not 7-bit ASCII'. Only thing is, per non-ASCII you will have to look up whether it's in MacRoman or not.

                1 person found this helpful
                • 5. Re: Write a string to text with .write
                  BenCloutier Level 1

                  Ahhh now i get it. The missing puzzle piece. Thank you Jongware. So i should always specify the encoding then?

                   

                  Here is the script if someone need it. It may not be as clean as it should be but it works!

                  Export all text from a selected layer in multiple documents

                  • 6. Re: Write a string to text with .write
                    [Jongware] Most Valuable Participant

                    > So i should always specify the encoding then?

                     

                    It actually depends on what further purpose you use the exported "plain text" file for. Not all plain text editors can read UTF-8 (TextWrangler, my editor of choice on the Mac can, but TextPad, the nearest Windows equivalent, cannot).

                    Some text editors *expect* a UTF-8 BOM code at the start of the file; others do not.

                    See Kasyan's struggle with uncommon characters systems in this thread: http://forums.adobe.com/thread/1141456?tstart=60

                     

                    ("Uncommon" only from the narrow point if view from both Microsoft and Apple -- there is nothing uncommon about Uwe, having German accented characters in his paragraph style names, and Kasyan's Cyrillic characters in same.)

                     

                    Apart from always using UTF-8 (either with or without BOM), you don't have many *realistic* options. Before writing your text file, you could scan your gathered text string for any character *not* in MacRoman Encoding (this is just a list of 256 Unicodes) and replace these with a question mark. But 1. Kasyan's Cyrillic text, for example, would then be replaced in its entirety with ????'s; and, 2. you cannot properly read a MacRoman encoded text file on a Windows machine.

                    The same argument goes against converting to a Windows Encoding ("a" -- there are too many of them to list here).

                     

                    Actually, the safest way would be to throw away everything not 7-bit ASCII. But then you'd still have an issue with line endings! Mac, Unix, Windows ...

                     

                    You could try to stuff all of the above options into a dialog box and let the user decide :-/

                    • 7. Re: Write a string to text with .write
                      BenCloutier Level 1

                      [Jongware] wrote:

                       

                      You could try to stuff all of the above options into a dialog box and let the user decide :-/

                       

                      Very funny! They are not aware that there are multiple text encoding options. The workflow, right now is open all InDesign flyer pages one by one (sometime up to 24) and copy paste all of the products one by one (talk about 250 - 300 products) into Text Edit, then save as text.

                      • 8. Re: Write a string to text with .write
                        -hans- Level 4

                        Hi,

                         

                        had a short view to the script.

                        One thing:  a layer has a own property allPageItems, so there is no need to get the pageItems of the whole doc ...

                        • 9. Re: Write a string to text with .write
                          BenCloutier Level 1

                          Thanks, i really should read (all) the Adobe Scripting Guide.