Artificial intelligent assistant

Get PID of the application running in the active terminal emulator My end goal is to be able to open a new terminal window (`urxvt`) directly in the current working directory of the program running in the window currently active. I'm currently using the shell (Bash), but I don't have anything against alternatives. So far, I've got the ID of the current active window using `xdotool`: wid=$(xdotool getactivewindow) and the PID of its process using `xprop`: pid=$(xprop -id $wid _NET_WM_PID | awk '{print $NF}') but this is not the PID I'm looking for. **I want the PID of the process running in the terminal displayed in that window.** For now, I mostly want the case of a `bash` shell running in that window, but I don't see why it would depend on that. I can already get CWD from a PID using `cwd="$(readlink /proc/$pid/cwd)"`.

Got it! Thanks to Stephane Chazelas for the help. The trick was to look for the child processes... D'oh!

My script is now:


#!/usr/bin/env bash
ppid=$(xdotool getactivewindow getwindowpid) # PID of process in the window
pid=$(pgrep -P $ppid | tail -n1) # PID of the last child
cwd="$(readlink /proc/${pid:-$ppid}/cwd)" # current CWD of pid, or ppid if no pid
cd "$cwd"
"$@"


You can use it by simply prefixing any command with the name of the script, eg. `incwd urxvt`.

The only caveat is that some programs, like `evince`, reset their `cwd`. I doubt there's anything I can do in these cases.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 723e2dd3260e62b03eb4d345f1386646