[英]Opening a new terminal tab in OSX(Snow Leopard) with the opening terminal windows directory path
我一直在谷歌搜索寻找一种简单的方法来做到这一点,我找不到一个。
我有一个自定义终端环境设置(zsh)与各种别名和功能,以使事情更容易。 我经常遇到的一件事是我会快速APPLE-t创建一个新选项卡,然后输入相对于我刚刚进入的终端窗口路径的命令。这总是失败,因为新选项卡的路径是〜/而不是我刚刚使用的任何东西! 脚本将新终端选项卡的目录路径设置为打开选项卡的目录路径的任何想法?
任何帮助最受赞赏。
伊恩
我使用了几个脚本:
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
没有脚本的另一个解决方案是iTerm2 ,它内置了这个功能。它还有更多功能,值得一试。
您可以通过修改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
好的,所以我的方式是我再次回答我自己的问题(至少接近回答它)
虽然两个脚本在成功完成之前输出了类似的错误,但我发现上面的一个较简洁的脚本(由Dan Benjamin提供 )似乎可以解决问题。 我通过在脚本的末尾添加清楚来解决这个问题,这不是什么大问题。
我说我几乎已经解决了我自己的问题,因为我的目标是找到一种方法来实现这一点,Apple-t键命令被烧成了我的肌肉记忆,作为任何东西的新标签的快捷方式,多亏了无数个小时在各种Web浏览器中。 我可以使用像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
在我的答案在这里 ,我提供的功能和别名:
function cd () { command cd "$@"; echo "$PWD" > /tmp/CWD; }
export cd
alias cdp='cd $(cat /tmp/CWD)'
您应该能够在~/.bashrc
或~/.zshrc
的末尾放置一个(可能是条件的)语句来执行该别名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.