Skip to content

Instantly share code, notes, and snippets.

View indragiek's full-sized avatar

Indragie Karunaratne indragiek

View GitHub Profile
@indragiek
indragiek / gist:2772551
Created May 23, 2012 00:30
Application support folder
- (NSURL *)applicationSupportDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : NSTemporaryDirectory();
NSString *path = [basePath stringByAppendingPathComponent:@"YourAppName"];
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:path isDirectory:NULL]) {
[fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}
return [NSURL fileURLWithPath:path];
@indragiek
indragiek / NSImage+PrefPane.h
Created May 24, 2012 20:42
NSImage loading in pref panes
@interface NSImage (PrefPaneExtensions)
+ (NSImage*)prefPaneImageNamed:(NSString*)name;
@end
@indragiek
indragiek / SNRRestorationManager.h
Created August 19, 2012 20:16
Simple Cocoa state restoration
//
// SNRRestorationManager.h
// Sonora
//
// Created by Indragie Karunaratne on 2012-08-19.
//
#import <Foundation/Foundation.h>
@protocol SNRRestorableState <NSObject>
@indragiek
indragiek / SNRDiscogsEngine.h
Created August 19, 2012 23:28
API wrapper for the Discogs API built on AFNetworking
//
// SNRDiscogsEngine.h
// Sonora
//
// Created by Indragie Karunaratne on 11-11-18.
//
#import <Foundation/Foundation.h>
@interface SNRDiscogsEngine : NSObject
@indragiek
indragiek / main.c
Created November 29, 2012 01:10
Simple key logger for OS X using CGEventTap
// Super simple key logger that uses a CGEventTap to log
// the unicode strings for each key down event
// Doesn't handle special keys (enter, backspace, etc.)
#include <stdio.h>
#import <Carbon/Carbon.h>
#import <ApplicationServices/ApplicationServices.h>
CGEventRef loggerCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* context)
{
//
// FGOManagedObjectContextStack.h
//
// Created by Indragie Karunaratne on 2012-12-23.
// Copyright (c) 2012 Indragie Karunaratne. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface FGOManagedObjectContextStack : NSObject
@indragiek
indragiek / gist:4659386
Last active December 11, 2015 20:59
Fix for NSSplitView's non integral drawing/frame calculations
// In an NSSplitView subclass
- (void)drawDividerInRect:(NSRect)rect
{
[super drawDividerInRect:NSIntegralRect(rect)];
}
// In NSSplitViewDelegate implementation
- (CGFloat)splitView:(NSSplitView *)splitView constrainSplitPosition:(CGFloat)proposedPosition ofSubviewAt:(NSInteger)dividerIndex
@indragiek
indragiek / gist:5200264
Created March 19, 2013 21:23
Easy Core Data fetching/inserting
- (void)fetchWithRequest:(NSFetchRequest *)request
completion:(void(^)(NSArray *results, NSError *error))handler
{
[request setResultType:NSManagedObjectIDResultType];
void (^executeHandler)(NSArray *, NSError *) = ^(NSArray *results, NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
if (handler) handler(results, error);
});
};
[self.backgroundContext performBlock:^{
@indragiek
indragiek / gist:5297435
Last active March 5, 2023 21:55
Draft of a ReactiveCocoa based interface for CoreData
//
// FGOManagedObjectContextStack.h
//
// Created by Indragie Karunaratne on 2012-12-23.
//
#import <Foundation/Foundation.h>
typedef void (^FGOConfigurationBlock)(id);
@indragiek
indragiek / gist:5372841
Created April 12, 2013 15:23
Sanitizing URL strings
NSString *sanitizedString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *URL = [NSURL URLWithString:sanitizedString];