Determine the source application of current pasteboard content
Asked Answered
C

2

6

Several OSX clipboard managers from AppStore show the ability to determine the source-application of content that was copied to the clipboard.

I am writing some simple clipboard observer and would like to show the source-application icon near the content, stored in general NSPasteboard. And I would like to know how this can be achieved.

As far as I can see, NSPasteboard doesn't provide any additional info except types of data and data itself.

Maybe there are some events or notifications to know that a 'copy' command was triggered? Or some other ways?

Conductive answered 1/5, 2013 at 19:35 Comment(2)
Several OSX clipboard managers = CopyLess. This is the only app that does the trick as far as I can see.Conductive
Maybe they use AppleScript somehow?Conductive
H
6

I believe that the way CopyLess and Alfred 2 (which also supports this) work is that they have a timer that checks the clipboard for changes. When they see a change on the clipboard they get the active application and associate that app with the change. It's likely not a foolproof method but should work in most cases.

Hoofbound answered 1/5, 2013 at 23:28 Comment(1)
Thanks @Dustin. I ended up with the same conclusion.Conductive
A
2

There's a new quasi-standard for this now (as of Dec 2016), see http://nspasteboard.org

It works as follows:

  • By default, a program that records the clipboard contents shall assume that the frontmost application is the one that filled the clipboard (which, admittedly, only works if the program is already running when the clipboard content is set).

  • But if there's a clipboard flavor (kind) of type org.nspasteboard.source, then this flavor's content shall be the Bundle ID of the app that filled the clipbard.

(This is a quasi-standard because it has been discussed and agreed upon by interested members of a mailing list managed by nspasteboard.org)

Affranchise answered 4/1, 2017 at 15:43 Comment(2)
Somebody, show that to 1Password developers :) I mean the org.nspasteboard.source for their menubar agent.Conductive
The 1pw devs are on said mailing list. Someone just has to convince them to implement the new protocol.Affranchise

© 2022 - 2024 — McMap. All rights reserved.