Skip to content

Instantly share code, notes, and snippets.

View stephancasas's full-sized avatar

Stephan Casas stephancasas

View GitHub Profile
@stephancasas
stephancasas / NSViewReader.swift
Last active April 14, 2024 16:19
A SwiftUI view that can reliably access outer or inner instances of SwiftUI-managed NSView objects.
//
// NSViewReader.swift
//
// Created by Stephan Casas on 4/11/24.
//
import SwiftUI;
import AppKit;
import Combine;
@stephancasas
stephancasas / NSManagedObject+DynamicBindings.swift
Created March 31, 2024 21:07
Syntactically-clean and portable SwiftUI bindings for optional properties on CoreData entities.
//
// NSManagedObject+DynamicBindings.swift
//
// Created by Stephan Casas on 3/31/24.
//
import SwiftUI;
import CoreData;
protocol DynamicBindings: NSManagedObject { }
@stephancasas
stephancasas / DumbHTTPServer.swift
Created March 29, 2024 16:28
A low-level HTTP server offering a SwiftUI-observable lifecycle.
//
// DumbHTTPServer.swift
// DumbHTTPServer
//
// Created by Stephan Casas on 3/29/24.
//
import SwiftUI;
import Combine;
@stephancasas
stephancasas / NSApplication+NSResponderDebug.swift
Created March 18, 2024 20:35
An extension on NSApplication providing a computed property that describes the current responder chain.
//
// NSApplication+NSResponderDebug.swift
//
// Created by Stephan Casas on 3/18/24.
//
import Cocoa;
extension NSApplication {
@stephancasas
stephancasas / CGEventSupervisor+Convenience.swift
Created March 18, 2024 18:46
A convenience extension for CGEventSupervisor which offers built-in discriminator types for keyboard events.
//
// CGEventSupervisor+Convenience.swift
//
// @see: https://github.com/stephancasas/CGEventSupervisor
//
// @note: This is provided as an extension because it is poorly-structured as a result of
// backward-compatibility consideration. There are several ways it can be improved.
//
// Created by Stephan Casas on 3/18/24.
//
@stephancasas
stephancasas / http-server.jxa.js
Created March 12, 2024 16:29
A basic HTTP server built in JXA for macOS
#!/usr/bin/env osascript -l JavaScript
ObjC.bindFunction('malloc', ['void*', ['int']]);
ObjC.bindFunction('memset', ['void*', ['void*', 'int', 'int']]);
ObjC.bindFunction('listen', ['int', ['int', 'int']]);
ObjC.bindFunction('socket', ['int', ['int', 'int', 'int']]);
ObjC.bindFunction('accept', ['int', ['int', 'void*', 'void*']]);
ObjC.bindFunction('bind', ['int', ['int', 'void*', 'int']]);
@stephancasas
stephancasas / NSView+DebugBorder.swift
Created March 4, 2024 23:50
Quickly apply a 1-pixel debugging border to NSView instances.
extension NSView {
var debugBorder: NSColor? {
set {
guard let newValue = newValue else {
self.layer?.borderColor = nil;
self.layer?.borderWidth = 0;
return;
}
@stephancasas
stephancasas / ProcessSerialNumber.swift
Created March 2, 2024 21:20
How 'bout I do anyway? 😒
//
// ProcessSerialNumber.swift
//
// Created by Stephan Casas on 2/28/24.
//
extension ProcessSerialNumber {
/// Create a new `ProcessSerialNumber` using the given process identifier.
init?(_ pid: pid_t) {
@stephancasas
stephancasas / EquatableProcess.swift
Created March 2, 2024 19:39
A type which records both the process identifier and the time at which the represented instance was dispatched.
//
// EquatableProcess.swift
//
// Created by Stephan Casas on 3/2/24.
//
/// A value type which records both the process identifier and the time at which the
/// represented instance was dispatched — providing a unique identity for a single
/// application instance.
struct EquatableProcess: Identifiable, Hashable {
@stephancasas
stephancasas / NSApplication+openSettings.swift
Last active December 5, 2023 03:22
An extension enabling global access to settings scene of a macOS SwiftUI application.
//
// NSApplication+openSettings.swift
//
// Created by Stephan Casas on 12/3/23.
//
import SwiftUI;
fileprivate let kAppMenuInternalIdentifier = "app"
fileprivate let kSettingsLocalizedStringKey = "Settings\\U2026";