Table View example - Read from File - 3 Columns

Hi Folks,

  • I know how to read from a file
  • I know how to create a table view with 3 columns,

but has anybody a clue on how to populate a table view from a text file?
The three colums of the table view should be filled with the source from the
text file - the text file looks like this: part 1; part 2; part 3
so, part 1 should be the first column
part 2 should be the second column
part 3 sould be the third column

then again part 1, part 2, part 3

I do not want you to make this for me, but maybe somebody has a little
tutorial or example for me…

Thanks for your help…

Stefan

Hi Stefan,

have you seen all the helpful sample projects in /Developer/Examples/AppleScript Studio/ ?

there are:
/Developer/Examples/AppleScript Studio/Simple Table/
/Developer/Examples/AppleScript Studio/Table/
/Developer/Examples/AppleScript Studio/Table Reorder/
/Developer/Examples/AppleScript Studio/Table Sort/

I’m sure, you’ll find there what you need

Hi Stefan,

thanks for your reply, you are completely right - it is helpful, but not solving my problems…

The Code I have until now looks like this:


property myList : missing value
--property myList : {{"Macintosh HD", "Macintosh HD:"}, {"Users", "Macintosh HD:Users:"}, {"bwaldie", "Macintosh HD:Users:bwaldie:"}}

property myList : missing value

(*
Contents of received.txt
In;067682007038; Testnachricht;
Out;06505700550; Das ist eine längere Nachricht;
In;0664444444; Von wem ist diese Nachricht?;
*)

on awake from nib theObject
	repeat
		
		set theFile to alias ((path to desktop as string) & "received.txt")
		set num_list to (read theFile using delimiter ";")
		
		repeat with num in num_list
			set a to item 1 of num_list
			set b to item 2 of num_list
			set c to item 3 of num_list
			exit repeat --To be honest, I have no clue how to exit a loop when there are no more lines
-- I am not sure how to fetch the .txt file line after line
			
		end repeat
	end repeat
	
	set myList to {"\"& a &\"", "\"& b &\"", "\"& c &\""} --this is not working
	
	set theTable to table view 1 of scroll view 1 of window 1
	set content of theTable to myList
	set recycledList to {}
	repeat with theRowData in (get content of theTable)
		copy (call method "allValues" of theRowData) to the end of recycledList
		-- or simply: copy (items of theRowData) to the end of recycledList
	end repeat
	log recycledList
	
	
end awake from nib


May you can help me solving this…

Thanks,

Stefan

try this


.
on awake from nib theObject
	set theFile to alias ((path to desktop as Unicode text) & "received.txt")
	set num_list to paragraphs of (read theFile)
	set myList to {}
	set {TID, text item delimiters} to {text item delimiters, ";"}
	repeat with num in num_list
		set end of myList to text items 1 thru 3 of num
	end repeat
	set text item delimiters to TID
	set content of theObject to myList
.

Edit: PS: I’m sorry, my friend, but I got all information about the structure of the table form the mentioned sample projects.
Very important is: connect NSTableView to the script, not NSScrollView

Hi Stefan,

thanks for your help - the reason why you can get the informations from the samples is because you
are a prof. and I am a noob…

Therefore I am not even able to deal with your tut.

What do you mean with “connect”? Do I have to do this in Interface Builder?

Sorry for my stupidness, but there are so many things which are black holes for me…

Thanks for your help,

Stefan

In Interface Builder click (once) on your table.
The title of the Inspector window will be NSScrollView Inspector. Make sure, that awake from nib is not checked in menu item AppleScript
Doubleclicking the table the title changes to NSTableView Inspector. Check awake from nib and choose the script clicking the radio button.

Save and make build in Xcode

Hi Stefan,

thanks a lot - perfect - works like a charme!

but (even here is a but) - how can I bring the contents of a “selected row” into different text fields?

According to the samples I did the following:


property myList : missing value
property contacts : {}
property contactIndex : 0
--property myList : {{"Macintosh HD", "Macintosh HD:"}, {"Users", "Macintosh HD:Users:"}, {"bwaldie", "Macintosh HD:Users:bwaldie:"}}

(*
Contents of received.txt
In;067682007038; Testnachricht;
Out;06505700550; Das ist eine längere Nachricht;
In;0664444444; Von wem ist diese Nachricht?;
*)

on awake from nib theObject
	set theFile to alias ((path to desktop as Unicode text) & "received.txt")
	set num_list to paragraphs of (read theFile)
	set myList to {}
	set {tid, text item delimiters} to {text item delimiters, ";"}
	repeat with num in num_list
		set end of myList to text items 1 thru 3 of num
	end repeat
	set text item delimiters to tid
	set content of theObject to myList
end awake from nib

on selection changed theObject
	if name of theObject is "contacts" then
		set theWindow to window of theObject
		
		-- Set the contact index to the current row, so that we can use it to update the right contact later
		set contactIndex to selected row of theObject
		
		if contactIndex = 0 then
			-- There wasn't any selected so clear the contact information
			my clearContactInfo(theWindow)
		else
			-- A contact was selected, so show the contact information
			my setContactInfo(theWindow, item contactIndex of contacts)
			
			-- Enable the "Update" and "Remove" buttons
			--set enabled of button "update" of theWindow to true
			--set enabled of button "remove" of theWindow to true
		end if
	end if
end selection changed


-- Get the values from the text fields and return a contact record
--
on getContactInfo(theWindow)
	tell theWindow
		return {name:contents of text field "name", address:contents of text view "adress" of scroll view "adress", city:contents of text field "city"}
	end tell
end getContactInfo

-- Set the text fields with the values from the contact
-- 
on setContactInfo(theWindow, theContact)
	tell theWindow
		set contents of text field "name" to name of theContact
		set contents of text view "adress" of scroll view "adress" to address of theContact
		set contents of text field "city" to city of theContact
	end tell
end setContactInfo



When I am clicking on a row then the error messge: Can’t get item 10 of {}. (-1728) (if I am pressing the 10th row) - and then: -1708

Thanks again for your help!

Stefan

does the original code from “Table” work?

      
 .
       -- Set the contact index to the current row, so that we can use it to update the right contact later
       set contactIndex to selected data rows of theObject
       
       if (count of contactIndex) = 0 then
           -- There wasn't any selected so clear the contact information
           my clearContactInfo(theWindow)
       else
           -- A contact was selected, so show the contact information
           my setContactInfo(theWindow, item 1 of contactindex)
 .

Hi Stefan,

now - when running the application it starts with 2 Error Messages:

Error - 1708

When pressing on a row of the data view the error message is as follows:

Can’t set name of «class datR» id 8 of «class datS» id 7 to name of «class datR» id 8 of «class datS» id 7. (-10006)

(I am pressing on the first row)

property myList : missing value
property contacts : {}
property contactIndex : 0
--property myList : {{"Macintosh HD", "Macintosh HD:"}, {"Users", "Macintosh HD:Users:"}, {"bwaldie", "Macintosh HD:Users:bwaldie:"}}

(*
Contents of received.txt
In;067682007038; Testnachricht;
Out;06505700550; Das ist eine längere Nachricht;
In;0664444444; Von wem ist diese Nachricht?;
*)

on awake from nib theObject
	set theFile to alias ((path to desktop as Unicode text) & "received.txt")
	set num_list to paragraphs of (read theFile)
	set myList to {}
	set {tid, text item delimiters} to {text item delimiters, ";"}
	repeat with num in num_list
		set end of myList to text items 1 thru 3 of num
	end repeat
	set text item delimiters to tid
	set content of theObject to myList
end awake from nib

on selection changed theObject
	if name of theObject is "contacts" then
		set theWindow to window of theObject
		
		set contactIndex to selected data rows of theObject
		
		if (count of contactIndex) = 0 then
			-- There wasn't any selected so clear the contact information
			my clearContactInfo(theWindow)
		else
			-- A contact was selected, so show the contact information
			my setContactInfo(theWindow, item 1 of contactIndex)
		end if
	end if
end selection changed


-- Get the values from the text fields and return a contact record
--
on getContactInfo(theWindow)
	tell theWindow
		return {name:contents of text field "name", address:contents of text view "adress" of scroll view "adress", city:contents of text field "city"}
	end tell
end getContactInfo

-- Set the text fields with the values from the contact
-- 
on setContactInfo(theWindow, theContact)
	tell theWindow
		set contents of text field "name" to name of theContact
		set contents of text view "adress" of scroll view "adress" to address of theContact
		set contents of text field "city" to city of theContact
	end tell
end setContactInfo


Thanks again for your continious help…

Best Regards,

Stefan

Hi Stefan,

the first 2 Errors are gone - they were because of missing handlers…

but the others are still there…

Thanks for your help!

Best Regards,

Stefan

My knowledge about tables is quite poor,
but it looks like a wrong reference, probably in the line

 set contents of text field "name" to name of theContact

Hi Stefan,

this should be correct! I found out, that the identifier has to be set - I did that but the error is still there…

I will investigate more further…

Thanks for your help,

Stefan

Hi Folks,

this issue is solved:



property myList : missing value
property contacts : {}
property contactIndex : 0
--property myList : {{"Macintosh HD", "Macintosh HD:"}, {"Users", "Macintosh HD:Users:"}, {"bwaldie", "Macintosh HD:Users:bwaldie:"}}

(*
Contents of received.txt
In;067682007038; Testnachricht;
Out;06505700550; Das ist eine längere Nachricht;
In;0664444444; Von wem ist diese Nachricht?;
*)

on awake from nib theObject
   set theFile to alias ((path to desktop as Unicode text) & "received.txt")
   set num_list to paragraphs of (read theFile)
   set myList to {}
   set {tid, text item delimiters} to {text item delimiters, ";"}
   repeat with num in num_list
       set end of myList to text items 1 thru 3 of num
   end repeat
   set text item delimiters to tid
   set content of theObject to myList
end awake from nib

on selection changed theObject
   if name of theObject is "contacts" then
       set theWindow to window of theObject
       
       set contactIndex to selected data rows of theObject
       
       if (count of contactIndex) = 0 then
           -- There wasn't any selected so clear the contact information
           my clearContactInfo(theWindow)
       else
           -- A contact was selected, so show the contact information
           set contents of text field "in_out" of theWindow to (item 1 of (item contactIndex of myList)) as string
           set contents of text field "sms_nummer" of theWindow to item 2 of (item contactIndex of myList)
           set contents of text view "text" of scroll view "text" of theWindow to item 3 of (item contactIndex of myList)
           log item contactIndex of myList
       end if
   end if
end selection changed

-- Get the values from the text fields and return a contact record
--
on getContactInfo(theWindow)
   tell theWindow
       return {name:contents of text field "name", address:contents of text view "adress" of scroll view "adress", city:contents of text field "city"}
   end tell
end getContactInfo

-- Set the text fields with the values from the contact
-- 
on setContactInfo(theWindow, theContact)
   tell theWindow
       set contents of text field "name" to name of theContact
       set contents of text view "adress" of scroll view "adress" to address of theContact
       set contents of text field "city" to city of theContact
   end tell
end setContactInfo


Thanks for your help…

Stefan