Skip to content

Instantly share code, notes, and snippets.

@ericdke
ericdke / iOSVideoFrameGrab.swift
Last active October 3, 2015 17:48
Video thumbnail
import UIKit
import XCPlayground
import AVFoundation
func generateThumnail(url : NSURL, fromTime:Float64) -> UIImage {
var asset :AVAsset = AVAsset.assetWithURL(url) as! AVAsset
var assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
var error : NSError? = nil
var time : CMTime = CMTimeMakeWithSeconds(fromTime, 600)
@ericdke
ericdke / getMACAddress.swift
Last active January 28, 2026 07:03
Swift OS X: get MAC address for en0
/ Returns an iterator containing the primary (built-in) Ethernet interface. The caller is responsible for
// releasing the iterator after the caller is done with it.
func FindEthernetInterfaces() -> io_iterator_t? {
let matchingDictUM = IOServiceMatching("IOEthernetInterface");
// Note that another option here would be:
// matchingDict = IOBSDMatching("en0");
// but en0: isn't necessarily the primary interface, especially on systems with multiple Ethernet ports.
if matchingDictUM == nil {
@ericdke
ericdke / getFontNames.swift
Created August 5, 2015 14:50
iOS: get installed fonts UIFont names
func getAllFontNames() -> [String] {
return UIFont.familyNames().flatMap({ UIFont.fontNamesForFamilyName($0) })
}
func getRealFontNames(prefix: String) -> [String] {
return getAllFontNames().filter({ $0.lowercaseString.hasPrefix(prefix.lowercaseString) })
}
let matches = getRealFontNames("Gill")
/*
File: LSSharedFileList.h
Contains: Services to load and share file lists.
Copyright: (c) 2003-2012 by Apple Inc. All rights reserved.
Bugs?: For bug reports, consult the following page on
the World Wide Web:
@ericdke
ericdke / setTextToPasteBoard.swift
Last active January 9, 2016 22:43
Swift OS X: set text to PasteBoard
import Cocoa
func setTextToPasteBoard(text: String) -> Bool {
let pasteBoard = NSPasteboard.generalPasteboard()
pasteBoard.declareTypes([NSStringPboardType], owner: nil)
return pasteBoard.writeObjects([text])
}
@ericdke
ericdke / recentlyOpenedApplications.swift
Last active January 9, 2016 22:45
Swift OSX: recently opened applications (only OS X 10.9 and 10.10)
let listBase = LSSharedFileListCreate(kCFAllocatorDefault, kLSSharedFileListRecentApplicationItems.takeUnretainedValue(), NSMutableDictionary())
let list = listBase.takeUnretainedValue() as LSSharedFileListRef
var seed:UInt32 = 0
let itemsCF = LSSharedFileListCopySnapshot(list, &seed)
if let items = itemsCF.takeUnretainedValue() as? [LSSharedFileListItemRef] {
for item in items {
let listItemURL = LSSharedFileListItemCopyResolvedURL(item, 0, nil)
let urlBase = listItemURL.takeUnretainedValue()
let url = urlBase as NSURL
let listItemName = LSSharedFileListItemCopyDisplayName(item)
@ericdke
ericdke / gist:859d4a8b1d749948f6dd
Created August 1, 2015 15:29 — forked from erica/gist:7c298164409795bbf92f
Swift: Launch at Login Helper
//
// LaunchAtLoginHelper.swift
//
// Created by Erica Sadun on 4/1/15.
// Copyright (c) 2015 Erica Sadun. All rights reserved.
//
import Foundation
public func getLoginItems() -> LSSharedFileList? {
@ericdke
ericdke / getFinderSidebarVolumes.swift
Last active January 9, 2016 22:46
Swift: get the Finder sidebar icons of all mounted volumes (only OS X 10.9 and 10.10)
let listBase = LSSharedFileListCreate(kCFAllocatorDefault, kLSSharedFileListFavoriteVolumes.takeUnretainedValue(), NSMutableDictionary())
let list = listBase.takeRetainedValue() as LSSharedFileList
var seed:UInt32 = 0
let itemsCF = LSSharedFileListCopySnapshot(list, &seed)
if let items = itemsCF.takeRetainedValue() as? [LSSharedFileListItemRef] {
for item in items {
let icon = LSSharedFileListItemCopyIconRef(item)
let image = NSImage(iconRef: icon)
@ericdke
ericdke / sidebarIcons.swift
Created August 1, 2015 11:15
Swift: get Finder sidebar icons
if let bundle = NSBundle(path: "/System/Library/CoreServices/CoreTypes.bundle"),
let path = bundle.pathForResource("SidebarInternalDisk", ofType: "icns"),
let iconImage = NSImage(contentsOfFile: path) {
...
}
@ericdke
ericdke / IconsCore.h
Created August 1, 2015 10:35
Finder icons Cocoa constants
/*
File: LaunchServices/IconsCore.h
Contains: Icon Utilities and Icon Services Interfaces.
Version: LaunchServices-283~12
Copyright: � 1990-2006 by Apple Computer, Inc. All rights reserved
Bugs?: For bug reports, consult the following page on