Skip to content

Instantly share code, notes, and snippets.

@ahknight
ahknight / fix_timemachine_backup.sh
Last active August 9, 2023 20:25
When Time Machine asks you to delete your backups and start over, ignore it. Run this and then start a backup again.
#!/bin/bash -x
# Generally based on ideas found at:
# http://www.garth.org/archives/2011,08,27,169,fix-time-machine-sparsebundle-nas-based-backup-errors.html
#
# Reduced the ideas there down to their essentials.
# 1. Unlock the image.
# 2. Reset the saved failure in the backup metadata.
# 3. Verify/fix the filesystem.
@ahknight
ahknight / LineParser.swift
Created January 27, 2018 15:19
Iterates over a large string line-by-line, with configurable line endings. Uses Swift 4's Substring for efficiency.
//
// LineParser.swift
// Mail
//
// Created by Adam Knight on 1/26/18.
//
import Foundation
public class LineParser: Sequence, IteratorProtocol {
@ahknight
ahknight / UIFont+Sanity.m
Created August 27, 2014 17:32
Using custom fonts with dynamic type without jumping through a thousand hoops.
@interface UIFont (Sanity)
+(UIFont*)preferredFontForTextStyle:(NSString *)style withFontFamily:(NSString*)family;
@end
@implementation UIFont (Sanity)
+(UIFont*)preferredFontForTextStyle:(NSString*)style withFontFamily:(NSString*)family
{
UIFont *font = nil;
UIFontDescriptor *descriptor = nil;
@ahknight
ahknight / dotfiles.sh
Last active May 5, 2017 18:09
Manages a dotfiles repo with global and host-specific settings.
#!/bin/bash
HOSTNAME=$(hostname -f)
GREEN=$( tput setaf 10 )
CYAN=$( tput setaf 14 )
RED=$( tput setaf 1 )
GRAY=$( tput setaf 8 )
RESET=$( tput setaf 7 )
NAME=$( basename "$0" )
@ahknight
ahknight / sudoish.sh
Last active May 5, 2017 18:04
sudoish - Authenticate shell commands with the system authentication dialog, including using Touch ID on MacBook Pros
#!/bin/sh
CMD=()
for i in "$@"; do
if [[ $i =~ "[[:space:]]" ]]; then
CMD+=(\\\"$i\\\")
else
CMD+=($i)
fi
done
@ahknight
ahknight / notification-center.py
Created April 25, 2013 15:18
A simple Python script that lets you post to the notification center from the command line without dealing with a full desktop app. Because I can.
#!/usr/bin/python
# Copyright (c) 2013 Adam Knight
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
@ahknight
ahknight / add_fonts_to_info_plist.sh
Created February 19, 2014 17:33
Adds all given files to the given plist as properties of UIAppFonts (iOS).
#!/bin/bash
if [ -z $1 ]; then
echo "usage: `basename $0` /full/path/to/MYPROJ-Info.plist font1.(ttf|otf) [font2 font3 ...] #names of font files"
exit 1
fi
PLIST=$1
shift
FONTS=$@
@ahknight
ahknight / symkvs.py
Created January 3, 2014 21:27
Using symlinks as a key-value store. Great for multiprocessing. Sample subclass to keep links to unique files, though there are other uses (considering values only go up to PATH_MAX (usually 1024)).
#!/usr/bin/env python
import os
from errno import *
class SymlinkKVS(object):
def __init__(self, storepath):
self.storepath = storepath
def __delitem__(self, key):
@ahknight
ahknight / searchmail.sh
Created December 30, 2013 18:05
Presuming you keep an archive of your email in a local maildir and presuming you're wise enough to have found mu (it's in homebrew), this script searches for messages and opens up mutt with the results (also in homebrew).
#!/bin/bash
MAILDIR="~/Maildir"
RESULTS="~/Maildir/.SearchResults"
mu find -o links --linksdir="${RESULTS}" --clearlinks $@ && mutt -f "${RESULTS}"
@ahknight
ahknight / clang_diagnostic.c
Last active December 26, 2015 13:59
For when you really do know what you're doing...
#define _HELPER(x) #x
#define START_IGNORE_DIAGNOSTIC(diag) _Pragma("clang diagnostic push"); _Pragma(_HELPER(clang diagnostic ignored #diag));
#define STOP_IGNORE_DIAGNOSTIC _Pragma("clang diagnostic pop");
START_IGNORE_DIAGNOSTIC(-Wsomething-stupid)
somethingSeeminglyStupid();
STOP_IGNORE_DIAGNOSTIC