Highlighting a newly created table cell to edit it

I have a table view, and I have 2 handers- both do the same thing, they create a new blank line in my table. The user will then type in the text they want for that row, and optionally check a checkbox on or off. The handlers work fine, BUT…
I would like the newly created row to be selected, the blank text area where they should type should be highlighted. As I have it, the row is made, but the user must click again in the empty row to edit the text area, which seems a little clunky.
Also, if the user types then presses return, the editing should end, but instead it jumps to the first item of the table and selects it for editing. But if Return is pressed that should end editing.

Suggestions? TIA,
Chris

This will create a new row, select it, and start the editing process on the first column. You can use multiple tabs to adjust where the editing is. Note that objmainTable is a reference to your table view.

set newRow to make new data row at the end of the data rows of theDataSource
set selected data rows of objmainTable to {newRow}
tell application "System Events" to keystroke tab

I have to double-click a cell to edit it, but once I’m done and press return the editing ends and the selection stays where it is… it doesn’t jump to the first item. Maybe you need to play with the table view attribute settings in IB. I didn’t do anything special with the settings to make it work this way.

I found a better way to select a cell and start editing it in a table. Here’s a handler for that.

on addNewRow() -- this will make a new row in a table, select the row, and begin editing a cell in the row
	set columnIdentifier to "col1" -- make sure the column you want to edit has this name in the identifier field of the table column attributes in IB
	set myTable to table view 1 of scroll view 1 of window 1 -- this is a reference to your table view
	
	set newRow to make new data row at the end of the data rows of (get data source of myTable)
	set selected data rows of myTable to {newRow}
	set colNumber to call method "columnWithIdentifier:" of myTable with parameter columnIdentifier
	set rowNumber to call method "selectedRow" of myTable
	call method "editColumn:row:withEvent:select:" of myTable with parameters {colNumber, rowNumber, null, yes}
end addNewRow

Notice the variable in the first line… columnIdentifier. You have to do 2 things with that. First, in IB you have to go to the table column attributes for the appropriate column where you want the editing to start. In the table column attributes is a section called “Identifier”. Put a name in for the identifier. Second, in the handler above set that name to the variable.

Also notice the variable myTable in the second line of the handler. That is a reference to your table view so make sure it matches properly for your project.

If you do those things then the handler works well. Good luck.

Thanks, that is righteous!
I had been trying to use the “call method”, but it wouldn’t work. I was trying to plug in direct values from Applescript variables, I had not thought to use the first 2 single call methods to build the variables for the final method, in order to have the correct kind of data type (NS not NSCF). Works good for me.
I still can’t get my cell value to just complete the edit when using return, but it’s not that big of a deal right now. I’m working on a 10.4 unit now, maybe it will be fixed for me in 10.5.

Chris

Glad I could help! :cool:

I hadn’t thought to try applescript commands like you were having trouble with. I’m getting so used to using call methods that now that’s the first thing I look to do. In general call methods are faster so that’s why I go to them first now.

Anyway, good luck.