7

iOS and macOS have a built in feature called Universal Clipboard that syncs the clipboard's content across devices using iCloud.

NSPasteboard.general.clearContents()
NSPasteboard.general.writeObjects("Test 123") 

I would like to write something to the general pasteboard on my Cocoa app to share it across apps without it being synced with other iCloud devices. I wasn't able to find a way to do it. In fact I think it is not possible without the user disabling it manually in the settings.

The docs say:

The general pasteboard, available by way of the general class method, automatically participates with the Universal Clipboard feature in macOS 10.12 and later and in iOS 10.0 and later. There is no macOS API for interacting with this feature. https://developer.apple.com/documentation/appkit/nspasteboard

But maybe there is a workaround, a private API (no App Store, I know) or something else someone might know about. :)

Cheers

Daniel
  • 1,473
  • 3
  • 33
  • 63

1 Answers1

7

Yes, the general pasteboard is available to all apps, but the NSPasteboard can be used to create private pasteboards. All you need to do is:

let myPasteboard = NSPasteboard(name: NSPasteboard.Name("mypasteboard"))

You can check the documentation here. Thus, you can copy the pasted item to your private pasteboard and, only when you want it, you can then transfer the data to the general pasteboard and make the data available to all apps.

However, if you want to prevent the Universal Clipboard from being shared across devices, all you have to do is:

let generalPasteboard = NSPasteboard.general

// current host only
generalPasteboard.prepareForNewContents(with: .currentHostOnly)
// write here to the pasteboard
jvarela
  • 3,744
  • 1
  • 22
  • 43
  • Thanks. That's a good solution if you don't want to share the pasteboard with other apps. In my case I want to share the pasteboard with other apps, but not not with other devices using Universal Clipboard. If anyone has a solution for that, please let us know. :) – Daniel Jul 11 '19 at 22:32
  • Then you should have said so from the beginning. Yes, there is a solution. I will edit my answer, but you should edit your question too. ;) – jvarela Jul 11 '19 at 22:40
  • That’s why I said I want to use the general pasteboard. :) Thanks for the update. I can’t believe I didn’t stumble across that function which is exactly what I was looking for. – Daniel Jul 13 '19 at 06:40
  • That is why SO exists, for a rainy day when we miss something or someone. ;) – jvarela Jul 13 '19 at 07:04