1

我想获取最顶层终端选项卡/窗口的当前目录(通过 AppleScript 或其他方式,这并不重要)。我怎样才能做到这一点?

4

3 回答 3

3

另一种解决方案。

get_foregroundterminal_curdir_fast.scpt:

tell application "Terminal"
    do shell script "lsof -a -p `lsof -a -c bash -u $USER -d 0 -n | tail -n +2 | awk '{if($NF==\"" & (tty of front tab of front window) & "\"){print $2}}'` -d cwd -n | tail -n +2 | awk '{print $NF}'"
end tell

我使用lsof自己来获取相应终端窗口的 bash shell 的 PID。这比使用fuser(毫秒与秒)快得多。

于 2013-04-18T03:29:53.247 回答
3

在发布有关如何在 Applescript 中查找当前目录的问题时,我被指出了该问题,因此我发布此答案是为了让未来的推荐读者知道例外答案存在缺陷。

如果当前目录路径中有空格字符,那么它只返回(最后一个)空格字符之后的路径部分!

改用这个简单的脚本,它处理每个路径:tell application "Terminal" to set currentDirectory to (do shell script "pwd")

于 2017-02-04T20:08:23.290 回答
0

好的,我有一个解决方案。

get_foregroundterminal_proclist.scpt:

tell application "Terminal"
    do shell script "fuser " & (tty of front tab of front window)
end tell

get_foregroundterminal_curdir.sh:

#!/bin/bash

function pwdx {
    lsof -a -p $1 -d cwd -n | tail -1 | awk '{print $NF}'
}

for pid in $(osascript "$(dirname "$0")/get_foregroundterminal_proclist.scpt"); do
    pwdx $pid
    break # break on first
done
于 2011-03-13T15:51:35.620 回答