3 Replies Latest reply on Jul 7, 2013 12:36 PM by Larry G. Schneider

    Placing a text file into Illustrator

    seanolvie

      Hi everyone

       

      So I found out how to place an image into illustrator via scripting with javascript, however I am having difficulties with text files. My code is:

       

      var myDoc = app.activeDocument;

      var Layer1 = myDoc.layers[0];

      function getTextFile() {

          return File.openDialog('Please select the text file to be imported:', undefined, undefined);

      }

      var myTextfile = getTextFile();

      var myPlacedFile = Layer1.placedItems.add();

      myPlacedFile.file = myTextfile;

       

      But when I select a text file, it says it is an unsupported format. I'm assuming it has something to do with the extra options that come with importing a text document, specific for .txt and .doc for example. Has anyone had any success with this?

       

      For what it's worth I was working on an alternate method by read() ing in the contents of the .txt file and setting them equal to the contents of a textFrame, however it imported the text without carriage returns/newlines and I haven't been able to result that issue.

       

      Any assistance on the matter would be greatly appreciated!

       

      Thanks. Sean

        • 1. Re: Placing a text file into Illustrator
          pixxxel schubser Level 5

          Use the second method and do something like this:

           

           

          // Textdatei_auslesen.jsx
          // a txt file on ~/Desktop is required
          // regards pixxxelschubser
          
          var myFile = File ('~/Desktop/test.txt');
          myFile.open("r");
          var contentString = myFile.read();
          myFile.close();
          alert (contentString);  // or do something else
          

           

          This works good with *.txt, but not with *.doc

           

          Have fun

          • 2. Re: Placing a text file into Illustrator
            seanolvie Level 1

            Thanks for your reply. That's basically what I have been doing for now. Except the strange thing is, when I alert(contentString) it displays it correctly with all of the line breaks. However, when I set the contents of a text frame object equal to it, such as

             

            myTextFrame.contents = contentString;

             

            the new lines/carriage returns go away and I am left with one big block of text (with spaces). I found a work around by reading the .txt file in line by line using .readln() and adding carriage returns where necessary so that's fine.

             

            BUT I still would like a solution with word files (i.e. doc). Does anyone have any ideas?

            • 3. Re: Placing a text file into Illustrator
              Larry G. Schneider Adobe Community Professional & MVP

              You would have to add a rectangle and make it a textFrame object in order for the text to act like you want.