Skip to content

Instantly share code, notes, and snippets.


Pierre Houston jpmhouston

  • Vancouver, BC
View GitHub Profile
jpmhouston /
Created Aug 24, 2020
Fish shell function that outputs linefeed delimited paths of the finder selection
# Useful to refer to finder selections from the fish shell command line, like:
# $ file (selection)
# /Users/me/Documents/Files/Throwback.dvtcolortheme: XML 1.0 document text, ASCII text
# /Users/me/Documents/Files/Test.sparsebundle/: directory
function selection
osascript -e "tell application \"Finder\" to set s to the selection as alias list" -e "repeat with f in s" -e "set contents of f to POSIX path of f" -e "end repeat" -e "set AppleScript's text item delimiters to linefeed" -e "s as string"
jpmhouston / Result+succeeded.swift
Created Jul 4, 2020
Helpful Result extension functions failed() and suceeded() which return an optional of either the failure error or success value
View Result+succeeded.swift
extension Result {
func failed(else handleSuccess: ((Success) -> Void)? = nil) -> Failure? {
switch self {
case .success(let value):
return nil
case .failure(let error):
return error
jpmhouston / gist:db1fa96997cef811bc3251b6d50e99d6
Created Jan 30, 2019
Data extension method hexEncodedString
View gist:db1fa96997cef811bc3251b6d50e99d6
extension Data {
struct HexEncodingOptions: OptionSet {
let rawValue: Int
static let uppercase = HexEncodingOptions(rawValue: 0)
static let lowercase = HexEncodingOptions(rawValue: 1 << 0)
func hexEncodedString(options: HexEncodingOptions = []) -> String {
let hexDigits = Array((options.contains(.lowercase) ? "0123456789abcdef" : "0123456789ABCDEF").utf16)
var dump: [unichar] = []
var ascii: [unichar] = []
jpmhouston / gist:de82ffa91a9fdb026b39bc6ad65c7c0c
Created May 11, 2017
convert TimeInterval to DispatchTimeInterval
View gist:de82ffa91a9fdb026b39bc6ad65c7c0c
public extension TimeInterval {
public var dispatchInterval: DispatchTimeInterval {
let microseconds = Int64(self * TimeInterval(USEC_PER_SECS)) // perhaps use nanoseconds, though would more often be > Int.max
return microseconds < Int.max ? DispatchTimeInterval.microseconds(Int(microseconds)) : DispatchTimeInterval.seconds(Int(self))
jpmhouston / ColorPHTextField.swift
Last active Sep 15, 2016
UITextField subclass adding placeholderColor property
View ColorPHTextField.swift
// TODO: link here to SO post that inspired this
// ^ that source worked fine when setting placeholderColor sometime after
// placeholder or attributedPlaceholder, but not the other way around.
// this is an attempt to fix this, it might work.
class ColorPHTextField : UITextField {
@IBInspectable var placeholderColor: UIColor? {
didSet {
jpmhouston / gist:f25d560cbd771d0f74f3d7b2d0ce6fbd
Last active Aug 8, 2016
Swift Array element(at:), why is this missing from the standard library?
View gist:f25d560cbd771d0f74f3d7b2d0ce6fbd
extension Array {
func element(at index: Int) -> Element? {
return index >= 0 && index < count ? self[index] : nil
jpmhouston / NSManagedObject+JPHClone.h
Last active May 19, 2017
View NSManagedObject+JPHClone.h
// NSManagedObject+Clone.h
// TResActivityLog
// Created by Pierre Houston on 2016-05-30.
// Copyright © 2016 Resilience software. All rights reserved.
// When no context provided, uses [NSManagedObjectContext MR_defaultContext].
// Note, I was going to add unique prefix "jph_" to these category method names,
// but currently I've left those out.
jpmhouston / gist:35686fc811b1c62effe5
Last active Aug 29, 2015
Shrink UITextField placeholder to fit
View gist:35686fc811b1c62effe5
// call from a view controller's -viewDidLayoutSubviews or -viewWillAppear:
// pass it a text field, etc, assign result to that text field's attributedPlaceholder property
// note that the maxKerning parameter must be a negative value (a "maximally negative" kerning value)
// pro tip: i've sometimes found that a text field still has incorrect bounds in -viewDidLayoutSubviews
// and needs to forcibly have its layout done:
//- (void)viewDidLayoutSubviews {
// [self.field layoutIfNeeded];
// self.field.attributedPlaceholder = [self resizedPlaceholderForField:self.field withPadding:4.0 maxKerningReduction:0.3];
jpmhouston / gist:daa75345f6a017ce1681
Created Jun 19, 2015
POST with both URL parameters & JSON body in AFNetworking
View gist:daa75345f6a017ce1681
@interface MixedPOSTHTTPSessionManager : AFHTTPSessionManager
- (NSURLSessionDataTask *)POST:(NSString *)URLString
JSONParameters:(NSDictionary *)jsonParameters
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure;
@implementation MixedPOSTHTTPSessionManager
View gist:131b96d02830d4a540ce
Verifying I am +jpmhouston on my passcard.