Man muss sehr vorsichtig sein, wenn man Saiten durch verschiedene Umgebungen führt.
Ich führe 10.4 aus, daher öffnet mein 'tfork'-Skript stattdessen immer ein neues Fenster. Es sollte einfach sein, es an die Verwendung eines Tabs anzupassen:
#!/bin/sh
# source: http://www.pycs.net/bob/weblog/2004/02/23.html#P49
# Rewritten to use osascript args -> run handler args.
# Added ability to pass additional initial command and args to new shell.
# Bug: Non ASCII characters are unreliable on Tiger.
# Tiger's osascript seems to expect args to be encoded in
# the system's primary encoding (e.g. MacRoman).
# Once in AppleScript, they are handled OK. Terminal sends them
# back to the shell as UTF-8.
test $# -eq 0 && set -- : # default command if none given
osascript - "$(pwd)" "$@" <<\EOF
on run args
set dir to quoted form of (first item of args)
set cmd_strs to {}
repeat with cmd_str in rest of args
set end of cmd_strs to quoted form of cmd_str
end
set text item delimiters to " "
set cmd to cmd_strs as Unicode text
tell app "Terminal" to do script "cd " & dir & " && " & cmd
end
EOF
Beispiel: tfork git log -p ..FETCH_HEAD
Änderung: cwd eines bereits laufenden Prozesses, der eine Terminal- Registerkarte belegt
Die Vorstellung, dass „das aktuelle Verzeichnis des Programms die aktuelle Registerkarte belegt“, ist nicht so offensichtlich, wie man es erwarten würde.
Jede Registerkarte " Terminal" verfügt über ein einzelnes tty-Gerät, das von den von ihr ausgeführten Prozessen verwendet wird (anfangs eine Shell; danach, unabhängig davon, welche Shell gestartet wird).
Jedes (normale) Terminal- Tty hat eine einzelne Vordergrund-Prozessgruppe, die man als das Tty "besetzend" betrachten könnte.
Jede Prozessgruppe kann mehrere Prozesse enthalten.
Jeder Prozess kann sein eigenes aktuelles Arbeitsverzeichnis (cwd) haben (einige Umgebungen geben jedem Thread ein eigenes cwd oder cwd-Äquivalent, aber das werden wir ignorieren).
Die vorstehenden Tatsachen legen eine Art Spur fest, die von tty bis cwd: tty -> Vordergrundprozessgruppe -> Prozesse der Vordergrundprozessgruppe -> cwds.
Der erste Teil (von tty bis zum Vordergrundprozess) des Problems kann mit der Ausgabe von ps gelöst werden :
ps -o tty,pid,tpgid,pgid,state,command | awk 'BEGIN{t=ARGV[1];ARGC=1} $1==t && $3==$4 {print $2}' ttyp6
(wobei "ttyp6" der Name der gewünschten Menge ist)
Die Zuordnung von process (PID) zu cwd kann mit lsof erfolgen :
lsof -F 0n -a -p 2515,2516 -d cwd
(wobei "2515,2516" eine durch Kommas getrennte Liste der interessierenden Prozesse ist)
Unter Tiger sehe ich jedoch keine direkte Möglichkeit, den tty-Gerätenamen eines bestimmten Terminalfensters abzurufen. Es gibt eine schrecklich hässliche Art, den Namen in Tiger zu finden. Vielleicht kann Leopard oder Snow Leopard es besser machen.
Ich habe alles in einem AppleScript wie folgt zusammengefasst:
on run
(* Find the tty. *)
-- This is ugly. But is seems to work on Tiger. Maybe newer releases can do better.
tell application "Terminal"
set w to window 1
tell w
set origName to name
set title displays device name to not title displays device name
set newName to name
set title displays device name to not title displays device name
end tell
end tell
set tty to extractTTY(origName, newName)
if tty is "" then
display dialog "Could not find the tty for of the current Terminal window." buttons "Cancel" cancel button "Cancel" default button "Cancel"
end if
(* Find the PIDs of the processes in the foreground process group on that tty. *)
set pids to paragraphs of (do shell script "
ps -o pid,tty,tpgid,pgid,state,command |
awk '
BEGIN {t=ARGV[1];ARGC=1}
$2==t && $3==$4 {print $1}
' " & quoted form of tty)
if pids is {} or pids is {""} then
display dialog "Could not find the processes for " & tty & "." buttons "Cancel" cancel button "Cancel" default button "Cancel"
end if
(* Find the unique cwds of those processes. *)
set text item delimiters to {","}
set lsof to do shell script "lsof -F 0n -a -d cwd -p " & quoted form of (pids as Unicode text) without altering line endings
set text item delimiters to {(ASCII character 0) & (ASCII character 10)}
set cwds to {}
repeat with lsofItem in text items of lsof
if lsofItem starts with "n" then
set cwd to text 2 through end of lsofItem
if cwds does not contain cwd then ¬
set end of cwds to cwd
end if
end repeat
if cwds is {} then
display dialog "No cwds found!?" buttons "Cancel" cancel button "Cancel" default button "Cancel"
end if
if length of cwds is greater than 1 then
set cwds to choose from list cwds with title "Multiple Distinct CWDs" with prompt "Choose the directory to use:" without multiple selections allowed and empty selection allowed
if cwds is false then error number -128 -- cancel
end if
(* Open a new Terminal. *)
tell application "Terminal" to do script "cd " & quoted form of item 1 of cwds
end run
to extractTTY(a, b)
set str to textLeftAfterRemovingMatchingHeadAndTail(a, b)
set offs to offset of "tty" in str
if offs > 0 then
return text offs through (offs + 4) of str
end if
return ""
end extractTTY
to textLeftAfterRemovingMatchingHeadAndTail(big, little)
set text item delimiters to space
if class of big is not list then set big to text items of big
if class of little is not list then set little to text items of little
set {maxLen, minLen} to {length of big, length of little}
if maxLen < minLen then ¬
set {big, little, maxLen, minLen} to {little, big, minLen, maxLen}
set start to missing value
repeat with i from 1 to minLen
if item i of big is not equal to item i of little then
set start to i
exit repeat
end if
end repeat
if start is missing value then
if maxLen is equal to minLen then
return ""
else
return items (minLen + 1) through end of big as Unicode text
end if
end if
set finish to missing value
repeat with i from -1 to -minLen by -1
if item i of big is not equal to item i of little then
set finish to i
exit repeat
end if
end repeat
if finish is missing value then set finish to -(minLen + 1)
return items start through finish of big as Unicode text
end textLeftAfterRemovingMatchingHeadAndTail
Speichern Sie es mit dem Skript-Editor ( AppleScript-Editor in Snow Leopard) und verwenden Sie einen Starter (z. B. FastScripts ), um es einer Taste zuzuweisen (oder führen Sie es einfach über das AppleScript-Menü aus (aktiviert über / Applications / AppleScript / AppleScript Utility.app )).