#!/bin/zsh -f

# Execute a command in the same directory but in a new tab

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

# First, get the directory for the new tab

ThisDirectory=$PWD

if [[ $# == 0  ]]; then
      print "usage: $0 [commands]"
      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 last 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
