Saving reordered rows in NSTableView

Hey there,

Hoping someone can help me with this little problem.

I’ve a data source hooked up to an NSTableView and have added several rows.

I’ve used the following to enable drag re-ordering support:

set allows reordering of table view "TableViewName" of scroll view "ScrollViewName" of window "WindowName" to true

This works great, but I can’t for the life of me figure out how to save the state of the reordered rows, as of course the data source remains in whatever order it was in, regardless of the reordering performed on the Table View.

Can anyone suggest how I might achieve this?


There’s a “sorted data rows” property for a data source. Something like this should work…
set myList to sorted data rows of myDataSource

Thanks for the suggestion!

I can’t seem to get this to return anything - if I set a variable to this, and then try to log it, I get an error saying the variable isn’t defined.

If I try to log it direct without setting it to a variable first, it just logs the text, for example:

log (sorted data rows of data source of table view "TableView" of scroll view "ScrollView" of window "Window")

will result in the following text being logged to the console:

Does anyone have any other ideas? Would it be possible to bind the sort order to user defaults for example?


You probably have to get the “content” or “contents” of the sorted data rows. Maybe something like this will work…

set myList to contents of (sorted data rows of myDataSource)


Have you setup your data source to be sorted? If not try this.

	tell dataSource -- Whatever the name of the data source you created is
		make new data column at end of data columns with properties {name:"Column Name1", sort order:ascending, sort type:alphabetical, sort case sensitivity:case sensitive}
		make new data column at end of data columns with properties {name:"Column Name2", sort order:ascending, sort type:alphabetical, sort case sensitivity:case sensitive}

		set sorted to true
		set sort column to data column "Column Name1"
	end tell

Dallasbr: If I set the data source to be sorted, then you can no longer reorder the rows manually by dragging them into the desired order.

regulus6633: The contents seem to be blank. If I enable the data source to be sorted as Dallasbr suggests then this returns the data, but the as I say the rows can no longer be manually dragged into order.

Is there any way to save the order of rows if they are arranged by dragging?

Thanks for all the help and suggestions so far!

Ok I figured it out -

I checked to see what would happen if I dragged the rows into a new order, then added a new row. Quitting and restarting the app I saw that the rows remained in the correct order.

When adding a new row I have a call to a method that saves the data rows, which evidently gets the values out in the order they are being displayed.

So I have put a call to this save method on the ‘should quit’ handler. My only remaining question really is whether there is a better handler to use? I’ve looked at the drag and drop handlers, and have tried a few of them out, but none of those seem to get called when a row is drag-sorted. Is there a handler that will get fired off when a row is dragged into a new position at all?

The only problem with just having this on the ‘should quit’ handler is that if the application is force quit or crashes in between the sorting of rows, the changes will be lost.

I don’t really want to put a regular save in an idle event either, any ideas?