19

私はこれを行う簡単な方法をしばらくグーグルで探していましたが、見つけることができません。

作業を簡単にするために、さまざまなエイリアスと関数を使用してカスタム端末環境(zsh)をセットアップしています。私が遭遇し続けることの1つは、新しいタブをすばやく作成してから、今いたターミナルウィンドウのパスに関連するコマンドを入力することです。新しいタブのパスが〜/であるため、これは常に失敗します。私が使っていたものの代わりに!新しいターミナルタブのディレクトリパスを開始タブのディレクトリパスに設定するスクリプトのアイデアはありますか?

どんな助けでも大歓迎です。

イアン

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

スクリプトを使用しないもう 1 つのソリューションは、この機能が組み込まれているiTerm2です。チェックする価値のあるさらに多くの機能があります。

于 2011-05-17T23:23:34.477 に答える
4

OK、私のやり方で、私は自分の質問に再び答えています(とにかく、少なくとも答えに近づいています)

上記のスクリプトよりも冗長でないスクリプト ( Dan Benjaminの厚意により提供) を見つけましたが、どちらのスクリプトも正常に完了する前に同様のエラーを出力します。スクリプトの最後にclearを追加することで対処したので、大きな問題はありません。

私は自分の問題をほぼ解決したと言っています。なぜなら、数え切れないほどの時間のおかげで、何かの新しいタブへのショートカットとして筋肉の記憶に焼き付けられた 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
于 2009-10-20T00:32:04.007 に答える
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 に答える
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 に答える