Skip to content

Instantly share code, notes, and snippets.

View mminer's full-sized avatar

Matthew Miner mminer

View GitHub Profile
@mminer
mminer / formatBytes.swift
Last active April 19, 2023 00:59
Formats bytes into a more human-readable form (e.g. MB).
import Foundation
func format(bytes: Double) -> String {
guard bytes > 0 else {
return "0 bytes"
}
// Adapted from http://stackoverflow.com/a/18650828
let suffixes = ["bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
let k: Double = 1000
@mminer
mminer / NSImage+Base64.swift
Last active May 21, 2021 15:00
NSImage extension that allows initializing an image from a Base64 encoded string.
import AppKit
extension NSImage {
convenience init?(base64EncodedString: String) {
guard
let url = URL(string: base64EncodedString),
let data = try? Data(contentsOf: url)
else {
return nil
@mminer
mminer / String+Insert.swift
Last active October 18, 2017 04:45
String extension that adds a method to insert a string in the middle of it.
extension String {
func insert(_ string: String, at index: Int) -> String {
let prefix = String(characters.prefix(index))
let suffix = String(characters.suffix(characters.count - index))
return prefix + string + suffix
}
}
@mminer
mminer / NSButton+TextColor.swift
Last active September 10, 2022 23:24
NSButton extension that adds a method to change its text color.
import AppKit
extension NSButton {
func set(textColor color: NSColor) {
let newAttributedTitle = NSMutableAttributedString(attributedString: attributedTitle)
let range = NSRange(location: 0, length: attributedTitle.length)
newAttributedTitle.addAttributes([
.foregroundColor: color,
@mminer
mminer / HoverView.swift
Last active April 9, 2025 23:26
NSView subclass that applies an effect when the mouse hovers over it.
import AppKit
class HoverView: NSView {
override var wantsUpdateLayer: Bool {
return true
}
private var isMouseOver = false {
didSet {
@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)
}
@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 / 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 / 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 / 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()