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

How to read each line of a .txt and save it in an array?

Explorer ,
Apr 03, 2020 Apr 03, 2020

I need to extract each line from a text file to assign it as a variable value. Thanks in advance.

Carpe diem
TOPICS
How to , Scripting , Type
2.9K
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 , Apr 03, 2020 Apr 03, 2020

 

 

 

 

var txt = File("path/to/file.txt");
txt.open('r');
var str = txt.read(); 
var arr = str.split('\n');

 

 

 

More on the File object and the methods and props available: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#File.html There are other ways to skin the cat. You can do a while statement on !txt.eof and use txt.readln() to push a string to a fresh array, but I find split using the new line character quick and easy. You may want to set txt.encoding to 'UTF-8'. Another good practice

...
Translate
Community Expert ,
Apr 03, 2020 Apr 03, 2020

 

 

 

 

var txt = File("path/to/file.txt");
txt.open('r');
var str = txt.read(); 
var arr = str.split('\n');

 

 

 

More on the File object and the methods and props available: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#File.html There are other ways to skin the cat. You can do a while statement on !txt.eof and use txt.readln() to push a string to a fresh array, but I find split using the new line character quick and easy. You may want to set txt.encoding to 'UTF-8'. Another good practice is to check if (txt.exists) before opening it. 

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
Explorer ,
Apr 03, 2020 Apr 03, 2020

Friends, another query, How do I verify that the image is inside a text frame?

var g=app.activeDocument.allGraphics;
alert("There are "+ g.length + " image ");

for (var i = 0; i < g.length; i++) {
    if (g[i].itemLink!=null && "?????????"){
    app.select(g[i].parent);
    var fp = app.selection[0].graphics[-i].itemLink.filePath;
    alert(fp);}else{
        continue;
    }
};

imagen.JPGexpand image

Carpe diem
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
Explorer ,
Apr 03, 2020 Apr 03, 2020
LATEST

I already solved it, still thanks for the previous answer. 😄

Carpe diem
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