AppleScript:如何获取最顶层终端的当前目录 [英] AppleScript: how to get the current directory of the topmost Terminal

查看:34
本文介绍了AppleScript:如何获取最顶层终端的当前目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取最顶层终端选项卡/窗口的当前目录(通过 AppleScript 或其他方式,这并不重要).我该怎么做?

I want to get the current directory of the topmost Terminal tab/window (via AppleScript or something else, it doesn't really matter). How can I do that?

推荐答案

另一种解决方案.

get_foregroundterminal_curdir_fast.scpt:

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(毫秒与秒)快得多.

I use lsof itself to get PID of the bash shell of the corresponding Terminal window. This is MUCH faster than using fuser (milliseconds vs. seconds).

这篇关于AppleScript:如何获取最顶层终端的当前目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆