Problem when trying to sort columns

G’day

I’ve got a layout in Xcode and Interface builder than uses a split view and two tables with two columns.

I’ve used the code from Apple (the ‘on column clicked’ routine) to try and offer sorting in the tables but get an error that the sort column is not defined.

When I try and define a sort column I get an error (in the ‘on awake from nib’ that the variable for the data source is not defined.

Can anyone point out any basic mistakes this noobie to Studio is making please, cause I’m at wits end over this one.

Regards

Santa


-- rtfd Loader.applescript
-- rtfd Loader

--  Created by Brian on 7/08/07.
--  Copyright 2007 __MeSelf__. All rights reserved.

global theDataSource
global DeskTopPath
global theWindow
global SelectedItem
global theYear

property theSenderList : {}
property thertfdList : {}
property finalList : {}
property SenderFlag : false
property rtfdFlag : false


property contactsDataSource : null

on action theObject
	if name of theObject is "myTableView" then
		my showrtfds()
	end if
	if name of theObject is "rtfd Table" then
		my Getrtfd()
	end if
	if name of theObject is "Year selector" then
		set content of table view 1 of scroll view 1 of split view 1 of theWindow to {""}
		set content of table view "rtfdtable" of scroll view "rtfdscroll" of split view 1 of theWindow to {"", ""}
		set contents of text field "messages" of theWindow to "Select a year, and click the magnifying glass icon."
		set the thertfdList to {}
		set SenderFlag to false
		set rtfdFlag to false
	end if
end action

on awake from nib theObject
	set theDataSource to data source of table view "myTableView" of scroll view "myScrollView" of split view 1 of theWindow
	

-- I keep getting 'The data source is not defined' from this
	set the sort column of theDataSource to data column ("Email address") of theDataSource
	
	
set DeskTopPath to path to desktop
	if name of theObject is "Year selector" then
		tell application "Finder"
			set the_list to every item of desktop
			set temp_list to {}
			repeat with AnItem in the_list
				if the name of AnItem contains "Mail Data" and class of AnItem is folder then
					set end of temp_list to name of AnItem
				end if
			end repeat
		end tell
		tell theWindow
			delete every menu item of menu of popup button "Year selector"
			repeat with each_Item in temp_list
				make new menu item at the end of menu items of menu of popup button "Year selector" with properties {name:each_Item, title:each_Item, enabled:true}
			end repeat
		end tell
	end if
	-----
	if name of theObject is "cancel" then
		quit me
	end if
end awake from nib

on clicked theObject
	if name of theObject is "Go button" then
		my ShowSender()
	end if
end clicked

on double clicked theObject
	if name of theObject is "myTableView" then
		my showrtfds()
	end if
	if name of theObject is "rtfdtable" then
		my Getrtfd()
	end if
end double clicked

on will open theObject
	set theWindow to the theObject
	tell progress indicator "progress" of theWindow
		stop
		set indeterminate to false
		set contents to 0
	end tell
	set contents of text field "messages" of theWindow to "Select a year, and click the magnifying glass icon."
end will open

on mouse entered theObject event theEvent
	if name of theObject is "Year selector" then
		set contents of text field "messages" of theWindow to "Select a year, and click the magnifying glass icon."
	end if
	if name of theObject is "myScrollView" and SenderFlag then
		set contents of text field "messages" of theWindow to "Just click once on a Sender name."
	end if
	if name of theObject is "rtfdScroll" and rtfdFlag then
		set contents of text field "messages" of theWindow to "Just click once on an rtfd name."
	end if
end mouse entered

on choose menu item theObject
	set theCommand to tag of theObject
	if theCommand = 0 then
		tell application "rtfd Loader"
			activate
			set visible of window 1 to true
		end tell
	end if
end choose menu item

-- The "column clicked" event handler is called when the user clicks on a table column in the table view. We will use this handler to change the sort column of the data source as well as the sort order.
--
on column clicked theObject table column tableColumn
	-- Get the data source of the table view
	set theDataSource to data source of theObject
	
	-- Get the identifier of the clicked table column
	set theColumnIdentifier to identifier of tableColumn
	
	-- Get the current sort column of the data source
	set theSortColumn to sort column of theDataSource
	
	-- If the current sort column is not the same as the clicked column then switch the sort column
	if (name of theSortColumn) is not equal to theColumnIdentifier then
		set the sort column of theDataSource to data column theColumnIdentifier of theDataSource
	else
		-- Otherwise change the sort order
		if sort order of theSortColumn is ascending then
			set sort order of theSortColumn to descending
		else
			set sort order of theSortColumn to ascending
		end if
	end if
	
	-- We need to update the table view (so it will be redrawn)
	update theObject
end column clicked

--__________________________________________________________________________________________

on addMessage(TheEmailAddress)
	set theDataSource to table view 1 of scroll view 1 of split view 1 of theWindow
	set content of theDataSource to TheEmailAddress
end addMessage


on ShowSender()
	set SelectedItem to title of current menu item of popup button "Year selector" of theWindow as string
	set theYear to characters -4 thru end of SelectedItem as string
	set temppath to DeskTopPath & SelectedItem as string
	
	tell application "Finder"
		set theFilelist to every item of folder temppath
		set thecounter to count of theFilelist
	end tell
	tell progress indicator "progress" of theWindow
		set indeterminate to false
		set minimum value to 0
		set maximum value to thecounter
		set contents to 0
	end tell
	set theSenderList to {}
	set contents of text field "messages" of theWindow to "Please wait."
	set TheEmailAddress to {}
	repeat with EachPage in theFilelist
		tell progress indicator "progress" of theWindow to increment by 1
		set WholeList to (read file (EachPage as string) using delimiter return) as list
		repeat with x in items of WholeList
			set xx to my textToList(x, ",")
			if item 2 of xx is not in theSenderList then
				set end of theSenderList to item 2 of xx
				set end of TheEmailAddress to my extractAddressName(item 2 of xx)
			end if
		end repeat
	end repeat
	delay 0.5
	tell progress indicator "progress" of theWindow
		stop
		set contents to 0
	end tell
	set contents of text field "messages" of theWindow to "Now click on a Sender to make an rftd list from."
	set SenderFlag to true
	--set theSenderList to my sort_list(theSenderList)
	my addMessage(TheEmailAddress)
end ShowSender

on showrtfds()
	set clickedRow to clicked row of table view "myTableView" of scroll view "myScrollView" of split view 1 of theWindow
	if clickedRow ≠ 0 then
		try
			set theSender to item clickedRow of theSenderList
			set temppath to DeskTopPath & SelectedItem as string
			tell application "Finder"
				set theFilelist to every item of folder temppath
				set thecounter to count of theFilelist
			end tell
			tell progress indicator "progress" of theWindow
				set indeterminate to false
				set minimum value to 0
				set maximum value to thecounter
				set contents to 0
			end tell
			set finalList to {}
			set thertfdList to {}
			set contents of text field "messages" of theWindow to "Please wait."
			repeat with EachPage in theFilelist
				tell progress indicator "progress" of theWindow to increment by 1
				set WholeList to (read file (EachPage as string) using delimiter return) as list
				repeat with x in items of WholeList
					set xx to my textToList(x, ",")
					if item 2 of xx = theSender then
						set end of finalList to {item 1 of xx, item 3 of xx}
						set end of thertfdList to item 1 of xx
					end if
				end repeat
			end repeat
			set content of table view "rtfdtable" of scroll view "rtfdscroll" of split view 1 of theWindow to finalList
			delay 0.5
			tell progress indicator "progress" of theWindow
				stop
				set contents to 0
			end tell
			set contents of text field "messages" of theWindow to "Please now select an rtfd."
			set rtfdFlag to true
		end try
	end if
end showrtfds

on Getrtfd()
	set clickedRow to clicked row of table view "rtfdtable" of scroll view "rtfdscroll" of split view 1 of theWindow
	if clickedRow ≠ 0 then
		try
			set thertfd to item clickedRow of thertfdList
			set temppath to DeskTopPath & SelectedItem as string
			tell application "Finder"
				set FileName to "Yearly Orders " & theYear
				set theFilelist to every item of (entire contents of folder FileName) whose name is thertfd & ".rtfd"
				set errorflag to false
				if theFilelist = {} then
					set errorflag to true
				end if
				repeat with x in theFilelist
					tell application "TextEdit"
						try
							open x as alias
						end try
					end tell
				end repeat
			end tell
			if errorflag then
				repeat 16 times
					set contents of text field "messages" of theWindow to ""
					delay 0.1
					set contents of text field "messages" of theWindow to "Document " & thertfd & " couldn't be found."
					delay 0.2
				end repeat
			end if
		end try
	end if
end Getrtfd

on extractAddressName(theitem)
	set temp to my textToList(theitem, "<")
	set item 2 of temp to my replace_chars(item 2 of temp, ">", "")
	return temp
end extractAddressName

on replace_chars(this_text, search_string, replacement_string)
	set KeepDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to KeepDelimiters
	return this_text
end replace_chars

on textToList(theText, theSep)
	set {myTID, my text item delimiters} to {my text item delimiters, theSep}
	set soFar to text items of theText
	set my text item delimiters to myTID
	return soFar
end textToList

on sort_list(theList)
	set nl to (ASCII character 10)
	tell (a reference to my text item delimiters)
		set {old_delim, contents} to {contents, nl}
		set {theList, contents} to {"" & theList, old_delim}
	end tell
	return paragraphs of (do shell script "echo " & (quoted form of theList) & " | sort")
end sort_list

on clearMessages()
	tell scroll view "rtfd Scroll" of split view 1 of theWindow
		tell data source of table view "messages" to delete every data row
	end tell
end clearMessages


It’s OK, I found out how to do it myself (trial & many errors)

I changed this part to the following…


on addMessage(TheEmailAddress)
	set theDataSource to table view 1 of scroll view 1 of split view 1 of theWindow
	set content of theDataSource to TheEmailAddress
	set theObject to table view 1 of scroll view 1 of split view 1 of theWindow
	if name of theObject is "myTableView" then
		-- Get the data source of the table view
		set theDataSource to data source of theObject
		set the sort column of theDataSource to data column "Sender" of theDataSource
		set sorted of theDataSource to true
	end if
end addMessage