Skip to content

Instantly share code, notes, and snippets.

View mminer's full-sized avatar

Matthew Miner mminer

View GitHub Profile
@mminer
mminer / EventMonitor.swift
Last active October 18, 2017 04:54
Wrapper around NSEvent's global event monitor.
import AppKit
class EventMonitor {
private let handler: (NSEvent) -> Void
private let mask: NSEvent.EventTypeMask
private var monitor: Any?
init(mask: NSEvent.EventTypeMask, handler: @escaping (NSEvent) -> Void) {
self.handler = handler
@mminer
mminer / homeDirectory.swift
Last active September 17, 2016 19:15
Finds the user's home directory inside a sandboxed Cocoa application.
import Foundation
let homeDirectory: String = {
let home = getpwuid(getuid()).pointee.pw_dir!
return FileManager.default.string(withFileSystemRepresentation: home, length: Int(strlen(home)))
}()
@mminer
mminer / SparkleCleanup.swift
Last active December 14, 2016 01:30
Removes old Sparkle updates that weren't removed properly.
import Foundation
struct SparkleCleanup {
private static let oldDownloadAge: TimeInterval = 86400 // 24 hours
/// The location where Sparkle caches file downloads.
private static var cacheURL: URL {
guard let bundleIdentifier = Bundle.main.bundleIdentifier else {
fatalError("Unable to get bundle identifier.")
@mminer
mminer / URLSchemeHandler.swift
Last active October 18, 2017 04:53
Registers a Cocoa application to handle a custom URL scheme (e.g. someapp://action).
import Foundation
class URLSchemeHandler {
private let urlHandler: (URL) -> Void
init(urlHandler: @escaping (URL) -> Void) {
self.urlHandler = urlHandler
NSAppleEventManager.shared().setEventHandler(
@mminer
mminer / ScreenshotManager.swift
Last active May 23, 2018 22:28
Saves screenshots in a Cocoa application (wrapper around screencapture).
import AppKit
struct ScreenshotManager {
/// Matches macOS' screenshot filename format.
private static let filenameFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "'Screen Shot' yyyy-MM-dd 'at' HH.mm.ss"
return formatter
}()
@mminer
mminer / isPortInUse.swift
Last active September 17, 2016 19:33
Determines whether an HTTP port is being used by a process.
import Foundation
func isPortInUse(_ port: Int) -> Bool {
// Use netstat to find ports in use then grep for one we're interested in.
// See this solution for piping shell commands: http://stackoverflow.com/a/16650638
let process = Process()
process.launchPath = "/bin/sh"
process.arguments = ["-c", "netstat -an | grep '\\b\(port)\\b' | grep LISTEN"]
process.standardOutput = Pipe()
process.launch()
@mminer
mminer / iso8601Formatter.swift
Last active October 18, 2017 04:50
Swift ISO 8601 date formatter.
import Foundation
let iso8601Formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
return formatter
}()
@mminer
mminer / addToSidebar.swift
Last active October 18, 2017 04:49
Adds a folder to the Finder sidebar.
import Foundation
func addToSidebar(path: String) {
guard
let listType = kLSSharedFileListFavoriteItems?.takeUnretainedValue(),
let itemList = LSSharedFileListCreate(nil, listType, nil)?.takeUnretainedValue(),
let items = LSSharedFileListCopySnapshot(itemList, nil)?.takeUnretainedValue()
else {
return
}
@mminer
mminer / OutlineViewDelegate.swift
Last active January 20, 2019 18:30
Delegate for NSOutlineView that allows specifying an item right-click menu.
import AppKit
protocol OutlineViewDelegate {
func outlineView(outlineView: NSOutlineView, menuForItem item: Any) -> NSMenu?
}
extension OutlineViewDelegate {
func outlineView(outlineView: NSOutlineView, menuForItem item: Any) -> NSMenu? {
@mminer
mminer / VerticallyCenteredTextFieldCell.swift
Last active October 18, 2017 04:47
Vertically centred NSTextFieldCell.
import AppKit
class VerticallyCenteredTextFieldCell: NSTextFieldCell {
// Adapted from http://stackoverflow.com/a/8626071
override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
let adjustedFrame = adjusted(frame: cellFrame)
super.drawInterior(withFrame: adjustedFrame, in: controlView)
}