From 34006c6279bb251c73f06f465399da356640f1b1 Mon Sep 17 00:00:00 2001 From: Andrew Alba Date: Sat, 11 Jan 2025 16:48:15 -0600 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20Add=20support=20for=20custom=20?= =?UTF-8?q?terminal=20profiles=20in=20command=20execution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Introduce a new method `removeProfileParameter` to handle the removal of the `--profile` parameter from commands. - Modify the command execution logic to use the specified terminal profile when launching iTerm or other terminals. - Ensure that the command is executed correctly with the appropriate profile, enhancing user experience and flexibility. --- .../MenuBarSSHCommandsApp.swift | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/MenuBarSSHCommands/MenuBarSSHCommandsApp.swift b/MenuBarSSHCommands/MenuBarSSHCommandsApp.swift index 8f8044f..c4a0c60 100644 --- a/MenuBarSSHCommands/MenuBarSSHCommandsApp.swift +++ b/MenuBarSSHCommands/MenuBarSSHCommandsApp.swift @@ -146,15 +146,19 @@ struct MenuBarSSHCommandsApp: App { private func executeCommand(_ command: String) { let terminal = buttonContainerStore.terminal var source: String + var profile: String = "Default" + + 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 "\(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 @@ -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 @@ -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 @@ -218,6 +222,27 @@ struct MenuBarSSHCommandsApp: App { 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 = 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 { From ba25119f98f5e0a9619ec61795c056845990a6fc Mon Sep 17 00:00:00 2001 From: Andrew Alba Date: Sat, 11 Jan 2025 17:08:12 -0600 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9D=20Update=20README.md=20to=20in?= =?UTF-8?q?clude=20iTerm=20profile=20integration=20instructions=20for=20SS?= =?UTF-8?q?H=20commands=20to=20enhance=20user=20experience=20and=20provide?= =?UTF-8?q?=20clarity=20on=20usage.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index 9683376..3ff878e 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,28 @@ A simple Terminal and SSH Command Shortcut Menu Bar App for built for MacOS Vent Buy Me A Coffee +#### 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. From 8596a83683179892342f662c13486cf63f582813 Mon Sep 17 00:00:00 2001 From: Andrew Alba Date: Sun, 12 Jan 2025 09:30:42 -0600 Subject: [PATCH 3/3] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20code:=20cle?= =?UTF-8?q?an=20up=20whitespace=20and=20improve=20variable=20naming=20for?= =?UTF-8?q?=20clarity=20in=20MenuBarSSHCommandsApp.swift?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MenuBarSSHCommandsApp.swift | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/MenuBarSSHCommands/MenuBarSSHCommandsApp.swift b/MenuBarSSHCommands/MenuBarSSHCommandsApp.swift index c4a0c60..401a243 100644 --- a/MenuBarSSHCommands/MenuBarSSHCommandsApp.swift +++ b/MenuBarSSHCommands/MenuBarSSHCommandsApp.swift @@ -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() @@ -44,7 +44,7 @@ class ButtonContainerStore: ObservableObject { } } } - + private func startMonitoringFileChanges() { let fileURL = Bundle.main.url(forResource: jsonFileName, withExtension: "json") fileMonitor = FileMonitor(fileURL: fileURL) @@ -100,7 +100,7 @@ class FileMonitor { @main struct MenuBarSSHCommandsApp: App { - + @StateObject private var buttonContainerStore = ButtonContainerStore() var body: some Scene { MenuBarExtra{ @@ -131,14 +131,14 @@ 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) } } @@ -146,7 +146,7 @@ struct MenuBarSSHCommandsApp: App { private func executeCommand(_ command: String) { let terminal = buttonContainerStore.terminal var source: String - var profile: String = "Default" + var profile: String = "default profile" let modifiedCommand = removeProfileParameter(from: command, profile: &profile) @@ -155,7 +155,7 @@ struct MenuBarSSHCommandsApp: App { if application "\(terminal)" is running then tell application "\(terminal)" activate - tell (create window with profile "\(profile)") + tell (create window with \(profile)) delay 0.2 -- Wait for the terminal to launch tell current session write text "\(modifiedCommand)" @@ -213,12 +213,12 @@ struct MenuBarSSHCommandsApp: App { } - + private func openJSONFile() { guard let url = Bundle.main.url(forResource: buttonContainerStore.jsonFileName, withExtension: "json") else { return } - + NSWorkspace.shared.open(url) } @@ -229,7 +229,7 @@ struct MenuBarSSHCommandsApp: App { if let index = components.firstIndex(of: "--profile".split(separator: " ")[0]) { if index + 1 < components.count { let profileValue = components[index + 1] - profile = profileValue.trimmingCharacters(in: CharacterSet(charactersIn: "\"")) + profile = "profile \"\(profileValue.trimmingCharacters(in: CharacterSet(charactersIn: "\"")) )\"" modifiedCommand = components.enumerated().compactMap { (i, element) in if i == index || i == index + 1 {