How to handle selections in PDFView?

Hey,


I'm working on a project including the PDFView class. In my project I want to get the current selection whenever a new (complete) selection in the PDF document occurs. Easier said then done: PDFView has a selectionChanged notification, however it keeps firing while I move the cursor to finish the selection. I just want the text covered by the selection after the mouse button is released. I tried to use mouseUp and mouseDown but it seems as if only mouseUps are recognized on the PDFView.


Here is my code:


import Cocoa
import Quartz
class ViewController: NSViewController {
   
    @IBOutlet weak var noteView: NSScrollView!
    @IBOutlet weak var pdfView: PDFView!
    @IBOutlet weak var pdfThumb: PDFThumbnailView!
   
    var mouseU = true
    var mouseDow = false
   
    @IBAction func mark(sender: AnyObject) {
       
    }
    @IBAction func choose(sender: AnyObject) {
   
    }
   
    @IBAction func openPDF(sender: AnyObject){
        importFile()
    }
   
    override func viewDidLoad() {
        super.viewDidLoad()
       
        pdfThumb.setPDFView(self.pdfView)
       
        let nc = NSNotificationCenter.defaultCenter()
        nc.addObserver(self, selector: "selectionChanged", name: PDFViewSelectionChangedNotification, object: nil)
    }
   
    override var representedObject: AnyObject? {
        didSet {}
    }
   
    override func mouseUp(theEvent: NSEvent) {
        mouseU = true
        mouseDow = false
        print("up", terminator: "")
    }
   
    override func mouseDown(theEvent: NSEvent) {
        mouseU = false
        mouseDow = true
        print("down", terminator: "")
    }
   
   
    func importFile(){
        let fileChooser: NSOpenPanel = NSOpenPanel()
       
        fileChooser.allowsMultipleSelection = false
        fileChooser.canChooseFiles = true
        fileChooser.canChooseDirectories = false
       
        fileChooser.runModal()
       
        let chosenFile = fileChooser.URL
       
        if (chosenFile != nil){
           
            let pdfFile = PDFDocument(URL: chosenFile)
            self.pdfView.setDocument(pdfFile)
        }
    }
   
    func selectionChanged(){
        if (!mouseDow && mouseU){
            let currentSelection = pdfView.currentSelection()
           
            if(currentSelection != nil){
               print(currentSelection.string(), terminator: "")
            }
        }
    }
}


Anyone worked with PDFView and selections before? I would really appreaciate a solution.


Cheers

Steffen

How to handle selections in PDFView?
 
 
Q