Tables newbie

Help, :o

I’m trying to work out how to set the contents of a table to the contents of a .txt file. So for each line in the .txt file it should create a new row in the table.

Help please…

In the Applescript Studio Reference Manual, read the section pertaining to the “append” command. This section takes you through the steps to set up a data source, initialize data columns and add data to the data source. Also read up on working with “data rows.” You can work with tables without using a data source, however, I learned how to use tables WITH a data source and that’s how I always work them now.

When you read in your text file, convert it to a list where each paragraph of the text file is an item in the list. Each item of a list corresponds to a column of your table, i.e., if each item of your list contains 4 items, you should have 4 columns in your table. ({{“this”, “is”, “1”, “item”}, {“This”, “is”, “another”, “item”}}. ← This would fill 2 rows with 4 columns in each. In your case, it sounds like you will have a single column so each item of your list would be a single paragraph from your file.

Tables will also work with records and as I stated earlier you can work with tables with OR without a data source.

If after your reading, you still have questions, please ask them. Let us know how you intend to fill your table, i.e., at launch time, after the user selects a file, etc.

Hope this helps,
Brad Bumgarner, CTA

OK I looked into the manual but to be honest it didn’t really help.

here is the source code, I managed finally to work out how to get my txt file into a list that could be read, but now when I compile the app nothing happens, no errors nothing… help

property trackTableSource : “”
on awake from nib theObject
set theText to (read file “macintosh hd:Users:****:desktop:updates.txt” from 1 as text)
set trackTableSource to theText’s paragraphs
–on will open theObject
if trackTableSource is “” then
set trackTableSource to make new data source at end of data sources with properties {name:“trackTableSource”}
tell data source of table view “view” of scroll view “view” of window of theObject to append with ((read (theText))'s paragraphs)
end if

end awake from nib

Paul,

One thing that you didn’t do was to define the column(s) within the data source. I have a small example https://home.comcast.net/~b.bumgarner/Public/SingleColumnTableExample.zip that you can download to see what I’m talking about.

Hope this helps,
Brad Bumgarner, CTA

p.s.: I don’t often put files on my online storage so if the above link doesn’t work, let me know and I can send it to you directly.

Brad is right, you forgot to create a data column. Make sure it is named the same as your tablecolumn in IB. Here’s another simple example…

(* Store references to our table and datasource for easy reference *)
property theTable : null
property theTableDataSource : null

on launched theObject
	set theFilePath to "Macintosh HD:Users:jed:Desktop:untitled"
	set theDataList to paragraphs of (read file theFilePath)
	
	set theTableDataSource to make new data source at end of data sources with properties {name:"theTableDataSource"}
	make new data column at end of data columns of theTableDataSource with properties {name:"column"}
	
	set data source of theTable to theTableDataSource
	tell theTableDataSource to append with theDataList
	
	show window "Window"
end launched

on awake from nib theObject
	if name of theObject is "Table" then
		set theTable to theObject
	end if
end awake from nib

Thanks guys for your patience I’ve managed to get it working.

One other question how would I get this to work if I had a 2 column table?

¢ Define your data source as you already have.
¢ Define 2 columns (same procedure as for 1 column, but with a second line of code to define the second column).
¢ The list that will be appended will be a list of lists, i.e., {{“row 1 col 1”, “row 2 col 1”}, {“row 1, col 2”}, {“row 2 col 2”}}

Hope this helps,
Brad Bumgarner, CTA

Brad, I’m having real problems trying to get the 2nd column to work, is there any chance you could email my a sample. The previous download link didn’t work.

Thx

Not a problem, just let me know where to send it.

Brad Bumgarner, CTA