Jump to content

Terminal Control Workflow


Recommended Posts

Alfred - Terminal and iTerm2 Control Workflow

 

:) Requires Alfred 3+

 

Quick Start: Download Here  (Alfred 2 users, the last compatible version was Terminal Control 2.2, still available here)

 

An Alfred workflow for controlling aspects of Apple's Terminal Utility and iTerm2. Designed specifically for developers and admins who spend a lot of time in terminals at the command line. Particularly useful for those who manage multiple tabs and use varied Profiles (color themes) for Terminal/iTerm2 windows.

 

workflow.png

 

New Terminals

The keyword trigger term (or iterm) will bring up a list of Profiles (themes/skins) based on Terminal preferences. Continue typing to select one.

 

new-term.png

Modifier keys:

  • Shift: launch this Profile in a new tab
  • Command: change the active Terminal tab to this Profile

Merge and Split

 

Terminal only: Keyword trigger term move will separate the current tab into its own window. 

 

term-move.png

Terminal only: Keyword trigger term merge will merge all open Terminal windows into one tabbed set. 

 

term-merge.png

iTerm Only: Keyword trigger *iterm arrange* will call iTerm's "arrange all windows horizontally" window helper.

 

iterm-arrange.png

 

Terminal to a Directory

Keyword trigger term dir (or iterm dir), followed by a directory search phrase will launch the default Terminal profile, and automatically change to the selected directory.

term-dir.png

Why Do I Want This?

I spend a lot of time connected to various processes, machines, networks, running screen/tmux sessions, etc.  From my personal experience (read: typing the wrong command into an ssh session of a production machine and taking down large consumer web properties in the process), I've built a habit of always color-coding my terminal windows.  White on black for local OSX terminal.  Green on black for local virtual machines, green background for testing networks, and RED background for production machines (a constant reminder to be careful what I type).  This alfred workflow allows me to launch terminal themes, and also change a terminal tab quickly to a new theme if I repurpose it for a new task.

 

Install Notes

Download the compiled workflow directly, or visit the repository for the full source code

 

This is my very first workflow, so I'd appreciated feedback on the installation process.  In theory, I've bundled all the necessary ruby gems in the workflow package proper, so it should be a simple double-click install.  If you do need to install manually, please let me know.  Hints are in the developer notes.

 

You will need to allow accessibility support for Alfred.  This should be prompted automatically.  But since Mavericks, this happens in a new place, in the security and privacy system preferences area.  Enable it as shown here if you get stuck:

 

Security___Privacy-7.png

 

Thanks to Bryan McKelvey for the simple Alfredo Ruby Gem

Thanks to phyllisstein for Alleyoop

 

Thanks to Quentin Stafford-Fraser for inspiration and approach in his dedicated iTerm2 Profiles workflow

 

Release Notes

  • v1.0 (May 6, 2013) - Initial release
  • v1.1 (May 10, 2013) - Support for Alleyoop workflow updater
  • v2.0 (May 12, 2013) - Added control for popular alternative iTerm2 terminal emulator
  • v2.1 (Nov 7, 2013) - Included bundled gems for ruby 2.0.0 for Mavericks OS X 10.9 Support
  • v2.2 (Nov 21, 2014) - Improved UI scripting dialog to conform with 10.9+ privacy behaviors
  • v2.3 (Jun 23, 2016) - Support for iTerm2 version 3.0.0+ to accommodate their new AppleScript dictionaries, dropped support for Alfred 2.x
  • v2.4 (Oct 15, 2017) - Included bundled gems for ruby 2.3.0 for macOS High Sierra 10.13 Support

  • v2.5 (Sep 29, 2018) - Support for macOS Mojave 10.14

  • v2.6 (Oct 27, 2019) - Removed alfredo and nokogiri dependencies, moved to Alfred JSON format, macOS Catalina 10.15 Support. Requires Alfred 3+

 

Feedback Wanted: I'd love to hear feedback.  Are you using this workflow?  How?  What improvements would you like?

Edited by wkoffel
Link to comment

Yeah, I'm sure you won't be the only one.  Looks like a similar idea for iTerm was written by quentinsf, http://www.alfredforum.com/topic/2318-iterm-2-profiles-workflow/

 

I'm sure this particular workflow could be extended to support iTerm, I believe it actually has better AppleScript support than Terminal.

 

Yes, but I can not get the one for iTerm to work. Not sure why.

Link to comment

Just released a version 2.0 with the major addition of support for controlling iTerm2 as well as Apple Terminal.  The keywords are the same but with "iterm" instead of "term".  See top post for updates and release notes.

 

I'd love feedback from iTerm2 users in particular on what other additions would be helpful for you.  And if anyone knows how to modify the profile of a session already running, that'd be great.  That's the one feature I couldn't replicate from the Apple Terminal support.

Link to comment

I just downloaded it. It correctly gets my different iterm profiles! Yeah! The other workflow did not. I like the horizontal alignment feature and it works fine. If I do not already have an iterm window open, the iterm command with a different profile will open a default profile and the profile I stated. Could that get fixed to only opening one terminal each time? So far, that is the only problem with it.

Link to comment

I just downloaded it. It correctly gets my different iterm profiles! Yeah! The other workflow did not. I like the horizontal alignment feature and it works fine. If I do not already have an iterm window open, the iterm command with a different profile will open a default profile and the profile I stated. Could that get fixed to only opening one terminal each time? So far, that is the only problem with it.

 

Glad it's working for you!

 

I can confirm the behavior you are seeing, but only when iTerm is not already running.  When iTerm is running, but with no open windows, it behaves as expected.  Is that consistent with what you are seeing?

 

And yes, happy to see if I can find a workaround.

Link to comment

Glad it's working for you!

 

I can confirm the behavior you are seeing, but only when iTerm is not already running.  When iTerm is running, but with no open windows, it behaves as expected.  Is that consistent with what you are seeing?

 

And yes, happy to see if I can find a workaround.

 

Yes, that is exactly what I am seeing. Thanks.

Link to comment

For folks who don't want a fully-quit iTerm2 launching a default window and then this workflow launching another window on top of it....

 

Most of the solutions to preventing this are hacks in which the script would manually close the first window before opening a new one, and detecting whether a process is already running in Applescript is non-trivial.

 

The best solution is modify your iTerm2 launch behaviors.  See the following link for more info.

http://rottmann.net/2013/03/launch-iterm-2-on-startup-without-opening-a-terminal-window/

Link to comment
  • 3 months later...

Love this, but I want new terminal sessions opened as tabs instead of windows (iTerm).

 

Fuzzy76, if you hold down the SHIFT key while selecting the profile from Alfred, it should open in a new tab.  Designed to work in both iTerm and Terminal.  Let me know if that does it for you.  If you wanted, you could go into the Workflow and modify that to be the default behavior, to save having to hit SHIFT.

Link to comment
  • 2 months later...

I just posted an update (fresh link in the main post at the top) with support for Mavericks.

 

Internally, the bundled gems now include version for ruby 1.8 (shipped prior to 10.9) and 2.0.0 (ships as the system ruby on Mavericks).  Please let me know if you have any troubles running it on either, I haven't tested backwards compatibility to earlier OS X versions, but they should remain unaffected.

 

Please note the new instructions in the main post, you will need to enable Accessibility differently on Mavericks, the workflow will no longer do it for you automatically.

Link to comment
  • 1 year later...

Love this utility... any chane you could update it so it works on Yosemite? In particular the "one time password" dialog is broken now.

 

Thanks for the nice words, Alex.  Actually, that feature has been broken since 10.9, I hadn't realized until now!

 

In 10.9, Apple increased security of the UI scripting accessibility layer, so programs can't modify it. The user has to add access to Alfred manually (too bad, it's slightly confusing for users, but I understand the security benefits of their decision).  See https://developer.apple.com/library/mac/releasenotes/AppleScript/RN-AppleScript/RN-10_9/RN-10_9.html for the gory details.

 

So the utility still works great on Yosemite, I'm using it every day.  But the dialog is confusing.  I will update to provide a better error message.  In the meantime, you can add "Alfred 2.app" to the list of applications under System Preferences -> Security & Privacy -> Privacy.  See instructions at http://mizage.com/help/accessibility.html for a detailed walkthrough.

 

Hope that helps, and thanks for the reminder to update the plugin to make this clearer.

Edited by wkoffel
Link to comment

Love this utility... any chane you could update it so it works on Yosemite? In particular the "one time password" dialog is broken now.

 

Decided to just get it fixed up right.  The new version is now available, linked to from the main links up top.  You can update with alleyoop, or directly download the latest version 2.2.  If you don't yet have scripting enabled, it will show a dialog like the one below, and then helpfully open the system preferences privacy pane, so you can add Alfred to it.

 

Screen%20Shot%202014-11-21%20at%2010.52.

Let me know if that works for you Alex, and do spread the word about the plugin, always happy when people are taking advantage of it.

Link to comment
  • 1 month later...

I'm a different Alex, but just letting you know this workflow is pretty darned neat :)

 

Working exactly as advertised on OS X 10.9.5.

 

Once I got home, Alfred had Terminal Control Workflow installed due to Dropbox syncing. There was no request to allow Alfred to control my computer for me (update: probably because the script was crashing, see below).

 

The "term" keyword wasn't working: I was expecting that typing "term " would result in Alfred displaying a list of terminal settings to choose from (i.e.: Solarized Dark, Solarized Light, etc) but nothing was there. I turned on debugging in Alfred and got this message when starting to type "term":

 

 

[ERROR: alfred.workflow.input.scriptfilter] Code 1: terminal-control.rb:31:in `split': invalid byte sequence in US-ASCII (ArgumentError)

from terminal-control.rb:31:in `<main>'

 

So I'll go checkout the source version and see if I can't figure out what's going on here.

 

Edit: nevermind, I was going to post a list of my current settings and realised that "split" is choking on "ManicDee’s Settings" (with a curly apostrophe).

 

If I update the script such that term_settings = `…` is replaced with term_settings = "ManicDee’s Settings," I will get the expected behaviour (i.e.: "term " brings up a list of terminal settings). Saving the AppleScript to a separate script and running it from the command line provides the expected Unicode output (at least, it looks that way).

 

Sorted! It was Ruby treated all input as ASCII 7-bit by default. I have submitted a pull request, which simply adds "Encoding.default_external = Encoding::UTF_8" at the top of terminal-control.rb.

 

WFM, YMMV.

Edited by ManicDee
Link to comment

Sometimes I want custom SSH terminals. For example if I need to access a production web server for some reason, I'd like to have a terminal set up with a (dark) red background and a different font to normal which should remind me that I'm on a production server, so I should be careful.

  1. Set up the appropriate configuration in ~/.ssh/config (for this example, 'Host manicdee-production-web')
  2. Set up a custom terminal setting, and give it a meaningful name (for this example manic@production-web)
  3. In the terminal settings, under "Shell" type "ssh manic@production-web" into the "Run command" box

Now when I want to access that server, I can summon Alfred and issue "term manic@production-web" and I'll get the custom red terminal.

 

If only I could combine this terminal control workflow with the SSH workflow …

Link to comment

Sorted! It was Ruby treated all input as ASCII 7-bit by default. I have submitted a pull request, which simply adds "Encoding.default_external = Encoding::UTF_8" at the top of terminal-control.rb.

 

WFM, YMMV.

 

Thanks for the sleuthing, Alex.  I didn't have any UTF characters in my terminal settings, so this was a good edge case to catch.  I'll merge your pull request and get it out in an upcoming release.

Link to comment
  • 1 year later...

Note for Alfred 3 upgraders: 

 

This Terminal Control workflow has been tested, appears to work great on Alfred 3 out of the box.  Be aware that you'll need to re-authorize the app in System Preferences -> Privacy -> Accessibility, since the name of the app has changed.

Link to comment
  • 1 month later...

Hi, I'm using Alfred 2.8.5 and it says, incompatible workflow. What can I do? I don't want to upgrade to Alfred 3

 

Bummer, GFEMajor.  I looked for an "Export as Alfred 2 workflow", but that doesn't appear to be a feature.  Unfortunately, I don't have the bandwidth to maintain two separate versions, as I'd have to muck about uninstalling and reinstalling Alfred versions and making a fork of the source.  If lots of folks end up stuck in your situation, I might consider a separate mac to manage a backwards compatible version.

 

In your case, sounds like a choice of either upgrading to Alfred 3 (come on in, the water's fine!), or downgrading iTerm back to the 2.x series, so either this workflow or Quentin's ITP workflow will get the job done.

 

Sorry for the hassle.

Link to comment
  • 1 year later...

Love this tool.

Anybody having issues on MacOS High Sierra.

I have the debug information below. It appears to be looking for file 'alfredo'

 

Starting debug for 'Terminal Control'

[2017-10-05 10:11:41][ERROR: input.scriptfilter] Code 1: /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- alfredo (LoadError)
    from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from terminal-control.rb:4:in `<main>'

 

line 4 in terminal-control.rb: require "alfredo"

 

Tried to uninstall terminal and reinstall, no luck.

Edited by robdbirch
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...