Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

Opening a new tab in Terminal should by default retain the current directory. If it doesn't, make sure you haven't broken the $PROMPT_COMMAND. Here's the default from /etc/bashrc:

# 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
share|improve this answer
    
$PROMPT_COMMAND is not broken. Any other suggestions? –  sargas Apr 28 at 17:43
up vote 1 down vote accepted

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?

share|improve this answer

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.

share|improve this answer
    
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 at 15:45

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 .'

share|improve this answer
1  
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. –  grgarside Apr 29 at 5:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.