6

有没有办法在 OS X 上以编程方式使用它的 PID 获取进程的当前工作目录?

Cocoa、Carbon 或 AppleScript 都是可以接受的。

将“pwd”发送到当前终端窗口/选项卡是不可接受的(不想影响工作区)。

linux 命令“pwdx”也是不可接受的(以防您阅读“Cocoa”部分)

4

2 回答 2

9

On 10.5 and later:

lsof -a -p $PID -d cwd -Fn

(Prefix with sudo if the process is owned by root.)

于 2009-11-13T03:51:56.203 回答
1

以下 AppleScript 是您的问题的部分解决方案。给定变量中的 UNIX pid,thePID它首先获取进程的名称。然后它将do shell script命令发送到应用程序进程,这将导致产生一个子 shell 进程。子进程继承当前目录,然后可以通过运行pwd命令来确定。

tell application "System Events"
    set theName to name of first process whose unix id is thePID
end tell

tell application theName
    do shell script "/bin/pwd"
end tell

该脚本不适用于不链接到 AppleEvent 框架的进程(例如,纯 POSIX 进程)。

于 2009-10-06T20:36:52.257 回答