Copy/paste problems

The “cheap” way to copy/paste is to use StandardAdditions “the clipboard”. For simple content, that works, but not for structured things that may include e.g an NSDate. Example:

The actual content I want to copy/paste are table rows, like this:
set myArray to theArrayController’s selectedObjects()
set the clipboard to (myArray as list)
the clipboard as list -->can raise malloc warning

myArray is an array of NSDictionaries.

If one of the dictionary components (i.e the data in one of the cells of the table) is e.g NSDate, then “the clipboard as list” raises the warning “malloc: reference count underflow for 0x2006c79a0, break on auto_refcount_underflow_error to debug.

One way to get around this is to convert the selected rows to the AppleScript form by Shane’s “fordDeep”, and that works fine for the kind of data I have. But it feels like doing it the Cocoa way would be the proper way, at least in the long term, and especially if I want to support drag&drop.

So I turn to NSPasteboard instead. But it quickly becomes pretty complicated, since when writing to the pasteboard by (“thePasteboard’s writeObjects_(myArray)”) it reports: “Instances of class NSCFDictionary not valid for NSPasteboard -writeObjects:. The class NSCFDictionary does not implement the NSPasteboardWriting protocol.”

So, then I read a lot about NSPasteboardWriting Protocol, and the Pasteboard Programming Guide, NSPasteboardItem, but it all gets a bit convoluted and above my head.

Is there is simple recipe how to copy/paste table rows from/to an array controller?

I’ve searched for and read some posts in this forum about drag&drop with tables, but it has mostly dealt with file urls or similar into a cell, or moving a single row inside a table, not between documents. Drag&drop of rows between documents would be nice of course, and I guess such a solution would more or less automatically support copy/paste of said rows. Has anyone done this?

–harald

Just a short note on speed: I had som doubts about speed with the current “cheap” solution, but it turns out that using “the clipboard as list” in combination with fordDeep is actually pretty fast: My table with 6 columns easily handle copy paste of a couple of thousand rows; at 10 000 rows you get a beach ball for several seconds, but it works, also between documents. I don’t know which part of the code that dominates the time, but overall I think that is very satisfactory.

From what I gather, this message implies that the problem has also been corrected in this instance – it’s related to using garbage collection.

You can look at the code fordDeep is using, to see how the date conversion is done.

It’s made more complicate by the fact that there were substantial changes in 10.6, and it’s not always clear which approach the documentation is referring to. I feel your pain.

There’s no automatic copy/paste – even the “proper” way involves writing code that gets the objects in the rows, deletes them from the source table, and inserts them into the destination table. NSPasteboard just gives you somewhere to store the objects (which you sometimes don’t need), and the rest is code to make it all happen nicely in the UI (which will be similar to that you have seen here for a single table).