19

我在谷歌上搜索了一段时间,寻找一种简单的方法来做到这一点,但我找不到。

我设置了一个自定义终端环境(zsh),其中包含各种别名和功能,以使事情变得更容易。我一直遇到的一件事是,我将快速 APPLE-t 创建一个新选项卡,然后键入一个相对于我刚才所在的终端窗口路径的命令。这总是失败,因为新选项卡的路径是 ~/而不是我刚刚使用的任何东西!脚本将新终端选项卡的目录路径设置为打开选项卡的目录路径有什么想法吗?

非常感谢任何帮助。

伊恩

4

5 回答 5

55

我有几个我使用的脚本:

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
于 2011-10-27T02:26:05.923 回答
6

另一个没有脚本的解决方案是iTerm2,它内置了这个特性。它有更多的特性,也值得一试。

于 2011-05-17T23:23:34.477 回答
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
于 2009-10-19T20:08:55.390 回答
4

好的,我的方式也是如此,我再次回答我自己的问题(无论如何至少接近回答了)

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

我说我几乎已经解决了我自己的问题,因为我的目标是找到一种方法来完成这个问题,使用 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
于 2009-10-20T00:32:04.007 回答
1

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

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

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

您应该能够在您的~/.bashrc~/.zshrc执行该别名的末尾放置一个(可能有条件的)语句。

于 2009-10-19T17:25:37.290 回答