Kaatt
Kaatt

Reputation: 1151

Globally replace NSPasteboard.generalPasteboard()

I want the NSPasteboard.generalPasteboard() to be temporarily switched to a custom one generated by my app using NSPasteboard.pasteboardWithUniqueName()

So that when the user presses Cmd+C/Cmd+V while the pasteboard is switched, my app's pasteboard gets used instead of the general pasteboard.

Is there a way to do this?

Upvotes: 0

Views: 397

Answers (2)

Davit Petrosyan
Davit Petrosyan

Reputation: 3

Yes there is possibility to do that. You need to create your own singleton e.g. customPasteboard with inheriting from UIPasteboard, then with swizzling generalPasteboard you can return your own singleton. Keep in mind that started from iOS then "persist" property is set to NO for named pasteboard, which is different from general pasteboard, it's kept in memory per session.

Upvotes: 0

Ken Thomases
Ken Thomases

Reputation: 90531

No, there's no way to do this.

If you're only looking to change the behavior within your own app, you can add a category/extension to NSPasteboard to add a method that returns either the general pasteboard or your app's private pasteboard. That at least isolates the logic to one place. Everywhere else in your code, you'd use you custom method instead of +generalPasteboard.

If you're looking to change the behavior of other apps, too, then you're out of luck. What is the higher-level goal you're trying to achieve? Why do you want to change which pasteboard apps use? If you explain that, perhaps there's another way to achieve that goal.

Upvotes: 1

Related Questions