Opening a new terminal tab in OSX(Snow Leopard) with the opening terminal windows directory path

前端 未结 5 812
难免孤独
难免孤独 2021-02-01 20:37

I\'ve been Googling for a while looking for a simple way to do this, and I can\'t find one.

I have a custom terminal environment set up (zsh) with various aliases and fu

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 20:59

    You can get what you want by modifying the BASH script found at http://www.entropy.ch/blog/Mac+OS+X/2008/06/27/Terminal-Tricks-“term”-revisited-with-tabs. Here is the script, taken from Marc Linyage's site 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 
    #
    # 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 <

提交回复
热议问题