2

I'd like to do something whenever clipboard content changes. I have a solution working now where I get the clipboard value and store it, then I start an infinite loop where I look up the clipboard again, see if it's changed, then sleep for 0.5s and do it all over again.

This definitely works, but I want to do this better! Would prefer if I can listen for a notification that just tells me when the clipboard changes. There's a UIPasteboardChanged event in UIKit that lets you do this, but would prefer not needing to write this in Swift code to accomplish my goal. :-)

Any ideas on how to do this in Applescript/Automator/Python? Thanks!

4
  • Is Swift/Objective C/Cocca in Apple Script cheating?
    – JBis
    Apr 26, 2018 at 21:20
  • that'd still be helpful to me if you had some sample code! I suspect that'd still be simpler than making a full dedicated Swift app, though at this point it's looking like I might have to go that route.
    – Chris
    Apr 27, 2018 at 22:40
  • Not experienced in those languages, but am skilled in Apple Script an I know there’s a way to do use those languages. I left the question for someone else to pickup.
    – JBis
    Apr 27, 2018 at 22:43
  • Unfortunately, no answer as been found. Heres how to do it in swift.
    – JBis
    Jun 12, 2018 at 4:00

1 Answer 1

1

I ended up solving this by using Objective C bindings for Python. So not really an AppleScript answer. It's still polling based, but it's about 1000x more CPU efficient than calling out to pbpaste from the shell.

After installing the AppKit python bindings, I can fetch the current pasteboard like this:

from AppKit import NSPasteboard, NSStringPboardType
NSPasteboard.generalPasteboard().stringForType_(NSStringPboardType)

I run this in a tight loop, polling every second, and its CPU usage is quite negligible.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .