【问题标题】:How to get Windowid from PID from a electron Application如何从电子应用程序的 PID 中获取 Windowid
【发布时间】:2020-02-21 13:56:03
【问题描述】:
我想从电子进程(例如 riot-desktop)的 PID 中获取 WindowID
我试图用这样的 xdotools 来获得它:
$ xdotool search --pid $(pgrep riot)
nothing is printed
$ pgrep riot
30461
$ xdotool search --pid 30461
nothing is printed again
【问题讨论】:
标签:
linux
window
x11
pid
xdotool
【解决方案1】:
您必须搜索电子子进程的pid。
您可以通过以下方式获取子进程的 pid:
PID=$(ps h -C electron | grep riot | cut -f1 -d"?")
现在您可以搜索 pid
xdotool search --pid $PID
您可以将两个命令组合成一个命令
xdotool search --pid $(ps h -C electron | grep riot | grep witzerstorfer | cut -f1 -d"?")
如果您的 ps 返回多个 pid,则必须添加更多 grep 命令,例如,如果您使用配置文件,则命令可能如下所示:
PID=$(ps h -C electron | grep riot | grep $PROFILENAME | cut -f1 -d"?")
【解决方案2】:
如果你想找到你的电子应用程序的windowID,那么你可以使用
win.getMediaSourceId()
返回String - 以 DesktopCapturerSource 的 id 格式的窗口 id。例如“window:1324:0”。
更准确地说,格式是window:id:other_id,其中id 在Windows 上是HWND,在macOS 上是CGWindowID (uint64_t),在Linux 上是Window (unsigned long)。 other_id 用于识别网页内容(标签),因此在同一个顶级窗口中。
例子:
// In the main process.
const { BrowserWindow } = require('electron')
const win = new BrowserWindow({ width: 800, height: 600 })
// Load a remote URL
win.loadURL('https://github.com')
// Or load a local HTML file
win.loadURL(`file://${__dirname}/app/index.html`)
// Print window id
console.log('window id is', win.getMediaSourceId())