Skip to content

Instantly share code, notes, and snippets.

@aberezin
Forked from vyder/iterm
Created June 22, 2018 22:53
Show Gist options
  • Save aberezin/f1e15db7181cdf062c3e7fb5ef6bc735 to your computer and use it in GitHub Desktop.
Save aberezin/f1e15db7181cdf062c3e7fb5ef6bc735 to your computer and use it in GitHub Desktop.
iterm.bash - Launch iTerm from command line
#!/usr/bin/env bash
#
# Open new iTerm window from the command line using v3 syntax for applescript as needed in iTerm2 Version 3+
# This script blocks until the cmd is executed in the new iTerm2 window. It then leaves the window open.
# TODO Add option to close iTerm2 after cmd execs
# See also https://www.iterm2.com/documentation-scripting.html
#
# Usage:
# iterm Opens the current directory in a new iTerm window
# iterm [PATH] Open PATH in a new iTerm window
# iterm [CMD] Open a new iTerm window and execute CMD
# iterm [PATH] [CMD] ... You can prob'ly guess
#
# Example:
# iterm ~/Code/HelloWorld ./setup.sh
#
# References:
# iTerm AppleScript Examples:
# https://gitlab.com/gnachman/iterm2/wikis/Applescript
#
# Credit:
# Forked from https://gist.github.com/vyder/96891b93f515cb4ac559e9132e1c9086
# Inspired by tab.bash by @bobthecow
# link: https://gist.github.com/bobthecow/757788
#
# OSX only
[ `uname -s` != "Darwin" ] && echo 'OS X Only' &&return
function iterm () {
local cmd=""
local wd="$PWD"
local args="$@"
if [ -d "$1" ]; then
wd="$1"
args="${@:2}"
fi
if [ -n "$args" ]; then
# echo $args
cmd="$args"
fi
# osascript &>/dev/null <<EOF
osascript <<EOF
tell application "iTerm"
activate
set new_window to (create window with default profile)
set cSession to current session of new_window
tell new_window
tell cSession
delay 1
write text "cd $wd;$cmd"
delay 2
repeat
delay 0.1
-- display dialog cSession is at shell prompt
set isdone to is at shell prompt
if isdone then exit repeat
end repeat
end tell
end tell
end tell
EOF
}
iterm $@
@hyproman
Copy link

Note that the name "iTerm" may need to be changed to "iTerm2" in the AppleScript script for this to work.

@webcpu
Copy link

webcpu commented Mar 28, 2019

It doesn't work for me. I have to split it into two lines instead.

          # write text "cd $wd;$cmd"
          write text "cd $wd"
          write text "$cmd"

@nivaca
Copy link

nivaca commented Apr 2, 2019

I had to replace line 56 with the following:

write text "cd '$wd'; cmd"

Otherwise, I would get an error with paths containing spaces.

@sammndhr
Copy link

You can also use open -a iTerm .
To create an alias, add the line alias iterm="open -a iTerm ." to ~/.aliases. Then run source ~/.aliases from the command line.

@geyang
Copy link

geyang commented Jul 14, 2020

This script has two issues:

  1. The last line blows up by recursively creating more iterm windows. This line need to be removed.
  2. iterm . does not resolve the path correctly because . is called in the new window.

My version of this script fixes both issues: gist/geyang/iterm.bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment