Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / encoder.py
Created August 21, 2014 13:56
Simple base converter for Py
#!/usr/bin/env python3
ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
def encode(n):
result = ""
while (n > 0):
n,m = divmod(n,len(ALPHABET))
result = ALPHABET[m] + result
return result
@ahknight
ahknight / uicolor+hex.m
Created August 18, 2014 14:52
Hex to UIColor
#define UICOLOR_FROM_HEX(x) [UIColor colorWithRed:(((NSUInteger)(x & 0xff0000) >> 16)/255.) \
green:(((NSUInteger)(x & 0x00ff00) >> 8)/255.) \
blue:(((NSUInteger)(x & 0x0000ff))/255.) \
alpha:1.0]
UIColor *color = UICOLOR_FROM_HEX(0xff00ff);
@ahknight
ahknight / wwdc.py
Last active August 29, 2015 14:05
Downloads WWDC media. Works for 2011, 2013, and 2014. 2012 seems to break on authentication, even with the ADCDownloadAuth cookie provided. Requires BeautifulSoup4 and Requests as well as the value of your ADCDownloadAuth cookie in a JSON file in the same dir as "wwdc_cookies.json".
#!/usr/bin/env python3
import argparse
import datetime
import os
import json
import requests
from bs4 import BeautifulSoup
@ahknight
ahknight / atvconvert.sh
Created March 7, 2014 15:23
Convert pretty much any video file to a format that will play on an AppleTV 3 (and maybe a 2). Only converts streams that need it.
#!/bin/bash
FILE="$1"
VCODEC=$( mediainfo --Inform="Video;%Format%" "$FILE" )
ACODEC=$( mediainfo --Inform="Audio;%Format%" "$FILE" )
echo $FILE: $VCODEC/$ACODEC
[ "$VCODEC" = "AVC" ] && VC="copy" || VC="libx264"
@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 / pdfman.sh
Created February 14, 2014 21:43
Generate a PDF of a manpage for easier/offline reading. Great for zfs/zpool manpages…
#!/bin/bash
MANFILE=$( man -w $1 $2 )
NAME=$( basename $MANFILE )
if [ -r $MANFILE ]; then
groff -mandoc $MANFILE -t | pstopdf -i -o "$NAME.pdf"
echo "wrote $NAME.pdf"
else
echo $MANFILE #Error message, probably.