JXA Javascript-automation parse CSV file into array

29 Views Asked by At

I'm using JXA in Script Editor on a Mac. I'm trying to parse a CSV file into an array. I am able to do it with the code below, however the array includes all the formatting information of the CSV file. How do I exclude this information and just keep the text in the file?

var editor = Application.currentApplication();
editor.includeStandardAdditions = true;

function readAndSplitFile(file, delimiter) {
  var fileString = file.toString();
  return editor.read(Path(fileString), { usingDelimiter: delimiter });
}

var file = editor.chooseFile({
ofType: "csv",
withPrompt: "Please select a csv file:"
})

readAndSplitFile(file, ",")

The CSV file's text is "dog, cat, pig, mouse"

The result I'm getting in Script Editor's result box is:

["{\\rtf1\\ansi\\ansicpg1252\\cocoartf2578\n\\cocoatextscaling0\\cocoaplatform0{\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;}\n{\\colortbl;\\red255\\green255\\blue255;}\n{\\*\\expandedcolortbl;;}\n\\margl1440\\margr1440\\vieww12540\\viewh9620\\viewkind0\n\\pard\\tx720\\tx1440\\tx2160\\tx2880\\tx3600\\tx4320\\tx5040\\tx5760\\tx6480\\tx7200\\tx7920\\tx8640\\pardirnatural\\partightenfactor0\n\n\\f0\\fs24 \\cf0 dog", " cat", " pig", " mouse}"]

How do I get the array to just be "dog, cat, pig, mouse"?

1

There are 1 best solutions below

0
user3080392 On

I found the solution. I changed the format of the csv file to plain text instead of rich text.