Terminal AppleScript:如何获取最顶端终端的当前目录
我想获取最顶端终端选项卡/窗口的当前目录(通过AppleScript或其他方式,这并不重要)。我该怎么做呢?好的,我有一个解决方案 获取\u foregroundterminal\u proclist.scpt:Terminal AppleScript:如何获取最顶端终端的当前目录,terminal,applescript,working-directory,Terminal,Applescript,Working Directory,我想获取最顶端终端选项卡/窗口的当前目录(通过AppleScript或其他方式,这并不重要)。我该怎么做呢?好的,我有一个解决方案 获取\u foregroundterminal\u proclist.scpt: tell application "Terminal" do shell script "fuser " & (tty of front tab of front window) end tell 获取\u foregroundterminal\u curdir.sh
tell application "Terminal"
do shell script "fuser " & (tty of front tab of front window)
end tell
获取\u foregroundterminal\u 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
好的,我有一个解决办法 获取\u foregroundterminal\u proclist.scpt:
tell application "Terminal"
do shell script "fuser " & (tty of front tab of front window)
end tell
获取\u foregroundterminal\u 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
另一个解决方案
获取\u foregroundterminal\u curdir\u 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。这比使用热熔器(毫秒与秒)快得多。另一种解决方案
获取\u foregroundterminal\u curdir\u 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(毫秒vs.秒)快得多。我在发布关于如何在Applescript中查找当前目录的问题时,被指向了这个问题,所以我发布这个答案是为了让未来的参考读者知道例外答案有缺陷
如果当前目录路径中有空格字符,则它只返回路径中(最后一个)空格字符之后的部分强>
改为使用这个简单的脚本,它处理每个路径:告诉应用程序“Terminal”将currentDirectory设置为(do shell script“pwd”)
我在发布一个关于如何在Applescript中查找当前目录的问题时,被指向了这个问题,因此我发布这个答案是为了让未来的参考读者知道例外答案中存在缺陷
如果当前目录路径中有空格字符,则它只返回路径中(最后一个)空格字符之后的部分强>
使用这个简单的脚本,它处理每个路径:告诉应用程序“终端”将currentDirectory设置为(do shell script“pwd”)
并不是说路径有空格。@ppasler-他说他想要“当前路径”。除非程序员也完全控制终端,否则用户可以很容易地将“cd”放入路径中有空格的目录中。这并不是说路径中有空格。@ppasler-他说他想要“当前路径”。除非程序员也完全控制终端,否则用户可以很容易地将“cd”放入路径中带有空格的目录中。