2010-08-31 24 views
6

Trong ứng dụng của tôi, tôi muốn được thông báo nếu có nội dung nào đó được thêm vào NSPasteboard. Nếu tôi sao chép một văn bản từ bất kỳ chương trình nào khác, tôi muốn ứng dụng của tôi biết về nó.Nhận thông báo khi có nội dung nào đó được thêm vào NSPasteboard

Một nơi nào đó tôi đọc không thể thực hiện theo cách đó. Tôi nên tạo một bộ đếm thời gian và kiểm tra nội dung của NSPasteboard.

Đây có phải là cách để làm không? Hoặc có bất kỳ loại thông báo nào không?

Trả lời

11

Có, về cơ bản bạn phải thăm dò ý kiến ​​của các bảng để xem nội dung của nó có thay đổi hay không. Nó không phải là lý tưởng, nhưng nó có thể. Về cơ bản, bạn có bộ hẹn giờ kích hoạt một hoặc hai lần một giây và kiểm tra số -[NSPasteboard changeCount]. Nếu các thay đổi changeCount, điều đó có nghĩa là nội dung của bảng đã thay đổi (hoặc có ít nhất một chủ sở hữu mới).

+0

Argh, nhận tôi khi đang nhập ... Vâng, để thêm vào bài đăng, bạn có thể thiết lập một số mã như thế này (http://pastie.org/1129293) để xem các thay đổi. –

3

Dựa trên câu trả lời cung cấp bởi Dave DeLong tôi đã đưa ra thực hiện tương tự nhưng trong nhanh chóng, đây là liên kết để ý chính của nó: PasteboardWatcher.swift

đoạn mã từ cùng:

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 : trong mã được chia sẻ Tôi đang cố xác định xem người dùng đã sao chép url tệp hay chưa, mã được cung cấp có thể dễ dàng được sửa đổi cho các mục đích chung khác của .

Các vấn đề liên quan