使用打开的终端windows目录路径在OSX(Snow Leopard)中打开一个新的终端选项卡

时间:2009-10-19 14:48:35

标签: bash macos terminal zsh

我一直在谷歌上寻找一种简单的方法来做这件事,我找不到一个。

我有一个自定义终端环境设置(zsh),其中包含各种别名和功能,以简化操作。我经常遇到的一件事是我会快速APPLE-t创建一个新选项卡,然后输入相对于我刚刚进入的终端窗口路径的命令。这总是失败,因为新选项卡的路径是〜/而不是我刚刚使用的任何东西! 是否有任何想法让脚本将新终端选项卡的目录路径设置为打开选项卡的目录路径?

最值得赞赏的任何帮助。

伊恩

5 个答案:

答案 0 :(得分:56)

我使用了几个脚本:

dup (带工作目录的新窗口):

#!/bin/sh
pwd=`pwd`
osascript -e "tell application \"Terminal\" to do script \"cd $pwd; clear\"" > /dev/null

tup (具有相同工作目录的新标签页):

#!/bin/sh

pwd=`pwd`
osascript -e "tell application \"Terminal\"" \
    -e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
    -e "do script \"cd $pwd; clear\" in front window" \
    -e "end tell"
    > /dev/null

答案 1 :(得分:6)

没有脚本编写的另一个解决方案是iTerm2,它内置了此功能。它还有更多功能,值得一试。

答案 2 :(得分:4)

您可以通过修改http://www.entropy.ch/blog/Mac+OS+X/2008/06/27/Terminal-Tricks-“term”-revisited-with-tabs中找到的BASH脚本来获得所需内容。这是脚本,取自Marc Linyage的网站www.entropy.ch/blog

#!/bin/sh
#
# Open a new Mac OS X terminal window or tab in the current or another
# directory and optionally run a command in the new window or tab.
#
# - Without any arguments, the new terminal window opens in
#   the current directory, i.e. the executed command is "cd $PWD".
# - If the first argument is a directory, the new terminal will "cd" into
#   that directory before executing the remaining arguments as command.
# - The optional "-t" flag executes the command in a new tab 
#   instead of a new window.
# - The optional "-x" flag closes the new window or tab
#   after the executed command finishes.
# - The optional "-p" flag takes an argument of the form x,y (e.g. 40,50) and
#   positions the terminal window to the indicated location on the screen
# - The optional "-s" flag takes an argument of the form w,h (e.g. 800,400) and
#   resizes the terminal window to the indicated width and height in pixels.
#
# Written by Marc Liyanage <http://www.entropy.ch>
#
# Version 2.1
#

set -e

while getopts xtp:s: OPTION; do
    [ $OPTION = "x" ] && { EXIT='; exit'; }
    [ $OPTION = "t" ] && { TAB=1; }
    [ $OPTION = "p" ] && { POSITION="set position of window 1 to {$OPTARG}"; }
    [ $OPTION = "s" ] && { SIZE="set size of window 1 to {$OPTARG}"; }
done

for (( $OPTIND; $OPTIND-1; OPTIND=$OPTIND-1 )); do shift; done

if [[ -d "$1" ]]; then WD=$(cd "$1"; pwd); shift; else WD=$PWD; fi


COMMAND="cd '$WD' && echo -n \$'\\\\ec';"
for i in "$@"; do
COMMAND="$COMMAND '$i'"
done

if [ $TAB ]; then

osascript 2>/dev/null <<EOF
tell application "System Events"
    tell process "Terminal" to keystroke "t" using command down
end
tell application "Terminal"
    activate
    do script with command "$COMMAND $EXIT" in window 1
    $POSITION
    $SIZE
end tell
EOF

else

osascript <<EOF
tell application "Terminal"
    activate
    do script with command "$COMMAND $EXIT"
    $POSITION
    $SIZE
end tell
EOF

fi

答案 3 :(得分:4)

好的,所以我的方式是我再次回答我自己的问题(至少接近回答它)

我发现上面的一个较简洁的脚本(由Dan Benjamin提供)似乎可以解决问题,尽管这两个脚本在成功完成之前输出了类似的错误。我已经通过在脚本的末尾添加清除来解决这个问题,所以这不是什么大问题。

我说我几乎已经解决了我自己的问题,因为我的目标是找到一种方法来实现这一点,Apple-t键命令已被烧入我的肌肉记忆中作为任何新标签的快捷方式,谢谢在各种网络浏览器中无数个小时。我可以使用像Dan这样的脚本管理的最好的是t-return,这不是最大的区别,但是足够大以至于每次发出上述命令时我都会略微烦恼。我知道,我应该放手.....但我不能,这可能是我最初陷入这个烂摊子的地方,无休止地摆弄着电脑。我离题了,这是我正在使用的剧本:

#!/bin/sh

# Make a new OS X Terminal tab with the current working directory.

if [ $# -ne 1 ]; then
    PATHDIR=`pwd`
else
    PATHDIR=$1
fi

/usr/bin/osascript <<EOF
activate application "Terminal"
tell application "System Events"
    keystroke "t" using {command down}
end tell
tell application "Terminal"
    repeat with win in windows
        try
            if get frontmost of win is true then
                do script "cd $PATHDIR; clear" in (selected tab of win)
            end if
        end try
    end repeat
end tell
EOF
clear

为了完整性,如果省略尾部清除,则在征求窗口中出现错误:

2009-10-20 01:30:38.714 osascript[20862:903] Error loading /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types:  dlopen(/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types, 262): no suitable image found.  Did find:
    /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no matching architecture in universal wrapper
osascript: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" declares no loadable handlers.
tab 2 of window id 13942

答案 4 :(得分:1)

在我的回答here中,我提供了一个函数和一个别名:

function cd () { command cd "$@"; echo "$PWD" > /tmp/CWD; }
export cd

alias cdp='cd $(cat /tmp/CWD)'

您应该能够在~/.bashrc~/.zshrc的末尾添加(可能是有条件的)语句来执行该别名。