Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
bash function for opening up iterm form current directory
#!/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
# 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 ./
# References:
# iTerm AppleScript Examples:
# Credit:
# Forked from
# Inspired by tab.bash by @bobthecow
# link:
# OSX only
[ `uname -s` != "Darwin" ] && echo 'OS X Only' &&return
function iterm () {
local cmd=""
local wd="$PWD"
local args="$@"
if [ -d "$1" ]; then
wd=$(cd "$1"; pwd)
if [ -n "$args" ]; then
# echo $args
osascript <<EOF
tell application "iTerm"
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
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

This comment has been minimized.

Copy link

@theRealSuperMario theRealSuperMario commented Apr 4, 2021

Unfortunately, I could not get this to work (totally lack of skills on my side), which is why I simply moved over to python

Putting this here in case someone might find it useful.

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