Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

import csv to become a list in applescript

Hi guys, I'm frozen.

I have a CSV file, exported from FileMaker, with date, time, telephone number.


date1,time1,tel1

date2, time2, tel2

dateN,timeN, telN


I need create 3 list8s) usable in applescript to perform some further actions.


{date1, date2, ... dateN}

{time1, time2, .... timeN}

{tel1, tel2, ....telN}


but I'm not able to get out from the MUD.


I have this, but just give me back {,}

set theFile to choose file with prompt "Select a csv file:"


set theFileContents to read theFile


set recs to paragraphs of theFileContents


set vals to {}


set AppleScript's text item delimiters to {","}



than in my attempt I reach to import all the CSV, but in a real mess, all confused with / and " everywhere



After that i tried this

set theFile to choose file with prompt "Select a csv file:"

set theFileContents to read theFile

--set theList to paragraphs of theFileContents

set namelist to {theFileContents}


Which open the CSV file, i can see all the text in the csv file bad formatted with a lot of / and " which are just craps. While if I improt tab file, i just have " at begin and end, and no mor

Posted on May 14, 2019 12:53 AM

Reply
Question marked as Best reply

Posted on May 21, 2019 2:27 PM

The original AppleScript code that I posted was for a 3 x 3 CSV. As the CSV may have more rows and columns, the original example becomes less efficient. Here is an example for a 3x5 CSV:


R1C1,R1C2,R1C3,R1C4,R1C5
R2C1,R2C2,R2C3,R2C4,R2C5
R3C1,R3C2,R3C3,R3C4,R3C5


The goal is to get each column in each row into its own list. The following AppleScript is a modification of my first post, and takes advantages of repeat loops and a list of lists:


-- from an n-column CSV, create separate lists of column data by row
-- change delim if not comma-delimited fields
-- select the document icon in the bottom toolbar to see the log output of the lists. Asterisks are
-- part of the log output and not actually within the lists.

set DELIM to {","}
set L1 to {} -- column 1 items
set L2 to {} -- column 2 items
set L3 to {} -- column 3 items
set L4 to {} -- column 4 items
set L5 to {} -- column 5 items

set Lx to {L1, L2, L3, L4, L5}

set acsv to (choose file)

set csvList to read acsv using delimiter linefeed
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, DELIM}
repeat with arow in csvList
	set ritems to text items of arow
	repeat with i from 1 to (count of ritems)
		copy (item i of ritems) to the end of (item i of Lx)
	end repeat
end repeat
set AppleScript's text item delimiters to TID

log L1
log L2
log L3
log L4
log L5
return


The stylized list results output that cleans up the log output:


L1 = {R1C1, R2C1, R3C1} -- all column 1 data
L2 = {R1C2, R2C2, R3C2} -- all column 2 data
L3 = {R1C3, R2C3, R3C3} -- all column 3 data
L4 = {R1C4, R2C4, R3C4} -- all column 4 data
L5 = {R1C5, R2C5, R3C5} -- all column 5 data


Similar questions

7 replies
Question marked as Best reply

May 21, 2019 2:27 PM in response to VikingOSX

The original AppleScript code that I posted was for a 3 x 3 CSV. As the CSV may have more rows and columns, the original example becomes less efficient. Here is an example for a 3x5 CSV:


R1C1,R1C2,R1C3,R1C4,R1C5
R2C1,R2C2,R2C3,R2C4,R2C5
R3C1,R3C2,R3C3,R3C4,R3C5


The goal is to get each column in each row into its own list. The following AppleScript is a modification of my first post, and takes advantages of repeat loops and a list of lists:


-- from an n-column CSV, create separate lists of column data by row
-- change delim if not comma-delimited fields
-- select the document icon in the bottom toolbar to see the log output of the lists. Asterisks are
-- part of the log output and not actually within the lists.

set DELIM to {","}
set L1 to {} -- column 1 items
set L2 to {} -- column 2 items
set L3 to {} -- column 3 items
set L4 to {} -- column 4 items
set L5 to {} -- column 5 items

set Lx to {L1, L2, L3, L4, L5}

set acsv to (choose file)

set csvList to read acsv using delimiter linefeed
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, DELIM}
repeat with arow in csvList
	set ritems to text items of arow
	repeat with i from 1 to (count of ritems)
		copy (item i of ritems) to the end of (item i of Lx)
	end repeat
end repeat
set AppleScript's text item delimiters to TID

log L1
log L2
log L3
log L4
log L5
return


The stylized list results output that cleans up the log output:


L1 = {R1C1, R2C1, R3C1} -- all column 1 data
L2 = {R1C2, R2C2, R3C2} -- all column 2 data
L3 = {R1C3, R2C3, R3C3} -- all column 3 data
L4 = {R1C4, R2C4, R3C4} -- all column 4 data
L5 = {R1C5, R2C5, R3C5} -- all column 5 data


May 14, 2019 7:28 AM in response to Andyk69

Give this a whirl. Here is what it does:

  1. It prompts you to pick the CSV file.
  2. It read that CSV file into a list, row by row, including the comma separators (e.g. {R1C1,R1C2,R1C3", "R2C1,R2C2R2C3"..."RnC1,RnC2,RnC3"} )
  3. It then loops through this list splitting on the comma delimiter, and appending these items to the end of their respective list
    1. Item 1 of row 1 is R1C1
    2. Item 2 of row 1 is R1C2
    3. Item 3 of row 1 is R1C3
  4. You end up with three lists containing their respective column number items


Assumption: The CSV has no header row, and it originated on macOS, so no Windows line endings to deal with.


set DELIM to {","}
set D to {} -- dates
set T to {} -- times
set Tel to {} -- telephone numbers

set acsv to (choose file)

set csvList to read acsv using delimiter linefeed
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, DELIM}
repeat with arow in csvList
	copy item 1 of text items of arow to the end of D
	copy item 2 of text items of arow to the end of T
	copy item 3 of text items of arow to the end of Tel
end repeat
set AppleScript's text item delimiters to TID

log D
log T
log Tel
return


Result:
	D   = {R1C1, R2C1, R3C1}
	T   = {R1C2, R2C2, R3C2}
	Tel = {R1C3, R2C3, R3C3}


May 15, 2019 4:31 AM in response to Andyk69

Your virgole4.csv file produced the following list content when run in the exact same AppleScript that I provided. I have reformatted the output for legibility:


D   = {"13 maggio 2019", "13 maggio 2019", "13 maggio 2019"}
T   = {"10:15", "10:45", "11:00 "}
Tel = {"3311778887", "3482434440", "3472952297"}


All the script does is generate the lists. If you did not check the document icon on the bottom toolbar of the Script Editor, and the Replies button in the middle toolbar, the script will appear to do nothing because no result will be shown.


At the bottom of Script Editor, click the blue themed icon shown below:



and once you have done that, click the Replies button shown:



Now, you will see that the AppleScript that I posted does not fail to produce your three lists as I originally indicated.

May 15, 2019 12:36 AM in response to VikingOSX

Thanks for your support , unluckily it does not works.

I mean, I read the script and it sound full of sense for me. But when i try to run does not work

this is the file I'm using


www.pcdata.it/upload18/virgole4.csv generated with excel for MAC


www.pcdata.it/upload18/virgole3.txt same file generated with textedit on MAC



The script allow me to choose the file, than does do nothing,

May 16, 2019 1:39 PM in response to VikingOSX

Hi, first at all thanks for your patience.

Then, I m a really dummy, You really teach me something, I did not know about the area below, with all those information.

Now, I see what is going on, but there is something strange.

www.pcdata.it/upload18/myresult.png

this is what i receive once i run the same script with the same CSV. There are a lot of " * ", and of course is not ok.

This part of the script, need to generate the list and then The list(s) must be processed by a second parte ( which is work ) to send text message:

here the complete script, first the part you are helping me, then the part which works ( if i write the list manually)


www.pcdata.it/upload18/the_two_part_togheer.txt ( the two part together )


www.pcdata.it/upload18/the_one_working.txt ( only the second part, working in sending text )



May 16, 2019 2:20 PM in response to Andyk69

Do not worry about those asterisks inside the lists. That is a visual by-product of using the log command, and those asterisks are simply not in your lists. I manually reformatted the lists in my previous post for readability.


You should review the integrity of the formatting within the CSV file though, as it looks as though the final Telephone number list data has inherited newlines, as those individual data items should appear on one line as the previous lists.

import csv to become a list in applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.