This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Collection { | |
/// Perform a block on each chunk of `size` elements in this collection | |
/// - Parameters: | |
/// - size: The maximum size of the chunk | |
/// - block: The block to call with the chunk. Set `stop = true` to stop processing | |
func chunking(into size: Int, _ block: (SubSequence, inout Bool) -> Void) { | |
var stop = false | |
var index = 0 | |
while index < self.count { | |
let offset = index + Swift.min(size, count - index) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UUID { | |
/// Errors thrown | |
public enum LoadError: Error { | |
case invalidUUIDData | |
} | |
/// Returns the raw bytes of the UUID as a Data object. | |
/// | |
/// Note that this method assumes a specific memory layout (the layout on the device calling the method) | |
/// and as such may not be portable to other systems. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Applescript: tell application "System Preferences" to get anchors of current pane | |
// Result: | |
// { anchor "Privacy_Reminders" of pane id "com.apple.preference.security" of application "System Preferences", | |
// anchor "Privacy_SystemServices" of pane id "com.apple.preference.security" of application "System Preferences", | |
// anchor "Privacy_Calendars" of pane id "com.apple.preference.security" of application "System Preferences", | |
// anchor "Firewall" of pane id "com.apple.preference.security" of application "System Preferences", | |
// anchor "Privacy_Assistive" of pane id "com.apple.preference.security" of application "System Preferences", | |
// anchor "Privacy_LinkedIn" of pane id "com.apple.preference.security" of application "System Preferences", | |
// anchor "Privacy_Accessibility" of pane id "com.apple.preference.security" of application "System Preferences", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CAAnimation+BlockCallback.swift | |
// | |
import UIKit | |
// | |
// Modified from http://onedayitwillmake.com/blog/2016/06/caanimation-completion-callback-block/ | |
// Updated for Swift 4 syntax | |
// All credit to the original author (Mario Gonzalez) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/swift | |
import Foundation | |
import AppKit.NSWorkspace | |
// Returns the name of the frontmost app, or <none> if no app is frontmost | |
func currentFocusApp() -> String { | |
NSWorkspace.shared.frontmostApplication?.localizedName ?? "<none>" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Observe all notifications generated by the default NotificationCenter | |
NotificationCenter.default.addObserver( | |
forName: nil, object: nil, queue: nil) { notification in | |
Swift.print("Notification: \(notification.name.rawValue), Object: \(notification.object)") | |
} | |
// Observe all notifications generated by the default DistributedNotificationCenter | |
DistributedNotificationCenter.default().addObserver( | |
forName: nil, object: nil, queue: nil) { notification in | |
Swift.print("Notification: \(notification.name.rawValue), Object: \(notification.object)") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension NSImage { | |
/// Create a CIImage using the best representation available | |
/// | |
/// - Returns: Converted image, or nil | |
func asCIImage() -> CIImage? { | |
if let cgImage = self.asCGImage() { | |
return CIImage(cgImage: cgImage) | |
} | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Copyright © 2024 Darren Ford. All rights reserved. | |
// | |
// Adapted from the PaintCode blog: https://www.paintcodeapp.com/news/code-for-ios-7-rounded-rectangles | |
// | |
// MIT license | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | |
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the | |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Provide the equivalent of @synchronised on objc | |
private func synchronized<T>(_ lock: AnyObject, _ body: () throws -> T) rethrows -> T { | |
objc_sync_enter(lock) | |
defer { objc_sync_exit(lock) } | |
return try body() | |
} | |
/// Simple lockable class | |
class Lockable { | |
private var lockable: AnyObject |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public extension Sequence where Element: Equatable { | |
/// Return the unique elements in the array using Equatable as the predicate | |
var unique: [Element] { | |
return self.reduce(into: []) { uniqueElements, element in | |
if !uniqueElements.contains(element) { | |
uniqueElements.append(element) | |
} | |
} | |
} | |
} |
NewerOlder