Applescript to open a NEW terminal window in current space

14

10

Yes, I'm having a terrible newbie experience with Apple Script.

I need to open a new Terminal window in the current desktop space. NOT move me to another space which has a Terminal running and then open another Terminal window.

Of course, if Terminal is not running then it should start a new Terminal process.

zaf

Posted 2010-10-04T12:54:00.003

Reputation: 411

Answers

18

tell application "Terminal"  
    do script " "  
    activate  
end tell

It seems weird but it takes advantage of an oddity in how Terminal handles incoming "do script" commands; it creates a new window for each one. You could actually replace that with something useful if you want; it'll execute whatever you want just after opening the new window.

Justin Mrkva

Posted 2010-10-04T12:54:00.003

Reputation: 923

1This works but OSX still moves spaces automatically to whatever space had open terminal windows. If I disable that feature in system preferences it fixes it but now all my other applications no longer move me to a space with open windows for that app. I hate launching an app only to find that no windows appear, just the menu bar at the top shows that application has focus. So bizarre. – Chev – 2013-11-25T23:39:03.490

15

If you don't have any text in-between the do script " " you won't get an extra command prompt in the terminal.

tell application "Terminal"  
    do script ""  
    activate  
end tell

Adam

Posted 2010-10-04T12:54:00.003

Reputation: 251

1I think it's okay if we leave it as an answer – you could alternatively suggest an edit on @jfm's answer to further improve it and delete yours. – slhck – 2011-11-29T20:55:59.613

8

I can think of three different ways to do it (the first two stolen from somewhere else but I forget where). I use the third one, which calls a shell script from the applescript, because I want to open a new window every time and because it was the shortest.

Unlike the script built into OS X since at least 10.10, all of these open the terminal in whatever directory is the current working directory in your finder window (i.e. you don't have to have a folder selected in order to open it).

Also included a couple of bash functions to complete the Finder > Terminal > Finder circle.

1. Reuse an existing tab or create a new Terminal window:

tell application "Finder" to set myDir to POSIX path of (insertion location as alias)
tell application "Terminal"
    if (exists window 1) and not busy of window 1 then
        do script "cd " & quoted form of myDir in window 1
    else
        do script "cd " & quoted form of myDir
    end if
    activate
end tell

2. Reuse an existing tab or create a new Terminal tab:

tell application "Finder" to set myDir to POSIX path of (insertion location as alias)
tell application "Terminal"
    if not (exists window 1) then reopen
        activate
    if busy of window 1 then
        tell application "System Events" to keystroke "t" using command down
    end if
    do script "cd " & quoted form of myDir in window 1
end tell

3. Generate a new window each time via a shell script called from an applescript

tell application "Finder"
    set myDir to POSIX path of (insertion location as alias)
    do shell script "open -a \"Terminal\" " & quoted form of myDir
end tell

4. (BONUS) Bash alias to open a new finder window for the current working directory in your terminal

Add this alias to your .bash_profile.

alias f='open -a Finder ./' 

5. (BONUS) Change directory in your terminal window to the path of the front Finder window

Add this function to your .bash_profile.

cdf() {
      target=`osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)'`
        if [ "$target" != "" ]; then
            cd "$target"; pwd
        else
            echo 'No Finder window found' >&2
        fi
}

shibaku

Posted 2010-10-04T12:54:00.003

Reputation: 81

0

The answers above only works if Terminal is already running. Otherwise it opens two Terminal windows at once - one because of the do script and one because of activate.

You can prevent this with a simple if ... else:

if application "Terminal" is running then
    tell application "Terminal"
        do script ""
        activate
    end tell
else
    tell application "Terminal"
        activate
    end tell
end if

Bonus:

If you want to directly run commands you can do this through keystrokes (Not very elegant - I know! But it works)

[...]
else
    tell application "Terminal"
        activate
        tell application "System Events" to keystroke "ls -la" 
        tell application "System Events" to key code 36
    end tell
end if

Peter Piper

Posted 2010-10-04T12:54:00.003

Reputation: 101