Skip to content

Instantly share code, notes, and snippets.

@davedelong
davedelong / UpdateRepositories.swift
Last active January 30, 2017 12:43
A headless Swift program that keeps a directory of git repositories up-to-date
#!/usr/bin/swift
import Foundation
func scanForRepositories(directory: NSURL, root: NSURL) {
let fileManager = NSFileManager.defaultManager()
let options: NSDirectoryEnumerationOptions = .SkipsSubdirectoryDescendants | .SkipsPackageDescendants
if let contents = fileManager.contentsOfDirectoryAtURL(directory, includingPropertiesForKeys: [NSURLIsDirectoryKey], options: options, error: nil) {
let urls = contents as Array<NSURL>
@davedelong
davedelong / autologin.sh
Last active August 10, 2016 13:03
Programmatically switch to another user
#!/bin/bash
# A script to automatically switch to another user in macOS
# Invoke like this: `autologin.sh {user} {password}`
uid=`id -u $1`
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID "$uid"
osascript <<EOD
### Keybase proof
I hereby claim:
* I am davedelong on github.
* I am davedelong (https://keybase.io/davedelong) on keybase.
* I have a public key ASBPhXPG-DfGDYTWePezV32Zhv_iOJVF2cqJVVvmtzdCHQo
To claim this, I am signing this object:
@davedelong
davedelong / Collection.swift
Created June 14, 2017 16:44
Grouping collections
public extension Collection {
func keyedBy<T>(_ keyer: (Iterator.Element) -> T?) -> Dictionary<T, Iterator.Element> {
var d = Dictionary<T, Iterator.Element>()
for item in self {
if let key = keyer(item) {
d[key] = item
}
}
return d
@davedelong
davedelong / Collapsing.md
Last active February 20, 2018 22:09 — forked from joyrexus/README.md
collapsible markdown

collapsible markdown?

CLICK ME

yes, even hidden code blocks!

print("hello world!")
@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
@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 / 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 / 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 / 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("""
┏━━┓
┃ ┃
┣━━┫