Last active
October 6, 2023 14:15
-
-
Save Daemon-Devarshi/13efd24f027a775ee862 to your computer and use it in GitHub Desktop.
A class which performs polling to determine the newly copied url of desired kind in an external app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PasteboardWatcher.swift | |
// PasteboardWatcher | |
// | |
// Created by Devarshi Kulshreshtha on 6/19/15.PasteboardWatcher | |
// Copyright © 2015 Devarshi Kulshreshtha. All rights reserved. | |
// | |
import Cocoa | |
/// Protocol defining the methods which delegate has/ can implement | |
protocol PasteboardWatcherDelegate { | |
/// the method which is invoked on delegate when a new url of desired kind is copied | |
/// - Parameter copiedUrl: the newly copied url of desired kind | |
func newlyCopiedUrlObtained(copiedUrl copiedUrl : NSURL) | |
} | |
/// Use this class to notify a delegate once a url with specified kind is copied | |
/// - Credit goes to: **Josh Goebel** | |
/// - His wonderful pastie: [PasteboardWatcher in objective-c](http://pastie.org/1129293) | |
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 | |
} | |
} | |
} |
Author
Daemon-Devarshi
commented
Apr 25, 2021
via email
Hi Michael, please free to tweak it as per your needs, I have not gotten
time to keep it up to date so I believe there is scope of upgrade and
improvement.
…On Saturday, April 17, 2021, Michael Lan ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
I guess I can just avoid coercing it into an NSURL
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/13efd24f027a775ee862#gistcomment-3709156>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAHKFFTZ5KHTXSHZWAAOSX3TJDAN3ANCNFSM4NNV54LQ>
.
--
Thanks,
Devarshi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment