Skip to content

Instantly share code, notes, and snippets.

@lancethomps
lancethomps / close_notifications_applescript.js
Last active April 27, 2025 01:40
AppleScript to close all notifications on macOS Big Sur, Monterey, Ventura, Sonoma, and Sequoia
function run(input, parameters) {
const appNames = [];
const skipAppNames = [];
const verbose = true;
const scriptName = 'close_notifications_applescript';
const CLEAR_ALL_ACTION = 'Clear All';
const CLEAR_ALL_ACTION_TOP = 'Clear';
const CLOSE_ACTION = 'Close';
@ollieatkinson
ollieatkinson / JSONObject.swift
Last active April 8, 2024 21:41
JSON Equatable in Swift
public let null = NSNull() as AnyHashable
public enum JSONObject {
case array([Any])
case dictionary([String: Any])
case fragment(Any)
}
extension JSONObject {
@inlinable public static func with(_ data: Data, options: JSONSerialization.ReadingOptions = []) throws -> JSONObject {
@ollieatkinson
ollieatkinson / OptionalProtocol.swift
Created September 9, 2020 22:04
Implementation of an Optional protocol for Swift using Swift 5.3 enum case as protocol witness
public protocol OptionalProtocol: ExpressibleByNilLiteral {
associatedtype Wrapped
var wrapped: Wrapped? { get }
static var none: Self { get }
static func some(_ newValue: Wrapped) -> Self
func map<U>(_ f: (Wrapped) throws -> U) rethrows -> U?
func flatMap<U>(_ f: (Wrapped) throws -> U?) rethrows -> U?
}
extension Optional: OptionalProtocol {
@ollieatkinson
ollieatkinson / Restorable.swift
Created June 16, 2020 08:43
Restorable - Undo/Redo management of values using Swift 5.1 property wrappers
@propertyWrapper
public struct Restorable<Value> {
public var wrappedValue: Value
public init(wrappedValue: Value, using undoManager: UndoManager = .init()) {
self.wrappedValue = wrappedValue
self.projectedValue = undoManager
}
@dabrahams
dabrahams / FactoryInitialization.swift
Last active June 5, 2024 20:44
Class factory initializers
/// Classes whose initializers actually create derived classes
protocol FactoryInitializable {
/// The type of the least-derived class declared to be FactoryInitializable.
///
/// - Warning: Do not define this in your FactoryInitializable type!
associatedtype FactoryBase: AnyObject, FactoryInitializable = Self
// This associatedtype is a trick that captures `Self` at the point where
// `FactoryInitializable` enters a class hierarchy; in other contexts, `Self`
// refers to the most-derived type.
}
@RobertAudi
RobertAudi / Int+Extenstion.swift
Last active May 2, 2020 12:07 — forked from gbitaudeau/Int+Extenstion.swift
Convert large numbers to smaller format
import Foundation
extension Int {
func abbreviate() -> String {
typealias Abbrevation = (threshold: Double, divisor: Double, suffix: String)
let abbreviations: [Abbrevation] = [
(0, 1, ""),
(1000.0, 1000.0, "K"),
(100_000.0, 1_000_000.0, "M"),
@ttscoff
ttscoff / convert_to_quiver.rb
Created April 30, 2020 15:32
Markdown files to Quiver JSON
#!/usr/bin/env ruby
require 'json'
require 'fileutils'
notebook = File.expand_path('~/Desktop/NewSnippets.qvnotebook')
FileUtils.mkdir_p(notebook)
nbmeta = {
"name" => "NewSnippets",
"uuid" => %x{uuidgen}.strip
@propertyWrapper
public struct AnyProxy<EnclosingSelf, Value> {
private let keyPath: ReferenceWritableKeyPath<EnclosingSelf, Value>
public init(_ keyPath: ReferenceWritableKeyPath<EnclosingSelf, Value>) {
self.keyPath = keyPath
}
@available(*, unavailable, message: "The wrapped value must be accessed from the enclosing instance property.")
public var wrappedValue: Value {
local CATEGORY_EX_ICES = {
"codex", "murex", "silex", "vertex", "index"
}
local CATEGORY_IX_ICES = {
"matrix", "radix", "helix"
}
local CATEGORY_UM_A = {
"baterium", "agendum", "desideratum", "erratum", "stratum", "datum", "ovum",
@tsutsu
tsutsu / README.md
Last active January 31, 2020 18:11
More Stupid Bash Tricks

More Stupid Bash Tricks

Before we get started, let's make a ~/.bash directory

Bash expects just a few files in your homedir, but is fine with these files being symlinks. As such, I instead create a subdir, ~/.bash, and then symlink ~/.bashrc to ~/.bash/bashrc and ~/.bash_profile to ~/.bash/bash_profile. This gives you a new namespace (the ~/.bash dir) to pollute with little files, which we're going to do plenty of below.