Get notified when pasteboard did change

Observe NSPasteboard — Swift 4

How to listen for clipboard changes

Federico Vitale
1 min readMay 22, 2019

--

Photo by Max Nelson on Unsplash

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:

--

--

Federico Vitale
Federico Vitale

Written by Federico Vitale

Software Engineer. Currently into Typescript, Rust and go

No responses yet