Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement for iTerm profiles #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions MenuBarSSHCommands/MenuBarSSHCommandsApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class ButtonContainerStore: ObservableObject {
@Published var buttonContainer: [ButtonContainer] = []
let jsonFileName = "MenuBarSSHCommandsData"
var terminal: String = "Terminal"

private var fileMonitor: FileMonitor?

init() {
do {
loadButtonContainer()
Expand Down Expand Up @@ -44,7 +44,7 @@ class ButtonContainerStore: ObservableObject {
}
}
}

private func startMonitoringFileChanges() {
let fileURL = Bundle.main.url(forResource: jsonFileName, withExtension: "json")
fileMonitor = FileMonitor(fileURL: fileURL)
Expand Down Expand Up @@ -100,7 +100,7 @@ class FileMonitor {

@main
struct MenuBarSSHCommandsApp: App {

@StateObject private var buttonContainerStore = ButtonContainerStore()
var body: some Scene {
MenuBarExtra{
Expand Down Expand Up @@ -131,30 +131,34 @@ struct MenuBarSSHCommandsApp: App {
})
}
label: {

let image: NSImage = {
let ratio = $0.size.height / $0.size.width
$0.size.height = 18
$0.size.width = 18 / ratio
return $0
}(NSImage(named: "Icon")!)

Image(nsImage: image)
}
}

private func executeCommand(_ command: String) {
let terminal = buttonContainerStore.terminal
var source: String
var profile: String = "default profile"

let modifiedCommand = removeProfileParameter(from: command, profile: &profile)

if terminal == "iTerm" {
source = """
if application "\(terminal)" is running then
tell application "\(terminal)"
activate
tell (create window with default profile)
tell (create window with \(profile))
delay 0.2 -- Wait for the terminal to launch
tell current session
write text "\(command)"
write text "\(modifiedCommand)"
end tell
end tell
end tell
Expand All @@ -164,7 +168,7 @@ struct MenuBarSSHCommandsApp: App {
delay 0.5 -- Wait for the terminal to launch
tell current window
tell current session
write text "\(command)"
write text "\(modifiedCommand)"
end tell
end tell
end tell
Expand All @@ -175,16 +179,16 @@ struct MenuBarSSHCommandsApp: App {
if application "\(terminal)" is not running then
tell application "\(terminal)"
activate
do script "\(command)"
do script "\(modifiedCommand)"
end tell
else
tell application "\(terminal)"
if (count windows) > 0 then
tell front window
do script "\(command)"
do script "\(modifiedCommand)"
end tell
else
do script "\(command)"
do script "\(modifiedCommand)"
end if
activate
end tell
Expand All @@ -209,15 +213,36 @@ struct MenuBarSSHCommandsApp: App {
}



private func openJSONFile() {
guard let url = Bundle.main.url(forResource: buttonContainerStore.jsonFileName, withExtension: "json") else {
return
}

NSWorkspace.shared.open(url)
}

private func removeProfileParameter(from command: String, profile: inout String) -> String {
var modifiedCommand = command
let components = command.split(separator: " ")

if let index = components.firstIndex(of: "--profile".split(separator: " ")[0]) {
if index + 1 < components.count {
let profileValue = components[index + 1]
profile = "profile \"\(profileValue.trimmingCharacters(in: CharacterSet(charactersIn: "\"")) )\""

modifiedCommand = components.enumerated().compactMap { (i, element) in
if i == index || i == index + 1 {
return nil // Skip the --profile and its value
}
return String(element)
}.joined(separator: " ")
}
}

return modifiedCommand
}

}

struct Section: Identifiable, Equatable, Decodable {
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@ A simple Terminal and SSH Command Shortcut Menu Bar App for built for MacOS Vent

<a href="https://www.buymeacoffee.com/dieskim" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a>

#### iTerm Profile Integration

When using iTerm, you can specify which profile to use by adding the `--profile` flag to your SSH command. This is helpful if you maintain different profiles for various environments (like production, staging, etc.).

For example, if you have an iTerm profile named "Production" configured with specific settings for production servers, you can reference it in your configuration like this:

```json
{
"terminal": "iTerm", // Supported terminals: 'Terminal' or 'iTerm' (recommended)
"data": [
{
"action": [
{
"name": "Action1",
"command": "ssh admin@host.com --profile Production"
}
]
}
]
}
```

## Credits

MenuBarSSHCommands was inspired by [Shuttle](https://github.com/fitztrev/shuttle) and [SSHMenu](http://sshmenu.sourceforge.net/), the GNOME applet for Linux.
Expand Down