简体   繁体   English

在将某些内容添加到NSPasteboard时收到通知

[英]Get notified when something is added to NSPasteboard

In my application I want to get notified if something is added into the NSPasteboard. 在我的应用程序中,我希望在NSPasteboard中添加一些内容时收到通知。 If I copy an text from any other program I want my application to know about it. 如果我从任何其他程序复制文本,我希望我的应用程序知道它。

Somewhere I read it can't be done that way. 在某处我读到它不可能那样做。 I should create a timer and check the content of the NSPasteboard myself. 我应该创建一个计时器并自己检查NSPasteboard的内容。

Is this the way to do? 这是这样做的吗? Or are there any kind of notifications? 或者有任何类型的通知吗?

Yes, you essentially have to poll the pasteboard to see if its contents have changed. 是的,您基本上必须轮询粘贴板以查看其内容是否已更改。 It's not ideal, but it's possible. 这不是理想的,但它是可能的。 Basically, you have a timer that fires once or twice a second and checks the -[NSPasteboard changeCount] . 基本上,你有一个计时器,每秒触发一次或两次并检查-[NSPasteboard changeCount] If the changeCount changes, that means the contents of the pasteboard have also changed (or there's at least a new owner). 如果changeCount更改,则表示粘贴板的内容也已更改(或者至少有新的所有者)。

Based on answer provided by Dave DeLong I came up with similar implementation but in swift, here is the link to its gist: PasteboardWatcher.swift 基于Dave DeLong提供的答案,我提出了类似的实现,但是在swift中,这里是它的主旨的链接: PasteboardWatcher.swift

Code snippet from same: 相同的代码片段:

class PasteboardWatcher : NSObject {

    // assigning a pasteboard object
    private let pasteboard = NSPasteboard.generalPasteboard()

    // to keep track of count of objects currently copied
    // also helps in determining if a new object is copied
    private var changeCount : Int

    // used to perform polling to identify if url with desired kind is copied
    private var timer: NSTimer?

    // the delegate which will be notified when desired link is copied
    var delegate: PasteboardWatcherDelegate?

    // the kinds of files for which if url is copied the delegate is notified
    private let fileKinds : [String]

    /// initializer which should be used to initialize object of this class
    /// - Parameter fileKinds: an array containing the desired file kinds
    init(fileKinds: [String]) {
        // assigning current pasteboard changeCount so that it can be compared later to identify changes
        changeCount = pasteboard.changeCount

        // assigning passed desired file kinds to respective instance variable
        self.fileKinds = fileKinds

        super.init()
    }
    /// starts polling to identify if url with desired kind is copied
    /// - Note: uses an NSTimer for polling
    func startPolling () {
        // setup and start of timer
        timer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("checkForChangesInPasteboard"), userInfo: nil, repeats: true)
    }

    /// method invoked continuously by timer
    /// - Note: To keep this method as private I referred this answer at stackoverflow - [Swift - NSTimer does not invoke a private func as selector](http://stackoverflow.com/a/30947182/217586)
    @objc private func checkForChangesInPasteboard() {
        // check if there is any new item copied
        // also check if kind of copied item is string
        if let copiedString = pasteboard.stringForType(NSPasteboardTypeString) where pasteboard.changeCount != changeCount {

            // obtain url from copied link if its path extension is one of the desired extensions
            if let fileUrl = NSURL(string: copiedString) where self.fileKinds.contains(fileUrl.pathExtension!){

                // invoke appropriate method on delegate
                self.delegate?.newlyCopiedUrlObtained(copiedUrl: fileUrl)
            }

            // assign new change count to instance variable for later comparison
            changeCount = pasteboard.changeCount
        }
    }
}

Note: in the shared code I am trying to identify if user has copied a file url or not, the provided code can easily be modified for other general purposes. 注意:在共享代码中,我试图识别用户是否复制了文件URL,可以轻松修改提供的代码以用于其他一般目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM