Hay que tener mucho cuidado al pasar cadenas a través de diferentes entornos.
Ejecuto 10.4, por lo que mi script 'tfork' siempre abre una nueva ventana. Debería ser fácil adaptarlo para usar una pestaña:
#!/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
Ejemplo: tfork git log -p ..FETCH_HEAD
Enmienda: cwd de un proceso que ya se está ejecutando "ocupando" una pestaña Terminal
La idea de "el directorio actual del programa que ocupa la pestaña actual" no es tan obvia como cabría esperar.
Cada pestaña de Terminal tiene un único dispositivo tty que es utilizado por los procesos que ejecuta (inicialmente, un shell; luego, sea lo que sea que se inicie el shell).
Cada terminal (normal) tty tiene un único grupo de procesos en primer plano que se podría considerar como "ocupando" el tty.
Cada grupo de procesos puede tener múltiples procesos en él.
Cada proceso puede tener su propio directorio de trabajo actual (cwd) (algunos entornos le dan a cada hilo su propio cwd o equivalente a cwd, pero lo ignoraremos).
Los hechos anteriores establecen un tipo de rastro que de tty a cwd: tty -> grupo de procesos en primer plano -> procesos del grupo de procesos en primer plano -> cwds.
La primera parte (desde tty hasta procesos en primer plano) del problema se puede resolver con la salida de ps :
ps -o tty,pid,tpgid,pgid,state,command | awk 'BEGIN{t=ARGV[1];ARGC=1} $1==t && $3==$4 {print $2}' ttyp6
(donde "ttyp6" es el nombre del tty de interés)
La asignación del proceso (PID) a cwd se puede hacer con lsof :
lsof -F 0n -a -p 2515,2516 -d cwd
(donde "2515,2516" es una lista separada por comas de los procesos de interés)
Pero bajo Tiger, no veo una forma directa de obtener el nombre del dispositivo tty de una ventana de Terminal en particular . Hay una forma horriblemente fea de obtener el nombre tty en Tiger. Quizás Leopard o Snow Leopard pueden hacerlo mejor.
Lo puse todo junto en un AppleScript como este:
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
Guárdelo con Script Editor ( AppleScript Editor en Snow Leopard) y use un iniciador (por ejemplo, FastScripts ) para asignarlo a una tecla (o simplemente ejecútelo desde el menú AppleScript (habilitado a través de / Applications / AppleScript / AppleScript Utility.app ).