Skip to content

Instantly share code, notes, and snippets.

@erica erica/note.swift
Last active Aug 22, 2016

Embed
What would you like to do?
#!/usr/bin/xcrun swift
import Cocoa
import ObjectiveC
// Pretend to be Finder
extension Bundle { var __bundleIdentifier: String? { return "com.apple.finder" } }
guard let bundleClass : AnyClass = NSClassFromString("NSBundle")
else { fatalError("Unable to fetch NSBundle class") }
let id1 = class_getInstanceMethod(bundleClass, #selector(getter: Bundle.bundleIdentifier))
let id2 = class_getInstanceMethod(bundleClass, #selector(getter: Bundle.__bundleIdentifier))
method_exchangeImplementations(id1, id2)
// Fetch Command Line Arguments
var arguments = CommandLine.arguments
let appName = arguments.remove(at: 0)
guard !arguments.isEmpty else {
print("Usage: \(appName) [-d|--delay delay] message")
exit(-1)
}
// Find delay if used
var delay = 0.0
let dashedArguments: [String] = arguments.filter({ $0.hasPrefix("-") })
for argument in dashedArguments {
guard var argumentIndex = arguments.index(of: argument) else { continue }
if !["-d", "--delay"].contains(argument) { continue }
// Fetch the time to delay
let nextIndex = argumentIndex.advanced(by: 1)
if nextIndex < arguments.endIndex {
let value = arguments[nextIndex]
if let delayValue = Double(value) {
// Set delay
delay = delayValue
// Remove flag and value from message
arguments.remove(at: nextIndex)
arguments.remove(at: argumentIndex)
// Stop looking for delay
break
}
}
}
// Compose message
let message = arguments.joined(separator: " ")
// Post notification
let note: NSUserNotification = {
$0.soundName = NSUserNotificationDefaultSoundName
$0.hasActionButton = true
$0.title = message
$0.deliveryDate = Date(timeIntervalSinceNow: delay)
return $0
}(NSUserNotification())
NSUserNotificationCenter.default.scheduleNotification(note)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.