-
Notifications
You must be signed in to change notification settings - Fork 3
/
iterm.sh
executable file
·71 lines (63 loc) · 1.94 KB
/
iterm.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env bash
# SOURCE: https://gist.github.com/aberezin/f1e15db7181cdf062c3e7fb5ef6bc735
# If iterm2.app is closed, 2 windows will be opended by this script. To prevent this, Iterm2 > Settrings > General > Startup > "Only Restore Hotkey Window"
# 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' && exit
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 $@