Skip to content

Instantly share code, notes, and snippets.

@ahknight
ahknight / start_socks_proxy.sh
Last active August 29, 2015 13:56
usage: start_socks_proxy.sh hostname [SSH port]
#!/bin/bash
# Using SSH, create a SOCKS4/5 proxy connection to a remote server and configure the system to use it.
_pgrep() {
P_CMD=$1
P_PID=$( ps -u $USER -o pid,command | grep "${P_CMD}" | grep -v grep | head -1 | awk '{ print $1 }' )
echo "$P_PID"
if [ -z $P_PID ]; then return 1; else return 0; fi
}
@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.
@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 / 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 / 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 / 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 / strip_alias.sh
Created March 27, 2013 19:16
Strip Alias: Takes an existing post-Lion obese alias and creates a simple alias. Released into the public domain without restriction.
#!/bin/bash
# Strip Alias
# Released into the public domain without restriction.
#
# Takes an existing post-Lion obese alias and creates a simple alias.
SRC="$1"
DST="$2"
@ahknight
ahknight / KeyObject.m
Last active December 21, 2015 22:59
Public domain. Init with a dictionary, define some properties as dynamic, serve from -valueForUndefinedKey however you wish.
#import <Foundation/Foundation.h>
#import <objc/objc.h>
#import <objc/objc-class.h>
#import <malloc/malloc.h>
#pragma mark Keyed Object
@interface KeyedObject : NSObject
@end
@ahknight
ahknight / make_ios_icons.sh
Last active December 23, 2015 10:58
Given an input image as the single argument, creates all the files needed for a Universal iOS app (in the same directory, overwriting). Uses pngcrush to compress the images, but that's easily removed/commented out.
#!/bin/bash
function make_icon {
sips -s format png -Z $3 "$1" --out "$2"
pngcrush -ow -q "$2"
}
echo "Using $1 as the base image."
echo "** App Store: iTunesArtwork **"
@ahknight
ahknight / gist:6888789
Created October 8, 2013 18:00
Forward calls for a specific delegate protocol to another class for answering.
-(id)forwardingTargetForSelector:(SEL)aSelector
{
// Do we have a scroll view delegate?
if (_scrollViewDelegate && [_scrollViewDelegate conformsToProtocol:@protocol(UIScrollViewDelegate)]) {
// Is this a selector in that protocol?
struct objc_method_description methodDescription = protocol_getMethodDescription(@protocol(UIScrollViewDelegate), aSelector, NO, YES);
if (methodDescription.name != NULL) return _scrollViewDelegate;
}
return [super forwardingTargetForSelector:aSelector];
}