Skip to content

Instantly share code, notes, and snippets.

View odrobnik's full-sized avatar

Oliver Drobnik odrobnik

View GitHub Profile
@odrobnik
odrobnik / gist:e8ac59e13b62ea80b623
Created January 11, 2016 11:26
Calling AppleScript from Swift App, passing a parameter
// Swift version of https://developer.apple.com/library/mac/technotes/tn2084/_index.html
@IBAction func testButtonPushed(sender: AnyObject) {
let URL = NSBundle.mainBundle().URLForResource("SendFinderMessage", withExtension: "scpt")!
var errors: NSDictionary?
let script = NSAppleScript(contentsOfURL: URL, error: &errors)!
@odrobnik
odrobnik / gist:f32ded1fce2d168b6b66
Created December 16, 2015 12:42
Enumerating points on a line no further than a certain distance apart
func enumeratePointsOnLine(startPoint: CGPoint, endPoint: CGPoint, maxDistance: CGFloat, block: (point: CGPoint)->())
{
// for single point we don't iterate anything
guard !CGPointEqualToPoint(startPoint, endPoint) else
{
block(point: startPoint)
return
}
let deltaX = endPoint.x - startPoint.x
@odrobnik
odrobnik / gist:2751fb3ce32792b8a85d
Last active February 21, 2023 12:06
Swift: create CGPath from attributed string
func appendToPath(path: CGMutablePath)
{
let textPath = CGPathCreateMutable()
let attributedString = NSAttributedString(string: string)
let line = CTLineCreateWithAttributedString(attributedString)
// direct cast to typed array fails for some reason
let runs = (CTLineGetGlyphRuns(line) as [AnyObject]) as! [CTRun]
for page in pages ?? []
{
guard let pageNumber = page.pageNumber
else { continue }
if let array = pageLookup[pageNumber]
{
pageLookup[pageNumber] = array + [page]
}
else
modified = @"Fri, 02 Oct 2015 16:20:34 CEST";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:locale];
_lastModifiedDate = [dateFormatter dateFromString:modified];
@odrobnik
odrobnik / gist:f2f3182fea54e07b694b
Last active November 14, 2015 15:22
Answer: how to restart animation after backgrounding
//
// HeroRaysView.swift
// HeroRays
//
// Created by Oliver Drobnik on 08/11/15.
// Copyright © 2015 Oliver Drobnik. All rights reserved.
//
import UIKit
@odrobnik
odrobnik / gist:a02ac1e49ff90ec4f8a7
Last active January 4, 2018 17:37
Upload progress reporting NSProgress for NSURLSessionTask
import Foundation
public class UploadProgress: NSProgress
{
var sessionTask: NSURLSessionTask!
required public init(sessionTask: NSURLSessionTask)
{
super.init(parent: nil, userInfo: nil)
@odrobnik
odrobnik / gist:3ecde0247717ef8f00eb
Created November 9, 2015 10:50
I would like to have this be an abstract class.
class Entity: NSObject
{
var objectID: String?
var createdAt: NSDate?
var updatedAt: NSDate?
required override init()
{
}
@odrobnik
odrobnik / something.swift
Created November 5, 2015 16:15
Why does this work? what is the difference between AnyClass and T.Type? How could this be made more generic?
func classForName(name: String) -> AnyClass
{
// build namespaced name
let namespacedName = "Module." + name
return NSClassFromString(namespacedName)!
}
func nameForType<T where T: Entity>(type: T.Type) -> String
{
@odrobnik
odrobnik / something.swift
Last active November 5, 2015 15:38
Trying to dynamically init the correct class based on a string
class Entity
{
var objectID: String?
var createdAt: NSDate?
var updatedAt: NSDate?
var avatarURL: NSURL?
var wallpaperURL: NSURL?
required init()
{