Skip to content

Instantly share code, notes, and snippets.

View drewmccormack's full-sized avatar

Drew McCormack drewmccormack

View GitHub Profile
@drewmccormack
drewmccormack / prefixer.py
Created May 10, 2011 07:25
Change the prefix of a Cocoa project
#!/usr/bin/env python
#---------
# Script to change the prefix of a framework
#---------
import os
import re
import string
@drewmccormack
drewmccormack / MCURLExtensions.m
Created July 14, 2011 13:05
NSURL category method to get the UTI of a file
@implementation NSURL (MCExtensions)
-(NSString *)UTI
{
FSRef fileRef;
Boolean isDirectory;
NSString *utiString = nil;
if ( !self.isFileURL ) return nil;
@drewmccormack
drewmccormack / NSString+MCHTMLToPlainTextConversion.h
Created October 5, 2012 14:29
Convert a NSString with HTML into a plain text string using NSXMLParser.
#import <Foundation/Foundation.h>
@interface NSString (MCHTMLToPlainTextConversion)
-(NSString *)stringByConvertingHTMLToPlainText;
@end
@drewmccormack
drewmccormack / gist:5262375
Created March 28, 2013 11:03
A category method for NSArray that facilitates enumeration with regular draining of the autorelease pool. Useful for those long, memory accumulating loops.
@implementation NSArray (AutoreleasePoolDraining)
-(void)enumerateObjectsDrainingEveryIterations:(NSUInteger)iterationsBetweenDrains usingBlock:(void (^)(id object, NSUInteger index, BOOL *stop))block
{
NSUInteger total = 0;
NSUInteger count = self.count;
NSUInteger numberOfChunks = (count / MAX(1,iterationsBetweenDrains) + 1);
BOOL stop = NO;
for ( NSUInteger chunk = 0; chunk < numberOfChunks; chunk++ ) {
@autoreleasepool {
@drewmccormack
drewmccormack / gist:6592e48c94b9aaab6678
Created May 16, 2014 08:49
Define availability macros for pre-iOS 7 and OS X 10.8
#import <Foundation/Foundation.h>
#if TARGET_OS_IPHONE && !defined(__IPHONE_7_0)
#define __IPHONE_7_0 70000
#endif
#if !TARGET_OS_IPHONE && !defined(__MAC_10_9)
#define __MAC_10_9 1090
#endif
// Dispatch
//
// Usage:
// Async dispatch to main queue
// -->block
// Async dispatch to queue
// queue --> block
// Sync dispatch to main queue
// -->|block
// Sync dispatch to queue
@drewmccormack
drewmccormack / gist:250542102aa77d700195
Created February 25, 2015 14:40
Determine if a CGImage is fully opaque
BOOL CGImageIsOpaque(CGImageRef image)
{
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
unsigned char pixelData[width * height];
CGContextRef context = CGBitmapContextCreate( pixelData, width, height, 8, width, NULL, (kCGBitmapAlphaInfoMask & kCGImageAlphaOnly) );
CGContextDrawImage( context, CGRectMake(0, 0, width, height), image );
CGContextRelease( context );
for (NSInteger i = 0; i < width * height; ++i) {
@drewmccormack
drewmccormack / jiggle-wifi.sh
Last active March 13, 2021 20:27
Keeps your OS X WiFi connected to the interwebs.
#!/usr/bin/env bash
# If your OS X WiFi connection stops working, but can be fixed by
# toggling WiFi off and then on, this script can help prevent mouse arm.
# Pings Apple's server repeatedly, toggling WiFi off/on when a connection times out.
while true ; do
curl --head --silent --connect-timeout 10 http://www.apple.com/my-wifi-keeps-dropping-out > /dev/null
if [ $? -ne 0 ] ; then
networksetup -setairportpower en1 off
networksetup -setairportpower en1 on
@drewmccormack
drewmccormack / SendFile.scpt
Created July 4, 2016 17:41
Javascript (JXA) script to send attached files one per email
// Open in Script Editor, and Export as an Application
// To use, drag files from folder onto Droplet
function openDocuments(docs) {
var mail = Application("Mail")
for (var i = 0; i < docs.length; i++) {
var doc = docs[i].toString()
var filename = doc.replace(/^.*[\\\/]/, '')
@drewmccormack
drewmccormack / verifyPaddleSignature.swift
Last active March 13, 2021 20:27
How to verify the Paddle webhook signature in Swift 3 using the Vapor packages CTLS and Crypto (include OpenSSL)
/// Verify the signature passed with the Paddle request parameters.
/// Details here: https://paddle.com/docs/reference-verifying-webhooks
func verifyPaddleSignature(inParameters parameters: [String:String]) throws -> Bool {
guard let signatureString = parameters[PaddleParameter.signature.rawValue],
let signature = Data(base64Encoded: signatureString) else {
return false
}
// Need to gather sorted parameters
var signatureParameters = parameters