Trying to dynamically create TableView columns

Hi all,

First off, I’m totally new to applescript studio, this is the very first attempt at writing anything with it…

I’m trying to dynamically create TableView columns.
Searching through the forum has been a great help so far but I cannot find the info I need to progress…

I am reading a csv file into a table view with no problems, but using a hard coded number of columns. I now want to be able to dynamically create the necessary columns on the fly, depending on the number of items in the csv file.

I have a check box on a window called “Main Window” to indicate if the file has headers.

Current file format example
USER, ID, TestID, Type, Valid
20666554,9985,558,0,0

Here is small portion of what I have written


-- Has the user indicated that the first line of the file contains header info?
-- True or False
set fileContainsHeaders to content of button "containsHeaders" of window "Main Window"

-- code omitted to keep this short
-- read the file

-- This is the data from the file
set num_list to paragraphs of (read_file)

if fileContainsHeaders then
  set {TID, text item delimiters} to {text item delimiters, ","}
  set firstItems to text item 1 of num_list

  -- This is where I want to get the number of items and then create the correct number columns
  -- and give them header titles as per the header line in the csv file
end if

When I use


display dialog firstItems as string

I can see that it returns … “USER, ID, TestID, Type, Valid”

What I now need to be able to do is count the number of items in that string - in this case it would be 5, and then dynamically create these in a table view.

Is there an applescript string function that I can use to count the items?

I am then thinking that once I get the number of items I would use “set number of Columns …” to the number I get and then use a
“repeat with” to set the “Header Title” for each column.

Can anyone point me to what functions I need to get the number of items in the string, and how to add colums to a table view?

Thanks and regards
John.

count {"a", "b", "c", 1, 2, 3}

Hi rnosek,

Thanks for the pointer :slight_smile:

I added this to the script:


if fileContainsHeaders then
          set {TID, text item delimiters} to {text item delimiters, ","}
          set firstItems to text item 1 of num_list
          
           set numberOfColumns to count the words in firstItems

           display dialog numberOfColumns

end if

Regards,
John.

Hi all,

I’ve searched high and low but cannot figure out how to set the “Columns” value for an NSTableView.

The pointer from rnosek got me as far as:


set numberOfColumns to count the words in firstItems

display dialog numberOfColumns

which is returns the correct number of columns that I want to assign to the table view.

How do I now dynamically change the Columns value from 0 (set in IB) to the value contained in numberOfColumns?

I have tried:


set the number of columns of table view "File Contents" of window "Main Window" to numberOfColumns

The error I get is:

“Can’t make number of coloumns of <<classtabW”>> “File Contents” of window “Main Window” into type reference. (-1700)

using “the value” instead of “the number” also results in an error.

Is what I am trying to do possibe within an AppleScript Studio application?