OpenInTerminalButton

main

A tiny SwiftPM package that provides a library and a SwiftUI button for opening a folder in macOS Terminal.app or iTerm2.app.
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.

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<SupportedTerminal, OpenInTerminalError> — 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:
.package(url: "https://github.com/kukushechkin/OpenInTerminalButton", .upToNextMajor(from: "1.0.0"))
  1. Add Apple Events to the Info.plist of your app:
<key>NSAppleEventsUsageDescription</key>
<string>Allow this app to open terminal app and execute commands.</string> 
  1. Use the OpenInTerminalButton view:
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.

  1. Use openInTerminal(location:commands:) function directly, for example if you would like to create a custom button:
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)")
}

Description

  • Swift Tools 5.7.1
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Apr 13 2024 05:49:26 GMT-0900 (Hawaii-Aleutian Daylight Time)