Hello.
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!