Skip to content

Instantly share code, notes, and snippets.

@davedelong
davedelong / NSLocale.m
Last active October 15, 2022 08:02
Locales
/*
======================================================
THIS CODE IS FOR EDUCATIONAL PURPOSES ONLY.
I'M NOT RESPONSIBLE IF YOU SHIP THIS AND IT BLOWS UP IN YOUR FACE.
IF IT DOES AND YOU COMPLAIN TO ME I WILL LAUGH AT YOU.
@davedelong
davedelong / DarkMode.applescript
Last active September 10, 2022 04:40
Toggle Dark Mode
on setDarkMode(shouldBeDark)
set paneID to "com.apple.preference.general"
tell application "System Events"
if dark mode of appearance preferences is shouldBeDark then return
end tell
set paneWasOpen to false
@davedelong
davedelong / gist:6270317
Last active May 16, 2020 02:51
A straight-forward way to inject NSViewControllers into the AppKit responder chain
@interface DDControlledView : NSView
@property (weak) IBOutlet NSViewController *viewController;
@end
@implementation DDControlledView
- (void)setNextResponder:(NSResponder *)aResponder {
NSViewController *controller = self.viewController;
@davedelong
davedelong / gist:7371853
Last active May 16, 2020 02:51
Delegate proxying (`protocol_methodForEach()` is a custom function that does just what its name implies)
@interface DDDelegateProxy : NSProxy
+ (id)proxyForDelegate:(id)delegate conformingToProtocol:(Protocol *)protocol;
@property (weak, readonly) id delegate;
@property (strong, readonly) Protocol *protocol;
/*!
* Set a default return value for a method on the delegate's protocol.
* \param value This must be a block that returns the default value. Bad Things will happen if it's not.
@davedelong
davedelong / build_script.sh
Created November 22, 2018 18:12
Auto-increment build numbers in release mode
# Automatically increment the CFBundleVersion in release builds
config=${CONFIGURATION}
if [ "${config}" = "Release" ]; then
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"
else
echo "info: Skipping build number incrementation in ${config} mode"
@davedelong
davedelong / reenumerate.m
Last active August 5, 2019 21:38
Reenumerate attached devices
#import <Foundation/Foundation.h>
#import <IOKit/IOKitLib.h>
#import <IOKit/usb/IOUSBLib.h>
int main() {
NSMutableDictionary *match = (__bridge NSMutableDictionary *)IOServiceMatching("IOUSBDevice");
if (match == nil) { return -1; }
match[@"IOPropertyMatch"] = @{@"SupportsIPhoneOS": @YES};
io_iterator_t iterator = 0;
@davedelong
davedelong / marsedit.applescript
Last active June 28, 2019 01:19
Export a blog from MarsEdit
on pad(thisNumber)
set thisText to thisNumber as text
if length of thisText = 1 then
return "0" & thisText
else
return thisText
end if
end pad
@davedelong
davedelong / Domino.swift
Created December 23, 2018 22:21
Mexican Train → given a set of dominos, find the optimal chain for a game of Mexican Train
fileprivate let tileBaseH = Array("""
┏━━┳━━┓
┃ ┃ ┃
┗━━┻━━┛
""")
fileprivate let tileBaseV = Array("""
┏━━┓
┃ ┃
┣━━┫
@davedelong
davedelong / japanese_eras.swift
Last active July 10, 2018 02:44
Japanese Eras
// obviously, we'll need a japanese calendar
let jp = Calendar(identifier: .japanese)
// this is a date formatter that, when formatting a date, gives us the full readable name of the date's era
let jpf = DateFormatter()
// see also: https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns
jpf.dateFormat = "GGGG"
jpf.calendar = jp
// ranges are numbered starting at 0
@davedelong
davedelong / Proposal.md
Last active April 3, 2018 15:33
Proposal: ContainmentSet

Adding the ContainmentSet Protocol to Swift

  • Proposal: SE-TBD
  • Author(s): Dave DeLong
  • Review manager: TBD
  • Status: TBD