Last active
March 5, 2021 01:27
-
-
Save erica/835ffc4c2107a99b6c3a to your computer and use it in GitHub Desktop.
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/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