Last active
June 1, 2024 05:13
-
-
Save GeorgeLyon/bbd443dcabef5ca5ae71ae83db6524ef 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/env xcrun -sdk macosx swift | |
// Displays UI in an NSWindow which can interact with the commandline | |
// Usage: `echo "Bar" | ./swift-ui-commandline-tool.swift` | |
import Foundation | |
import SwiftUI | |
extension CommandLine { | |
static let input: String = { AnyIterator { readLine() }.joined() }() | |
} | |
struct App: SwiftUI.App { | |
var body: some Scene { | |
WindowGroup { | |
VStack { | |
Text("Hello, George!") | |
Button("Print \"Foo\"") { print("Foo") } | |
Button("Echo Input") { print(CommandLine.input) } | |
Button("Done") { exit(0) } | |
} | |
.padding(100) | |
} | |
.windowStyle(HiddenTitleBarWindowStyle()) | |
} | |
} | |
App.main() |
Happy to help, this code is a bit old so thank you for checking it still works :)
Very cool! For me the window wouldn't open unless:
DispatchQueue.main.async {
NSApplication.shared.setActivationPolicy(.regular)
NSApplication.shared.activate(ignoringOtherApps: true)
}
App.main()
Thanks! Yeah I haven't re-ran this in some time so it probably is a bit out of date.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is amazing. Thank you!