0

Is there a way to globally remove the current item from the pasteboard (the item that will show up on pressing Command+V in any app) and make it so that pressing Command+V pastes the last copied item?

Say I copy foo and then bar, I press a button in my app that removes bar from the pasteboard, now if I press Command+V anywhere (in any app) I see foo pasted and bar is nowhere to be seen, it has completely vanish from the user's machine.

jscs
  • 63,694
  • 13
  • 151
  • 195
Kaatt
  • 1,061
  • 1
  • 8
  • 10
  • [Can I receive a callback whenever an NSPasteBoard is written to?](http://stackoverflow.com/q/5033266) – jscs Feb 14 '15 at 17:16
  • I've already been to that link. I'm not looking for listening to events. I think OS X automatically stores the last `n` clipboard items (maybe I'm wrong, I don't know). – Kaatt Feb 14 '15 at 17:18
  • It doesn't, so the first step is storing them yourself. – jscs Feb 14 '15 at 17:19
  • Thanks. I just got confused by all the different types of pasteboards there are and incorrectly assumed this. – Kaatt Feb 14 '15 at 17:22

1 Answers1

1

No, you can't do this. The system pasteboard does not have any "recent pasteboard items" mechanism.

In fact, copied data is not even necessarily on the pasteboard, ever. Apps can "promise" pasteboard data by putting a type in the list of available types, but not putting any data there. An object is registered as the "owner" of that type. If some other app requests the data for that type, the owner is asked to provide it at that time. This is especially appropriate for when the pasteboard data would be expensive to produce, transmit from the source app to the pasteboard server, and stored.

So, just because you copied "foo" in an app, that doesn't mean that "foo" was ever on the pasteboard. Consequently, there's no way to go back to having "foo" on the pasteboard.

Furthermore, when something else clears the pasteboard to put new data on it, the owners of all promised data are notified so they can clean up any state they may have recorded in order to fulfill that promise. So, even the source app won't be able to provide "foo" to once "bar" has been put on the pasteboard.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • This seems slightly misleading to me. Promised data doesn't stop you from polling the change count. If there's new data, promised or not, then you can force it to be delivered to you, either immediately with `readObjectsForClasses:options:` or after checking `canReadObjectForClasses:options:` Then you can keep it for later restoration of the pasteboard. – jscs Feb 17 '15 at 09:47
  • He said he was looking for a built-in facility. I'm explaining that it doesn't exist. As to your suggestion, polling can miss a change so you'd restore something other than the last contents. Also, restoring the exact state of the pasteboard has some surprising complexities. In any case, it's a bad idea for some pasteboard-saver program to pull all the items in all of their types every time the pasteboard changes. The user could copy megabytes of document data. The app may promise it in multiple types (e.g. app-specific structured data, rich text, plain text). You'd force all to be realized. – Ken Thomases Feb 17 '15 at 20:48