https://github.com/kukushechkin/openinterminalbutton
A tiny SwiftPM package that provides a library and a SwiftUI button for opening a folder in macOS Terminal.app or iTerm2.app.
https://github.com/kukushechkin/openinterminalbutton
macos swift swiftui terminal
Last synced: 2 months ago
JSON representation
A tiny SwiftPM package that provides a library and a SwiftUI button for opening a folder in macOS Terminal.app or iTerm2.app.
- Host: GitHub
- URL: https://github.com/kukushechkin/openinterminalbutton
- Owner: kukushechkin
- License: mit
- Created: 2024-03-30T19:19:37.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-31T12:31:21.000Z (over 2 years ago)
- Last Synced: 2026-04-23T01:39:18.199Z (3 months ago)
- Topics: macos, swift, swiftui, terminal
- Language: Swift
- Homepage:
- Size: 14.6 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://swiftpackageindex.com/kukushechkin/OpenInTerminalButton)
[](https://swiftpackageindex.com/kukushechkin/OpenInTerminalButton)
# OpenInTerminalButton
A tiny SwiftPM package that provides a library and a SwiftUI button for opening a folder in macOS Terminal.app or iTerm2.app.
## Comptbitility
For obvious reasons, this package is macOS-only.
Apple Events can be used in sandbox only with a temporary exception, but even if you manage to get one,
that app will, most probably, [not pass the App Store review process](https://developer.apple.com/forums/thread/663311?answerId=639603022#639603022).
## Public interface
- `struct OpenInTerminalButton: View` - a SwiftUI button that opens the current Finder window in Terminal.app or iTerm2.app.
- `struct BorderlessButton: View` - a SwuftUI button without a border and background.
- `enum OpenInTerminalError` — an enumeration of possible errors.
- `enum SupportedTerminal` — an enumeration of possible terminal applications.
- `func openInTerminal(location: URL, commands: [String]?) async throws -> Result` — a function that opens the specified location in Terminal.app or iTerm2.app and runs the specified commands.
## Usage
1. Add a dependency in your `Package.swift`:
```swift
.package(url: "https://github.com/kukushechkin/OpenInTerminalButton", .upToNextMajor(from: "1.0.0"))
```
2. Add Apple Events to the `Info.plist` of your app:
```xml
NSAppleEventsUsageDescription
Allow this app to open terminal app and execute commands.
```
3. Use the `OpenInTerminalButton` view:
```swift
import SwiftUI
import OpenInTerminalButton
struct ContentView: View {
var body: some View {
OpenInTerminalButton(
location: URL(fileURLWithPath: "~/Desktop"),
commands: [
"echo 'Hello, world!'",
"ls -la"
]
)
}
}
```
After clicking on the button, a new iTerm2.app (if installed) or Terminal window will open with the specified commands.
If you're using Hotkey Window in iTerm2.app, a new tab will open in the existing window.
4. Use `openInTerminal(location:commands:)` function directly, for example if you would like to create a custom button:
```swift
import OpenInTerminalButton
let result = openInTerminal(location: URL(fileURLWithPath: "~/Desktop"), commands: ["echo 'Hello, world!'"])
switch result {
case .success(let terminal):
print("Opened in \(terminal)")
case .failure(let error):
print("Error: \(error)")
}
```