Skip to content

Instantly share code, notes, and snippets.

@btisdall
Last active August 29, 2015 13:56
Show Gist options
  • Save btisdall/8972305 to your computer and use it in GitHub Desktop.
Save btisdall/8972305 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Runs the 'Duplicate iTerm tab' AppleScript, enabling iTerm 2 to run this AppleScript when a
# given keyboard shortcut is triggered.
get_tty_cwd(){
TTY=$1
PID=`ps -f | grep $TTY | head -n 1 | awk '{ print $2; }'`
lsof -a -d cwd -F n -p $PID | egrep -v 'p[0-9]+' | awk '{ sub(/^n/, ""); print; }'
}
export -f get_tty_cwd
osascript <<EOF
-- Duplicate iTerm tab.scpt
tell application "iTerm"
tell the current terminal
tell the current session
set the_name to get name
set tty_name to do shell script "basename " & (get tty)
set working_dir to do shell script "get_tty_cwd " & tty_name
end tell
launch session "Default Session"
tell the last session
set name to the_name
write text "cd '" & working_dir & "'"
end tell
end tell
end tell
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment