Get notified when pasteboard did change
Observe NSPasteboard — Swift 4
How to listen for clipboard changes
--
Today I was thinking about apps like Clippy and other clipboard managers, I thought: “For sure swift provides notification for the copy event”
WRONG! I sadly discovered that on MacOS unlike on iOS you can’t listen for NSClipboard
changes.
So I wanted to write a small snippet of code that provides a notification when the clipboard changes.
The Problem
The only solution is “polling” but what does that mean?
You need to check changeCount
and if it’s changed send a notification, let’s see how:
Easy right? Then to get notified you can listen as always via the NotificationCenter
like shown below:
That’s it! Quick and easy!
If you just want to play check out the example project on my Github:
You might find interesting also: