0

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?

Kaatt
  • 1,061
  • 1
  • 8
  • 10

2 Answers2

1

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.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Well, when you select some text, right-click and select some service, OS X sends the service a NSPasteboard object containing the selected text but the generalPasteboard remains unmodified. I assume OS X switches the generalPasteboard to a custom one, presses CmdC, and reverts the generalPasteboard. Follow up question: http://stackoverflow.com/q/28786776/4515130 – Kaatt Feb 28 '15 at 20:55
  • No. There's no simulated Command-C or copy. There are specific methods, such as `-writeSelectionToPasteboard:`, called on the items in the responder chain to supply the data and handle any returned data. The methods are passed the pasteboard to work with. There's no need for Cocoa to return a different pasteboard from `+generalPasteboard`. – Ken Thomases Feb 28 '15 at 21:19
  • Can I programatically invoke `writeSelectionToPasteboard`? – Kaatt Feb 28 '15 at 21:30
  • This link says its deprecated https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Protocols/NSServicesRequests_Protocol/#//apple_ref/occ/instm/NSObject/writeSelectionToPasteboard:types: – Kaatt Feb 28 '15 at 21:34
  • First, a minor correction to my comment: the method is `-writeSelectionToPasteboard:types:`. I accidentally left of the `types:` part before. I don't know why the documentation says it's deprecated. The headers don't indicate that. You can invoke it on an object in your own app that you have checked implements it, but why would you want to? You still haven't explained what you're trying to achieve (don't discuss in terms of the implementation you think you should use; explain in terms of high-level concepts, goals, and effects). – Ken Thomases Feb 28 '15 at 21:46
  • I need to get the selected text in my app. AX APIs don't support all apps. Pasteboard hack (backup, copy, restore) is a hack. Services work perfect but only when the user invokes it. I want to invoke it programatically. – Kaatt Feb 28 '15 at 21:53
  • Do you mean you want your app to get the selected text *of the active app, even if that app is not your app*? Or you do mean you want to get the text which is selected within some view within your app? – Ken Thomases Feb 28 '15 at 21:56
  • The active app, _only_ =p when that app is not mine. My app runs in the background – Kaatt Feb 28 '15 at 21:59
  • No, there's no reliable way to get the selected text from another app nor initiate invocation of a service by that other app. There are only various hacks or incomplete solutions (like Accessibility). – Ken Thomases Feb 28 '15 at 22:02
0

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.