簡體   English   中英

使用打開的終端windows目錄路徑在OSX(Snow Leopard)中打開一個新的終端選項卡

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM