Seseorang harus sangat berhati-hati ketika melewati string melalui lingkungan yang berbeda.
Saya menjalankan 10.4, jadi skrip 'tfork' saya selalu membuka jendela baru. Seharusnya mudah untuk mengadaptasinya menggunakan tab:
#!/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
Contoh: tfork git log -p ..FETCH_HEAD
Amandemen: cwd dari proses yang sudah berjalan "menempati" tab Terminal
Gagasan "direktori saat ini dari program yang menempati tab saat ini" tidak sejelas yang diharapkan.
Setiap tab Terminal memiliki satu perangkat tty yang digunakan oleh proses yang dijalankannya (awalnya, sebuah shell; setelah itu, apa pun shell yang dimulai).
Setiap terminal tty (normal) memiliki grup proses foreground tunggal yang dapat dianggap sebagai "menempati" tty.
Setiap grup proses dapat memiliki beberapa proses di dalamnya.
Setiap proses dapat memiliki direktori kerja saat ini (cwd) (beberapa lingkungan memberikan masing-masing utas cwd atau setara dengan cwd, tetapi kami akan mengabaikannya).
Fakta-fakta sebelumnya membentuk semacam jejak yang dari tty ke cwd: tty -> foreground process group -> proses dari foreground process group -> cwds.
Bagian pertama (dari proses tty ke foreground) dari masalah dapat diselesaikan dengan output dari ps :
ps -o tty,pid,tpgid,pgid,state,command | awk 'BEGIN{t=ARGV[1];ARGC=1} $1==t && $3==$4 {print $2}' ttyp6
(di mana "ttyp6" adalah nama tty of interest)
Pemetaan dari proses (PID) ke cwd dapat dilakukan dengan lsof :
lsof -F 0n -a -p 2515,2516 -d cwd
(di mana "2515.2516" adalah daftar proses bunga yang dipisahkan koma)
Tetapi di bawah Tiger, saya tidak melihat cara langsung untuk mendapatkan nama perangkat tty dari jendela Terminal tertentu . Ada cara buruk yang mengerikan untuk mendapatkan nama tty di Tiger. Mungkin Leopard atau Snow Leopard bisa melakukan yang lebih baik.
Saya menggabungkan semuanya dalam AppleScript seperti ini:
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
Simpan dengan Editor Skrip ( Editor AppleScript di Snow Leopard) dan gunakan peluncur (mis. FastScripts ) untuk menetapkannya ke tombol (atau jalankan saja dari menu AppleScript (diaktifkan melalui / Aplikasi / AppleScript / AppleScript Utility.app )).