8

I want to create and run a bash function that:

  1. cd into a project's directory
  2. Open a new tab in Terminal in the same directory
  3. Open my dev tools and start dev services

Item #2 doesn't work as expected, the new tab doesn't change directory.

Here is the script (both functions residing in my .bashrc file:

# Open a new tab (needs a path as an argument)
new_terminal_tab(){
    osascript -e "tell application \"Terminal\"" \
                        -e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
                        -e "do script \"cd $1; clear;\" in front window" \
                        -e "end tell"
                        > /dev/null
}

# Set up workspace
affiliatesForm(){
    # cd into the project's directory
    cd /Users/iamuser/Documents/path/to/project\ file
    # Get the working directory
    pwd=`pwd`

    # Open a new tab in Terminal and cd into project's directory
    # The idea is to have a tab with Rails server output, and another tab in the project's directory
    new_terminal_tab $pwd

    # Open the project in Sublime Text 2
    subl $pwd

    # Start the Rails server
    rails server
}

What am I doing wrong? What is this code doing that I don't know?


Update

The path for the directory I am trying to access has spaces in it. But escaping the space is not helping at all. The script works with paths that do not include spaces.

5 Answers 5

17

Opening a new tab in Terminal should by default retain the current directory. If it doesn't, you may've broken the $PROMPT_COMMAND.

Also remember to not replace the previous value if you intend to add custom behaviour by adding ; $PROMPT_COMMAND at the end.

PROMPT_COMMAND="my_custom_function; $PROMPT_COMMAND"

For reference, here's the default from /etc/bashrc (OS X 10.9):

# Tell the terminal about the working directory at each prompt.
if [ "$TERM_PROGRAM" == "Apple_Terminal" ] && [ -z "$INSIDE_EMACS" ]; then
    update_terminal_cwd() {
        # Identify the directory using a "file:" scheme URL,
        # including the host name to disambiguate local vs.
        # remote connections. Percent-escape spaces.
        local SEARCH=' '
        local REPLACE='%20'
        local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
        printf '\e]7;%s\a' "$PWD_URL"
    }
    PROMPT_COMMAND="update_terminal_cwd; $PROMPT_COMMAND"
fi
2
  • $PROMPT_COMMAND is not broken. Any other suggestions?
    – sargas
    Apr 28, 2014 at 17:43
  • 1
    This fix my problem, though it would be useful to explain why $PROMPT_COMMAND is used for current directory.
    – isevcik
    Oct 1, 2019 at 20:11
7

I know this question is a little dated but I just found an answer that would be useful for iTerm2 users on Mac OS X.

Under the "profile tab" in iTerm2 preferences you can set exactly where iterm opens up new tabs under the "Working Directory" heading

iterm preferences

After that, close and restart iTerm and you should be good to go!

1
  • This is exactly what I want. Tanks ;) Dec 3, 2016 at 15:22
1

So as it was mentioned, my script worked with paths that didn't include spaces. It turns out that the solutions lies in wrapping the variable (inside new_terminal_tab()) in single quotes like so:

-e "do script \"cd '$1'; clear;\" in front window" \

Amazing what quotes can do isn't it?

0

The service provided here works for me. It opens a new Terminal tab at the folder I ran the service from.

I did encounter the problem that when I already had a Terminal window open it would open a new one. I solved it as follows:

on run {input, parameters}
    repeat with this_item in input
        set the_path to POSIX path of this_item
        repeat until the_path ends with "/"
            set the_path to text 1 thru -2 of the_path
        end repeat

        set numberOfWindows to "0"

        tell application "Terminal"
            activate
            set numberOfWindows to (numberOfWindows + (count windows))
            if numberOfWindows is 0 then
                do script with command "cd " & quoted form of the_path
                activate
            else
                activate
                delay (0.3)
                tell application "System Events"
                    keystroke "t" using command down
                end tell
                do script with command "cd " & quoted form of the_path in window 0
            end if

        end tell
    end repeat
    return input
end run

All credit for this code goes to Jeroen Leenarts. I just made some small adjustments.

1
  • that's an interesting script. But my idea is to type a command in Terminal and have my applications opened as well as the two tabs in Terminal, one for Rails Server, another in the directory where the project resides.
    – sargas
    Apr 29, 2014 at 15:45
0

I use an app called go2shell. This gives you a little button on Finder that opens the current directory in a terminal.

https://itunes.apple.com/es/app/go2shell/id445770608?mt=12

You can also use the terminal to cd to the folder you want, and use 'open .'

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .