Skip to content

The First Responder: Setting it to a Table Cell

One of the problems I have had with using the first responder is the difficulty of setting the first responder (see this earlier posting for more on this) to a single cell in a table. After posting a question about this to both MacSripter and the official Apple email list for AppleScript Studio, a Philip Buckley was very nice to respond with a solution. You can find his explanation of how to do this here.

I have not been able to find any way accomplish this task in AppleScript. Philip Buckley’s solution uses the Cocoa method “editColumn:row:withEvent:select:” and the “call method” command to get at it (see this earlier posting for more on this)

This method takes four parameters, as his email outlines:

  • the column index — NB this is zero-based, so first column is 0, second column is 1 etc
  • the row index – NB this is zero-based, so first row is 0, second row is 1 etc
  • an NSEvent — should be nil I think, but for this purpose it seems it will accept pretty much anything, so in the example below, which I have tested, I have pased 0
  • a boolean — 1 for true
  • I created a sample XCode Project which implements his solution for others to play with. You can download it here:

    First Responder in Table Cell

    The project creates a data source for the main window’s table, fills it with some text in 10 rows, and then provides a button which, when clicked, will select the column and row indicated by the user in two separate text fields.

    The key code for the button is below:

    set theWindow to main window
    set theRow to (the contents of text field "selectRow" of theWindow) - 1
    (* I subtract one because the parameters in the Cocoa method used below is a 0-based index *)
    set theColumn to (the contents of text field "selectColumn" of theWindow) - 1
    (* I subtract one because the parameters in the Cocoa method used below is a 0-based index *)
    set theTable to table view 1 of scroll view 1 of theWindow
    set first responder of theWindow to theTable
    (* If you want the row to also be selected, uncomment the next line *)
    (* set selected row of theTable to (theRow + 1) *)
    call method "editColumn:row:withEvent:select:" of object theTable with parameters {theColumn, theRow, 0, 1}
    (* This example does not error check to see if the inputted row 
    and column numbers are greater than the total number of rows/columns *)
    

    Many thanks to Philip Buckley who responded to my posting about this problem.