Get the current working directory of the process with pid software on OS X

Is there a way to get the current working directory of a process using its PID programmatically in OS X?

Cocoa, Carbon or AppleScript are acceptable.

It is not possible to send "pwd" to the current terminal window / tab (do not want to affect the workspace).

The linux "pwdx" command is also unacceptable (just in case, when you read the "Cocoa" part)

+5
source share
2 answers

In 10.5 and later:

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

(Prefix with sudoif the process is root.)

+8
source

AppleScript - . UNID pid thePID, . do shell script , . , , 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

script , AppleEvent (, POSIX).

+1

All Articles