A versatile Finder toolbar droplet for opening Terminal windows

Hello. :slight_smile:

Edit: Save the script as an application, that doesn’t stay open, and displays no opening screen.

It should be named Crt.App, and dragged to your Finders toolbar. When clicked upon it will open a window there, with the current path of the Finder window as its current directory.

It will open a tab, or use an unused tab, in an existing Terminal window if such exists.

You can also drag Finder items onto it, several, and have them open in separate tabs, with the current directory set to the folder, if it is a folder, or the folder containing the file, if it is a file.

I use this, having set up Terminal.app to be shared among all spaces, and will then have no more windows, than I opt for.

The feature of reusing tabs works best with the accompanying prompt file, which is to be sourced from your .bashrc file. This gives you easy access to history numbers when you want to re-execute commands.

Then you can just enter ! 2 (enter) to rexecute command #2 and so on.

Tt is also necessarey to make the number of words in the window’s/tab’s command history predictable, as that is the parameter for checking if the terminal window is unused: hit cmd-K, when you are done with a tab, and you are ready to reuse it. This gives you the benefit of having your previous command history there.


(*
    I owe a lot to Nik who set me onto the idea, and Marc Linayage, who's script served as the foundation for this,
    though there is nothing left here.
    -- http://nik.me/blog/applescript-tricks-what-do-when-frontmost-window-isnt-frontmost
    -- http://www.entropy.ch/software/applescript/
    
    Crt for Snow leopard and later. To be installed on finders toolbar.

    Makes a new tab in current terminal window or creates a new window if none available, i.e terminal not running
     reuses a tab if one is unused.

    This applet works best if you have terminal set up to be active in all spaces, in the "Spaces and Expose Preference pane".

    The feature of reusing tabs works best with the accompanying prompt file, which is to be sourced from your .bashrc file.
    This gives you easy access to history numbers when you want to re-execute commands. it is also necessarey to make the 
    number of words in your command history predictable, as that is the parameter for checking if the terminal window is 
    unused: hit cmd-K, when you are done with a tab, and you are ready to reuse it. This gives you the benefit of having 
    your previous command history there.
Then you can just enter [b]! 2 (enter)[/b] to rexecute command #2 and so on.
    
    Dropping files onto it on the toolbar, in order to change to that directory works too, great when having a spotlight window up.
    
    Save it as an applet, and it deserves a nice Icon!

    No Copyright, as I have used others works so extensively, and no warranties what so ever. But please, have the decency to 
    refer to this post at Macscripter.net, or My upcoming github repository (with icon there!) (http://macscripter.net/viewtopic.php?id=39017)

    
    To get a nice titles for your terminal windows, look here:
    -- http://superuser.com/questions/79972/set-the-title-of-the-terminal-window-to-the-current-directory
    
    
    Enjoy! McUsr
*)
global multiple_items, first_item
on run
    set {multiple_items, first_item} to {false, true}
    set this_path to quoted form of POSIX path of my getCurrentFolder()
    my termScriptInNewTab("cd " & this_path)
end run

-- Processes Items dropped onto the toolbar applet icon

on open these_items
    if (count of these_items) > 1 then
        set multiple_items to true
    else
        set multiple_items to false
    end if
    set first_item to true
    
    repeat with this_item in these_items
        my process_item(this_item)
    end repeat
end open

-- this subroutine does the actual work
on process_item(this_item)
    copy this_item to probe_item
    set this_item to this_item as alias
    set the_path to POSIX path of this_item
    set isFolder to false
    if the_path ends with "/" then set isFolder to true
    if not isFolder then
        repeat until the_path ends with "/"
            set the_path to text 1 thru -2 of the_path
        end repeat
        set the_path to quoted form of (characters 1 thru -2 of the_path as text)
    else
        set the_path to quoted form of POSIX path of (characters 1 thru -2 of the_path as text)
    end if
    my termScriptInNewTab("cd " & the_path)
end process_item


on termScriptInNewTab(theStartOfCmd)
    -- http://nik.me/blog/applescript-tricks-what-do-when-frontmost-window-isnt-frontmost
    -- This version is tinkered a little bit, in order to work from an applet, and tinkered to reuse tabs.
    
    set cmd to theStartOfCmd & " && echo -n  $'\\ec'"
    tell application "Terminal"
        activate
        set termWins to count of windows
        set frontWin to 0
        repeat with i from 1 to (count of windows)
            try
                set tabCount to count of tabs of window i
                set frontWin to i
                exit repeat
            end try
        end repeat
        if frontWin is 0 then
            do script with command cmd
            set my first_item to false
        else
            if (multiple_items and first_item is true) or multiple_items is false then
                set first_item to false
                set frontmost of window frontWin to true
                
                set tabToUse to -1
                repeat with j from 1 to tabCount
                    if (count of every word of (get history of tab j of window 1 as text)) is 2 then
                        set tabToUse to j
                        exit repeat
                    end if
                end repeat
                if tabToUse is not -1 then
                    set selected of tab tabToUse of its window 1 to true
                    do script with command cmd in (tab tabToUse of window 1)
                else
                    tell application "System Events" to tell (first application process whose name is "Terminal") to keystroke "t" using {command down}
                    set xi to 0
                    repeat until (count of tabs of window 1) is (tabCount + 1)
                        if xi ≤ 4 then -- don't wait more than one second
                            delay 0.25
                        else
                            set xi to -1
                            exit repeat
                        end if
                    end repeat
                    if xi is not -1 then
                        do script with command cmd in (last tab of window 1)
                    else
                        do script with command cmd
                    end if
                end if
            else
                -- multiple items is true and first item is false  
                tell application "System Events" to tell (first application process whose name is "Terminal") to keystroke "t" using {command down}
                set xi to 0
                repeat until (count of tabs of window 1) is (tabCount + 1)
                    if xi ≤ 4 then -- don't wait more than one second
                        delay 0.25
                    else
                        set xi to -1
                        exit repeat
                    end if
                end repeat
                do script with command cmd in (last tab of window 1)
                
            end if
        end if
    end tell
end termScriptInNewTab


on getCurrentFolder()
    tell application "Finder"
        set theFolder to (path to desktop folder as alias)
        repeat with i from 0 to (count of windows)
            try
                get folder of window i
                set theFolder to result
                exit repeat
            end try
        end repeat
        return theFolder as alias
    end tell
end getCurrentFolder

The prompsetup file that are to be sourced from your ~/.bashrc file:

[code]#! /bin/bash

This file is to be sourced in your .bashrc.

To install promptsetup in your ~/.bashrc; insert

this: “source ~/.init/promptsetup” The current

promptcolor is set to black to set other colors for

your prompts: http://linuxgazette.net/issue65/padala.html

The number for black (30) is the only you should have to

replace, if you can accept to have the prompt

towards the background of your terminal window.

export promptUsage=“Usage : prompt [-h | -u |-v ]|[ - | PWP | PWT | PWN | DS ]”
function promptHelp()
{
echo "prompt : “$promptUsage”
PWP : Prompt With Path
PWT : Prompt With Time - Can be updated to update the time.
NRP : Normal Prompt.
DS : Dollar sign " 1>&2
}

function prompt()
{
case “$1” in
PWP | PWT | PWN | DS)
export OLPS1=“$PS1”
export OLDPROMPTYPE=$PROMPTTYPE
PROMPTTYPE=$1
if [ “$PROMPTTYPE” = “PWP” ]; then
PS1=“\w$”
elif [ “$PROMPTTYPE” = “PWT” ]; then
PS1=“\t:$”
elif [ “$PROMPTTYPE” = “PWN” ]; then
PS1=‘[\e]2;${PWD}\a
\e[1;30m]
!$
[\e[m]’
else
PS1=“$”
fi
export PS1
export PROMPTTYPE ;;
-h)
promptHelp ;;
-u)
echo $promptUsage 1>&2 ;;
-) PROMPTTMP=“$PS1”
export PS1=“$OLPS1”
export OLPS1=“$PROMPTTMP”
PROMPTTYPE_TEMP=$PROMPTTYPE
export PROMPTTYPE=$OLDPROMPTYPE
export OLDPROMPTYPE=$PROMPTTYPE_TEMP;;
*) echo -n “prompt - bad parameter”
return 1;;
esac
}
prompt PWN
PROMPTTYPE=PWN

PS2=“>>”

I couldn’t manage to enter escape sequences into PS3

PS3=‘choice nr : ’
PS4=’$0 line $LINENO:’ # Bash3 side 224.
export PS1 PS2 PS3 PS4 PROMPTTYPE[/code]
Enjoy! :wink:

Hello! :slight_smile:
I have decided I will copyright it after all, and put it into public domain.

Portions of the code is copyright Nik, http://nik.me/
Some copyright Marc Liynage at http://www.entropy.ch/home/

I have put a the applet with an icon, a readme file, the promptsetup script, and an Applescritp script to interface it with Quick Silver, here It is https://dl.dropbox.com/u/6829111/CRT.zip

I can only guarrantee that it works with Snow Leopard and later.

Enjoy!

Hello! :slight_smile:

I have improved the applet, the documentation and put in scripts to interface it with Quicksilver, BBedit and TextWrangler.

it can be found here http://dl.dropbox.com/u/6829111/CRT.zip

Enjoy! :wink:

Good stuff.

QS needs to be quit and restarted after the script is moved to its Actions folder.

Thanks! :slight_smile:

I’ll add that to the documentaion, and a few more scripts to interface it with

AppleScript Editor
Smile
Script Debugger 4.5
Xcode

Hello!

I have updated the applet/droplet today, making it react as fast as possible, as it is only slow sometimes, It is hard to tell if it is any gain in having the handlers run scripts. But it can’t hurt, and it at least doesn’t seem to work slower!
It also caters for a more robust applet, not that it has been needed. (Version 0.3)

I use this daily, now I just take it for granted! :slight_smile: That I have no clutter of terminal windows floating all over the place!

Here is the new version: I am going to write the interfacing scripts for other apps right away!

Here it is!

Hello!

I added the parameter for the run handler in the script that is run from the open handler … :slight_smile:

The fixed version is here

Hello!

Latest edition now includes a script that handles, AppleScript Editor, Smile, Script Debugger and Xcode in the sense that it opens a terminal window to the directory containing the file in the frontmost window. (It can be invoked from Quicksilver

I have also given a notice about restarting Quicksilver in the documentation!

It can be found here!

Hello!

I have updated it with scripts for opening XCode’s current project’s Debug, and Release Folders in terminal windows.

Nothing hinders you from doing open . from there! :slight_smile:

it can be found here!