Jump to content

Executing iTerm2 terminal commands in current shell

Recommended Posts

I like the terminal support in Alfred (prefacing commands with '>') but it always opens a new terminal window. I would prefer that the commands execute in the current terminal window. Luckily there's a new 'Custom' option for handling terminal commands. The following was taken almost verbatim from the iTerm site (http://code.google.com/p/iterm2/wiki/AppleScript) and will execute terminal commands in the active iTerm2 shell. It will launch iTerm2 if it isn't currently running.



on alfred_script(q)
    tell application "iTerm"
        try -- We don't need error messages
            set miniaturized of windows to false
        end try
            set _session to current session of current terminal
        on error
            set _term to (make new terminal)
            tell _term
                launch session "Default"
                set _session to current session
            end tell
        end try
        tell _session
            write text q
        end tell
    end tell
    tell application "System Events"
        set visible of process "iTerm" to true
    end tell
end alfred_script

Share this post

Link to post

Maybe you could help me with something, as I’m trying to do something similar.


I’d like commands passed through Alfred to run in a new iTerm2 window, regardless of whether it is already running or not. The issue is that if it’s not already running, Alfred opens two windows. I’ve been making some experiments, and it does not seem to work. It succeeds if run from within Applescript Editor, but not from Alfred’s custom option for terminal commands.


I’ve posted the script I’m working with (based on yours) on the forum http://www.alfredforum.com/topic/1180-extra-tab-is-open-when-using-iterm2/#entry5666. Any help on how to achieve that would be appreciated.

Share this post

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...