Skip to content

Instantly share code, notes, and snippets.

@douglashill
douglashill / App icons.jstalk
Created September 14, 2013 16:55
Commands Acorn to export an iOS app icon in all useful sizes for iOS 6 and iOS 7. As written, this script assumes there is a square image named icon.acorn on my desktop, and writes the output files to the desktop. Flattening is performed inside the loop because it did not work when I put it outside. No changes are made to the original file, alth…
var sizes = [29, 40, 50, 57, 58, 72, 76, 80, 100, 114, 120, 144, 152, 1024];
var acorn = JSTalk.application("Acorn");
var doc = acorn.open("/Users/Douglas/Desktop/icon.acorn");
for (idx = 0; idx < sizes.length; idx++) {
doc.flattenImage();
doc.scaleImageToWidth(sizes[idx]);
doc.dataRepresentationOfType("public.png").writeToFile("/Users/Douglas/Desktop/icon-" + sizes[idx] + ".png");
doc.undo();
@douglashill
douglashill / gist:6607468
Last active December 23, 2015 08:28 — forked from alistra/gist:5099553
NSStringFromCATransform3D
NSString * NSStringFromCATransform3D(CATransform3D t)
{
return [NSString stringWithFormat:@"CATransform3D {\n% 7.2f % 7.2f % 7.2f % 7.2f\n% 7.2f % 7.2f % 7.2f % 7.2f\n% 7.2f % 7.2f % 7.2f % 7.2f\n% 7.2f % 7.2f % 7.2f % 7.2f\n}", t.m11, t.m12, t.m13, t.m14, t.m21, t.m22, t.m23, t.m24, t.m31, t.m32, t.m33, t.m34, t.m41, t.m42, t.m43, t.m44];
}
@douglashill
douglashill / ShakeApplication.m
Created November 18, 2013 11:53
For detecting when an iOS device is shaken when the responder chain isn’t working for you. Handy for debugging. Subclass UIApplication, and pass in the new class name in UIApplicationMain().
NSString * const ShakeEventNotification = @"ShakeEventNotification";
@implementation ShakeApplication
{
BOOL _shakeStarted;
}
- (void)sendEvent:(UIEvent *)event
{
[super sendEvent: event];
#! /usr/bin/python
# coding=utf-8
# http://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename-in-python
import os, os.path, re
max_length = 100
file_name = 'Untitled.txt'
@douglashill
douglashill / newnote.sh
Created January 16, 2014 11:00
Opens a text editor ready to write a note, assuming notes are stored as text files in a Notes folder in Dropbox.
#! /bin/bash
NOTE_PATH=~/Dropbox/Notes/New\ note\ `date "+%H-%M-%S"`.txt
touch "$NOTE_PATH"
open "$NOTE_PATH"
@douglashill
douglashill / UTI from filename extension.m
Last active January 3, 2016 12:58
Finds the best uniform type identifier (UTI) for a given filename extension
@import Foundation;
@import CoreServices;
int main(int argc, char *argv[]) {
@autoreleasepool {
NSString * UTI = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
(CFStringRef)@"plist",
NULL);
NSLog(@"UTI is %@", UTI);
}
@douglashill
douglashill / NSObject+AccessibilityInspect.h
Created January 19, 2014 09:47
iOS: log UIAccessibility related properties
//
// NSObject+AccessibilityInspect.h
// Created by Douglas Hill on 29/09/2012.
//
#import <Foundation/Foundation.h>
@interface NSObject (AccessibilityInspect)
- (void)logAccessibiltyProperties;
@douglashill
douglashill / NSIndexSet+DHDescription.m
Created May 9, 2014 17:28
Concise string representations of index sets, like [4-5, 16, 19-21] or alternatively [4, 5, 16, 19, 20, 21]
static NSString * DHStringFromRange(NSRange range);
@implementation NSIndexSet (DHDescription)
- (NSString *)dh_description
{
return [self dh_descriptionShowingRanges:YES];
}
- (NSString *)dh_descriptionShowingRanges:(BOOL)shouldShowAsRanges
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
NSCountedSet *set = [NSCountedSet set];
NSError *error;
NSString *corpus = [NSString stringWithContentsOfFile:@"some-file.txt" encoding:NSUTF8StringEncoding error:&error];
[corpus enumerateSubstringsInRange:NSMakeRange(0, [corpus length]) options:NSStringEnumerationByWords usingBlock:^(NSString *word, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[set addObject:word];
}];
@douglashill
douglashill / gist:644f3b468347f2bc1f77
Created June 27, 2014 18:09
A rough guess at the implementation of +[NSBundle preferredLocalizationsFromArray:]
+ (NSArray *)dh_preferredLocalizationsFromArray:(NSArray *)requestedLocalisations
{
NSMutableSet *possibleLocalisations = [NSMutableSet setWithArray:requestedLocalisations];
BOOL shouldLimitToAvailableLocalisations = YES; // Set to YES to match what NSBundle seems to do, or NO to be more like the documentation.
if (shouldLimitToAvailableLocalisations) {
[possibleLocalisations intersectSet:[NSSet setWithArray:[[NSBundle mainBundle] localizations]]];
}