#!/bin/zsh -f

if [[  $TERM_PROGRAM != iTerm.app ]]; then
    open -a Terminal
    return 0
fi

# First, get the directory for the new tab

if [[ $# == 0 ]]; then
      ThisDirectory=$PWD
elif [[ $# == 1 && -d "$1" ]]; then
      ThisDirectory="$@"
else
      print "usage: ntab [directory]"
      return 1
fi

osascript <<-eof
 tell app "Finder"
 activate
tell application "iTerm"
	-- make new terminal
	tell the front terminal 
		activate current session
		launch session "Default Session"
		tell the current session
			write text "cd \"$ThisDirectory\""
		end tell
	end tell
end tell
tell application "Finder"
	activate
end tell
tell application "iTerm"
	activate
end tell
 end tell
eof
