I have a table view in which the user can select multiple rows and click a “Delete” button. How can I delete all the selected data rows from the data source?
I tried:
tell first responder of front window
delete selected data rows
end tell
which doesn’t error, but doesn’t do anything.
I can delete just one row, like this:
tell first responder of front window
delete (get selected data row)
end tell
But when I try similar syntax for multiple rows:
tell first responder of front window
delete (get selected data rows)
end tell
this gives an error “Can’t make {data row id 7053 of …, data row id 7054 of …} into type reference.”:
I can resort to a loop:
tell first responder of front window
repeat with selectedDataRow in (get selected data rows)
delete selectedDataRow
end repeat
end tell
which works but since it sends n events, it can take a long time if there is a large selection.
I have done this in two of my scripts. To be honest, I copied the example script from the examples folder (there is one “with data source” and one without - sounds like you want the one with). It’s a little complicated and took a little study before I felt like I understood what all the functions do, but it is really good code and works great. Rather than reinvent it, you might want to go to the examples and use it.
The “Table” sample, to which you refer, doesn’t delete multiple data rows. It only deletes one data row. So I don’t see how you think it answers the question. Here is an extract of the code from it:
set selectedDataRows to selected data rows of tableView
...
delete (item 1 of selectedDataRows)
The only way I’ve been able to delete multiple selected rows is with the repeat loop as you suggested. One thing you can do to speed up this process is as follows. This is directly from applescript studio terminology reference guide…
When you make changes to the data for a data source for a visible view, performance is likely to suffer
as the data source continually updates the view. You can ensure optimum performance by turning
off updating while you modify the data source, then turning it on again when finished. The following
lines show how to do this.
– Turn off updating
set update views of theDataSource to false
– Add statements here that modify the data source
–
– Turn updating back on
set update views of theDataSource to true