help with subroutine please

The following awake from nib handler loads a table view in my AS app with Data:

on awake from nib theObject
tell application “Finder”

	set prefsFolder to (home as text) & "Desktop:"
	set fileList to name of every item in folder prefsFolder whose kind is not "disk" and kind is not "shared disk" and name is not "Trash" as list
	repeat with fileItem in fileList
		copy fileItem as list to end of tableData
	end repeat	
end tell
-- Create the data source
set theDataSource to make new data source at end of data sources with properties {name:"names"}

-- Create each of the data columns, including the sort information for each column
make new data column at end of data columns of theDataSource with properties {name:"name", sort order:ascending, sort type:alphabetical, sort case sensitivity:case sensitive}

-- Make this a sorted data source
set sorted of theDataSource to true

-- Set the "name" data column as the sort column
set sort column of theDataSource to data column "name" of theDataSource

-- Set the data source of the table view to the new data source
set data source of theObject to theDataSource

-- Add the table data (using the new "append" command)
log tableData

append theDataSource with tableData
tell window 1
	set contents of text field "folderName" to prefsFolder as string
end tell
set theTableView to table view "names" of scroll view "names" of window "main"
set selected rows of theTableView to {1}
set numberOfItems to count of fileList

tell window "main"
	set contents of text field "number" to numberOfItems & "items in list" as string
end tell

end awake from nib

It works fine but I would like to make the loading of data a subroutine that I can call from other handlers in my application eg when deleting rows from the table do: my Subroutine() or: on awake from nib: my Subroutine()
I have tried to implement this and failed miserably :frowning:

any suggestions would be great
thanks
Peter

on awake from nib
     sayHello()
end awake from nib

to sayHello()
     display dialog "Hello"
end sayHello

This is the basics of handler usage. When executed “awake from nib” in your app, subroutine “sayHello” is called and run.
So, you can put anything you want into sayHello and it will be executed any time you wish…

Did I understand your question :?:

But for an update-table routine, maybe you should create the data source only the first time, then include into “sayHello” or whatever-is-called-your-routine the following functions: to refresh the table, delete every data row of data source of blah, and create the new rows; to append data, you can use the “append” command, so you don’t need destroy-rebuild the table’s contents every time…

thanks jj
I have tried similar before, here is my latest effort :

(* ==== Properties ==== )
global prefsFolder
property folderName : “”
property tableData : {}
property fileList : {}
property theKind : “”
property kindData : {}
property theFileName : “”
global thePath
global openPath
property theFile : “”
property theDate : “”
property theSize : “”
property theIcon : “”
property thePosix : “”
property thecreation : “”
property theTitle : “”
property numberOfItems : “”
(
==== Event Handlers ==== *)


to updateTable()
tell application “Finder”
–set fileItem to {name:name, kind:theKind}
set prefsFolder to (home as text) & “Desktop:”
set fileList to name of every item in folder prefsFolder whose kind is not “disk” and kind is not “shared disk” and name is not “Trash” as list
repeat with fileItem in fileList
copy fileItem as list to end of tableData
end repeat
end tell
tell window “main”

	-- Create the data source
	set theDataSource to make new data source at end of data sources with properties {name:"names"}
	
	-- Create each of the data columns, including the sort information for each column
	make new data column at end of data columns of theDataSource with properties {name:"name", sort order:ascending, sort type:alphabetical, sort case sensitivity:case sensitive}
	
	-- Make this a sorted data source
	set sorted of theDataSource to true
	
	-- Set the "name" data column as the sort column
	set sort column of theDataSource to data column "name" of theDataSource
	
	-- Set the data source of the table view to the new data source
	set data source of theObject to theDataSource
	
	-- Add the table data (using the new "append" command)
	log tableData
                 append theDataSource with tableData

end tell
tell window 1
	set contents of text field "folderName" to prefsFolder as string
	
end tell
set theTableView to table view "names" of scroll view "names" of window "main"
set selected rows of theTableView to {1}
set numberOfItems to count of fileList

tell window "main"
	set contents of text field "number" to numberOfItems & "items in list" as string
end tell

end updateTable

on awake from nib theObject
updateTable()
end awake from nib

I have tried variations on this, but I keep getting “NSCannotCreateScriptCommandError (10)” or theObject is not defined
I have included the whole whole data source script in the updateTable subroutine because one of the main reasons for updating the table is to change the source folder (prefsFolder)

hope that makes sense :slight_smile:
thanks again
Peter

Unless I’m missing something, you have a problem here:

set data source of theObject to theDataSource

Since theObject is an undefined variable.
As a general rule, you can place several test lines, such as “log 1”, “log 2”, so you can find where is the problem.

Here is a set of handlers I use in several projects:

on awake from nib theObject
	set theTable to table view "x" of scroll view "x" of theObject
	set theDataSource to make new data source at end of every data source with properties {name:"x"}
	make new data column at end of every data column of theDataSource with properties {name:"x"}
	set data source of theTable to theDataSource
	set someDataToPopulate to {"a", "b", "c"}
	actualizar(table view "x" of scroll view "x" of theObject, someDataToPopulate)
end awake from nib

on actualizar(theObject, stringList) --> rebuild table
	set theDataSource to data source of theObject
	set update views of theDataSource to false
	delete every data row of theDataSource
	repeat with i in stringList
		set theDataRow to make new data row at end of every data row of theDataSource
		set contents of data cell (name of theObject) of theDataRow to i
	end repeat
	set update views of theDataSource to true
end actualizar

So, you don’t need re-create the data source every time, but delete the old rows and create the new ones. And you can call the handler “actualizar” allways you need. Eg, attached in a button:

on clicked theObject
	set newData to {"d", "e", "f"}
	actualizar(table view "x" of scroll view "x" of window "x", newData)
end clicked

jj, thanks again for helping me out.
I have had success with your suggestions, though I am not using any parameters (yet) with my subroutine
I have to re-read the documentation on that but I get the general idea.
thanks again for being so helpful
all the best
Peter :smiley: