Skip to content

Instantly share code, notes, and snippets.

@aglee
Created June 23, 2024 23:09
Show Gist options
  • Save aglee/7724c686769ee4e923281ca90e3a3397 to your computer and use it in GitHub Desktop.
Save aglee/7724c686769ee4e923281ca90e3a3397 to your computer and use it in GitHub Desktop.
Swift program to display an alert
import Cocoa
func imageFromEmoji(emoji: String, size: CGSize) -> NSImage? {
let image = NSImage(size: size)
image.lockFocus()
let attributes: [NSAttributedString.Key: Any] = [
.font: NSFont.systemFont(ofSize: size.height) // Adjust font size based on image size
]
let attributedString = NSAttributedString(string: emoji, attributes: attributes)
attributedString.draw(at: CGPoint(x: 0, y: 0))
image.unlockFocus()
return image
}
func showAlert(message: String, info: String, image: NSImage?) {
let alert = NSAlert()
alert.messageText = message
alert.informativeText = info
if let image = image {
alert.icon = image
}
alert.addButton(withTitle: "OK")
let alertWindow = alert.window
alertWindow.level = .floating
alertWindow.center()
NSApp.activate(ignoringOtherApps: true)
alert.runModal()
}
//let arguments = CommandLine.arguments
//guard arguments.count >= 3 else {
// print("Usage: AlertCLI <message> <infoText>")
// exit(1)
//}
//let message = arguments[1]
//let infoText = arguments[2]
let app = NSApplication.shared
app.setActivationPolicy(.regular)
DispatchQueue.main.async {
let timeString = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .medium)
let icon = imageFromEmoji(emoji: "🥳", size: CGSize(width: 128, height: 128))
showAlert(message: "Build Succeeded", info: timeString, image: icon)
app.terminate(nil)
}
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment