How to create a series of texte frame using values from txt file

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / How to create a series of texte frame using values from txt file

Viewing 5 reply threads
  • Author
    Posts
    • #88733
      Kathy Cote
      Member

      Hi All ;-)
      I wondered if it was possible to change the code below?
      I want to read the txt file to create multiple new text frame in an active document.
      The code I have now written all row in one text frame.

      Example txt file:
      Joe(tab)65(tab)M
      Denis(tab)34(tab)M

      The code puts all the text in one text frame/per line.
      A text frame for:
      Joe 65 M

      and one for:
      Denis 34 M

      I prefer to have a text frame for each column:
      Text frame for: Joe
      Text frame for: 65
      Text frame for: M
      …

      Someone can help me?
      Thanks

      This is my code:

      //Au document Indesign ouvert, exécuter le script. Ill faut founir par default le fichier txt à lire.
      
      const COLUMN_WIDTH = 6; // Define the column width in inch
      
      var pageIndex;
      var textFramesExported; // not implemented.
      
      // Add a new dokument. Set myDoc to app.activeDocument to use
      // the current document instead of creating a new one.
      var myDoc = app.activeDocument; //Au document actif
      
      // Mettre par default le fichier txt à utiliser. (The doSomethingWithTextRow function is called upon for every line of text read.)
      readFileLineByLine('/Users/kcote/Documents/En cours/••Autres/•Indesign/Create_Batch_NewDocs/Classeur3.txt/', doSomethingWithTextRow);
      
      function doSomethingWithTextRow(row){
      // We expect the text line to be TAB separated (\t = TAB). We get that from just copying the contents of an
      // excel file into a text document.
      var cells = row.split('\t');
      var companyName = cells[0]; // The Company name in the first slot of the array
      var width = cells[1];
      var height = cells[2];
      
      // Create a new text frame for every row handled
      //var pageIndex = 0; // Count up when you have exported a number of texts, I leave this for you to do.
      
      var newTextFrame = myDoc.pages[0].textFrames.add();
      newTextFrame.contents = companyName;
      newTextFrame.contents = width;
      newTextFrame.contents = height;
      
      // The text frame is created in the top left corner.
      newTextFrame.geometricBounds = [0, 0, height + ' in', width + ' in']; // Top, Left, Bottom, Right
      
      // You might want to move the textframes to other positions, keeping track of how many you put out per page.
      newTextFrame.move( [10, 10] );
      
      }
      
      function readFileLineByLine(path, callbackFn){
      var myFileIn = new File(path);
      if (File.fs == 'Windows'){
      // This was probably added to recognize UTF-8 (even without its start marker?)
      myFileIn.encoding = 'UTF-8';
      }
      myFileIn.open('r');
      var myEncoding = myFileIn.encoding;
      try{
      if (!myFileIn.exists){
      throw('Missing file: ' + myFileIn.fsName)
      }
      var ln = '';
      while(!myFileIn.eof){
      // Read the lines from the file, until an empty line is found [now as a remark].
      ln = myFileIn.readln()
      // if(ln !='' && ln!='\n'){
      // Call the function supplied as argument
      callbackFn(ln);
      // }
      }
      }catch(e){
      alert(e);
      gCancel = true;
      }
      finally{
      myFileIn.close();
      }
      }
      
      
    • #88749
      Loic Aigon
      Member

      Hi

      Sorry to ask but what about datamerge ? It look slike it would do all of it natively ?

      Loic

    • #88756
      Kathy Cote
      Member

      Hi,
      I usually use the data merge but it is a bit complicated if I want to use data merge with 50 different document formats.

      I have a script that can create all my new Indesign documents with a .txt file. In those new documents, I want to create multiple automatic text frame.

      Thanks ;-)

      Kathy

    • #88815
      Kathy Cote
      Member

      Hi,
      I try to run the following script. The script will create the new InDesign document from a .txt file. It names new files according to their sizes, save and close. It works.

      Before closing, I would like to create a new text frame for each of the created files with the information of the third column [2] (or variable “c”). There is indeed a new text frame but I failed to insert my text “c”. I’m sure the problem is my variable but I can’t fix it.

      I have a .txt file with 3 columns et 50 lines.

      someone of you have the solution?

      Your help is greatly appreciated,

      Kathy

      This is my code:
      var file = File.openDialog(“Select Your Text File”, undefined, false);
      var folder = Folder.selectDialog(“Select the folder where the new documents should be saved”);
      file.open(“r”);
      var content = file.read().split(“\n”);
      var pageIndex;

      for (var i = 0; i < content.length ; i++) {
      var curLine = content[i].split(“\t”);
      var w = curLine[0];
      var h = curLine[1];
      var c = curLine[2];

      docName = w + “X” + h + “_” + c ;

      try {
      var newDoc = app.documents.add(false);
      newDoc.documentPreferences.pageHeight = h;
      newDoc.documentPreferences.pageWidth = w;

      newDoc.save(new File(folder + “/” + docName + “.indd”));

      if (pageIndex==undefined)pageIndex=0;
      var newTextFrame=newDoc.pages[pageIndex].textFrames.add();
      newTextFrame.newDoc=c; //it does not work

      newDoc.close(SaveOptions.no)
      } catch(myError){}
      }

    • #88817
      Kathy Cote
      Member

      Hi,
      ok I got!

      myFrame=newDoc.textFrames.add();
      myFrame.contents = c;
      myFrame.geometricBounds = [0,0,1,w];

    • #88850
      Loic Aigon
      Member

      Thanks for sharing ;)

Viewing 5 reply threads
  • You must be logged in to reply to this topic.
>