Skip to content

Instantly share code, notes, and snippets.

@ts95
Last active February 14, 2022 19:49
Show Gist options
  • Save ts95/300c5a815393087c72cc to your computer and use it in GitHub Desktop.
Save ts95/300c5a815393087c72cc to your computer and use it in GitHub Desktop.

Revisions

  1. ts95 revised this gist Nov 17, 2015. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions ScreenshotDetector.swift
    Original file line number Diff line number Diff line change
    @@ -53,8 +53,7 @@ class ScreenshotDetector: NSObject, NSMetadataQueryDelegate {
    // In a view controller for instance it should be allocated
    // in the class scope. If its reference is stored inside
    // a method it will be deallocated automatically when the
    // method returns and consequently it will stop obsering
    // any events.
    // method returns and consequently stop obsering any events.
    let detector = ScreenshotDetector()

    // The actual callback can be assigned anywhere there's
  2. ts95 revised this gist Nov 17, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion ScreenshotDetector.swift
    Original file line number Diff line number Diff line change
    @@ -53,7 +53,8 @@ class ScreenshotDetector: NSObject, NSMetadataQueryDelegate {
    // In a view controller for instance it should be allocated
    // in the class scope. If its reference is stored inside
    // a method it will be deallocated automatically when the
    // method returns, and it will stop obsering events.
    // method returns and consequently it will stop obsering
    // any events.
    let detector = ScreenshotDetector()

    // The actual callback can be assigned anywhere there's
  3. ts95 revised this gist Nov 17, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ScreenshotDetector.swift
    Original file line number Diff line number Diff line change
    @@ -59,5 +59,5 @@ let detector = ScreenshotDetector()
    // The actual callback can be assigned anywhere there's
    // a reference to the instance.
    detector.newFileCallback = { fileURL in
    // fileURL is a NSURL instance of the screenshot file.
    // fileURL is an NSURL instance of the screenshot file.
    }
  4. ts95 created this gist Nov 17, 2015.
    63 changes: 63 additions & 0 deletions ScreenshotDetector.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    import Foundation

    typealias NewFileCallback = (fileURL: NSURL) -> Void

    class ScreenshotDetector: NSObject, NSMetadataQueryDelegate {

    let query = NSMetadataQuery()

    var newFileCallback: NewFileCallback?

    override init() {
    super.init()

    let center = NSNotificationCenter.defaultCenter()
    center.addObserver(self, selector: Selector("queryUpdated:"), name: NSMetadataQueryDidStartGatheringNotification, object: query)
    center.addObserver(self, selector: Selector("queryUpdated:"), name: NSMetadataQueryDidUpdateNotification, object: query)
    center.addObserver(self, selector: Selector("queryUpdated:"), name: NSMetadataQueryDidFinishGatheringNotification, object: query)

    query.delegate = self
    query.predicate = NSPredicate(format: "kMDItemIsScreenCapture = 1")
    query.startQuery()
    }

    deinit {
    query.stopQuery()
    }

    func queryUpdated(notification: NSNotification) {
    if let userInfo = notification.userInfo {
    for v in userInfo.values {
    let items = v as! [NSMetadataItem]
    if items.count > 0 {
    let item = items[0]
    if let filename = item.valueForAttribute("kMDItemFSName") as? String {
    let filenameWithPath = NSString(string: "~/Desktop/" + filename).stringByExpandingTildeInPath
    let url = NSURL(fileURLWithPath: filenameWithPath, isDirectory: false)
    if let cb = self.newFileCallback {
    cb(fileURL: url)
    }
    }
    }
    }
    }
    }
    }

    ////////////////////////////
    // Usage //
    ////////////////////////////

    // Because this object is observing events it should stay
    // in memory for the duration of its usage.
    // In a view controller for instance it should be allocated
    // in the class scope. If its reference is stored inside
    // a method it will be deallocated automatically when the
    // method returns, and it will stop obsering events.
    let detector = ScreenshotDetector()

    // The actual callback can be assigned anywhere there's
    // a reference to the instance.
    detector.newFileCallback = { fileURL in
    // fileURL is a NSURL instance of the screenshot file.
    }