Skip to content

Instantly share code, notes, and snippets.

@Hexfire
Hexfire / osx-messagebox.swift
Last active April 24, 2017 07:58
macOS Modal NSAlert with completion handlers
// As Modal window:
func messageBox(_ title: String!, message: String! = nil,
firstOption: String? = nil, secondOption: String? = nil, thirdOption: String? = nil,
firstAction: (() -> ())? = nil, secondAction: (() -> ())? = nil, thirdAction: (() -> ())? = nil ) {
let alert = NSAlert()
alert.messageText = title ?? ""
alert.informativeText = message ?? ""
@Hexfire
Hexfire / visibleViewController.swift
Created May 23, 2017 03:03
[Swift 3] Visible UIViewController
extension UIApplication {
class func visibleViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return visibleViewController(nav.visibleViewController)
}
if let tab = base as? UITabBarController {
let moreNavigationController = tab.moreNavigationController
@Hexfire
Hexfire / AXUIElement_in_Swift.swift
Created November 7, 2017 12:13 — forked from c9iim/AXUIElement_in_Swift.swift
AXUIElement in Swift
import Cocoa
protocol AXUIProtocol {
func AXUIWindowArray(processIdentifier pid:pid_t) -> [AXUIElement]
func AXUIWindowArray(bundleIdentifier bid:NSString) -> [AXUIElement]
}
extension AXUIProtocol {
func AXUIWindowArray(processIdentifier pid:pid_t) -> [AXUIElement] {
let windowList : UnsafeMutablePointer<AnyObject?> = UnsafeMutablePointer<AnyObject?>.alloc(1)
import Cocoa
extension NSWorkspace {
class func frontmostApp() -> NSRunningApplication? {
return self.sharedWorkspace().frontmostApplication
}
class func runningApp(bundleIdentifier:NSString) -> NSRunningApplication? {
let runningApplications = NSWorkspace.sharedWorkspace().runningApplications
return runningApplications.filter({$0.bundleIdentifier == bundleIdentifier}).first
}
@Hexfire
Hexfire / uiimage-merge.swift
Created November 16, 2017 09:09
Merge two images
extension UIImage {
static func imageByMergingImages(topImage: UIImage, bottomImage: UIImage, scaleForTop: CGFloat = 1.0) -> UIImage {
let size = bottomImage.size
let container = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 2.0)
UIGraphicsGetCurrentContext()!.interpolationQuality = .high
bottomImage.draw(in: container)
let topWidth = size.width / scaleForTop
@Hexfire
Hexfire / HTTPStatusCodes.swift
Created December 5, 2017 09:24 — forked from brennanMKE/HTTPStatusCodes.swift
Swift Enums for HTTP Status Codes
enum HTTPStatusCodes: Int {
// 100 Informational
case Continue = 100
case SwitchingProtocols
case Processing
// 200 Success
case OK = 200
case Created
case Accepted
case NonAuthoritativeInformation
@Hexfire
Hexfire / ImageRotation.swift
Created April 19, 2017 02:56
Rotate NSImageView (UIImageView) around its center
extension NSView { // UIView
// Set source/destination angle.
// Angle is set in radians (0..2π), hence 360* rotation = 2π/-2π
func spinClockwise(timeToRotate: Double) {
startRotation(angle: -1 * CGFloat.pi * 2.0, timeToRotate: timeToRotate)
}
func spinAntiClockwise(timeToRotate: Double) {
startRotation(angle: CGFloat.pi * 2.0, timeToRotate: timeToRotate)
@Hexfire
Hexfire / UIView+Xib
Created July 10, 2019 14:05
UIView.loadFromXib()
import UIKit
extension UIView {
static func loadFromXib() -> UIView? {
let bundle = Bundle(for: self)
let nib = UINib(nibName: String(describing: self), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as? UIView
return view
}