I have a table I am trying to export to a text file. The table contains two columns.
I used code I located on the site to separate the columns using commas (csv).
set theData to (contents of every data cell of every data row of theDataSource)
set text item delimiters to {","}
repeat with i in theData
set contents of i to i & return
end repeat
set theData to (theData as text)
set theFile to open for access (chaptersFile) with write permission
write theData to theFile
close access theFile
set text item delimiters to {""}
Which works great…almost.
The problem is that the saved file is listed like this:
Column 1,Column Two,
,Column 1, Column Two,
,Column 1, Column Two
It adds the TID to the beginning of the column as well. I even understand why it’s happening, but how to I fix it?
I considered setting up a custom repeat function that referenced each data cell of each column but I couldn’t seem to reference it correctly because if I do
set theData to (content of data cell 1 in every data row of theDataSource & "," & content of data cell 2 in every data row of theDataSource & return)
Its just going to be
Column 1 Column 1 Column 1 , Column Two Column Two Column Two
Am I missing something? Or a better question might be, what am I missing?
I still have been unable to solve this problem.
Does anyone have a suggestion or solution?
Try this. It should work on any number of columns.
Cheers,
Craig
set fileString to ""
repeat with i from 1 to count of theData
set thisList to item i of theData
repeat with l from 1 to count of thisList
set thisItem to item l of thisList
set fileString to fileString & thisItem & ", "
end repeat
set fileString to texts 1 thru -3 of fileString & return
end repeat
Craig,
thank you for the reply.
I was unable to get the code to validate with the word “texts” but after changing it to “text” it validated, but the file is not saving correctly.
Here is the chunk of code that is involved, with your code implemented:
if the name of theObject is "save" then
set chaptersFile to (("/Library/Application Support/myApp/" & songSize) & ".ttt")
set chaptersFile to POSIX file chaptersFile
set theData to (contents of every data cell of every data row of theDataSource)
set fileString to ""
repeat with i from 1 to count of theData
set thisList to item i of theData
repeat with l from 1 to count of thisList
set thisItem to item l of thisList
set fileString to fileString & thisItem & ", "
end repeat
set fileString to text 1 thru -3 of fileString & return
end repeat
set theFile to open for access (chaptersFile) with write permission
write theData to theFile
close access theFile
end if
Did I incorporate that correctly?
I added two rows to my table and edited their names to look like this.
Row 1-Column 1 Row 1-Column 2
Row 2-Column 1 Row 2-Column 2
The text file should look like this:
[b]
Row 1-Column 1, Row 1-Column 2
Row 2-Column 1,Row 2-Column 2[/b}
and it wrote stuff like listlistutxt but it did include the correct data, just had other mysterious data with it with no returns.
Thanks again for your help and I look forward to hearing back from you
EDIT: clarification
GOT IT! 3 days of searching and scratching my eyes out and finally got it!
More or less, I’m writing fileString to the text file instead of theData. I should’ve seen that…
Craig, you are tha man!
Here the completed code:
if the name of theObject is "save" then
set chaptersFile to (("/Library/Application Support/myApp/" & songSize) & ".ttt")
set chaptersFile to POSIX file chaptersFile
set theData to (contents of every data cell of every data row of theDataSource)
set fileString to ""
repeat with i from 1 to count of theData
set thisList to item i of theData
repeat with l from 1 to count of thisList
set thisItem to item l of thisList
set fileString to fileString & thisItem & ", "
end repeat
set fileString to text 1 thru -3 of fileString & return
end repeat
set theFile to open for access (chaptersFile) with write permission
write fileString to theFile
close access theFile
end if
Hi,
this might be a faster solution
if the name of theObject is "save" then
set chaptersFile to ((path to application support folder as text) & "myApp:" & songSize & ".ttt")
set {TID, text item delimiters} to {text item delimiters, ", "}
set theData to (contents of every data cell of every data row of theDataSource) as text
set text item delimiters to TID
set theFile to open for access chaptersFile with write permission
write theData & return to theFile
close access theFile
end if