How can you get information about other apps running or in focus?

痴心易碎 提交于 2020-07-18 07:26:06

问题


My motivation: I'm writing an app to help with some quantified self / time tracking type things. I'd like to use electron to record information about which app I am currently using.

Is there a way to get information about other apps in Electron? Can you at least pull information about another app that currently has focus? For instance, if the user is browsing a webpage in Chrome, it would be great to know that A) they're using chrome and B) the title of the webpage they're viewing.

During my research I found this question: Which app has the focus when a global shortcut is triggered

It looks like the author there is using the nodObjc library to get this information on OSX. In addition to any approaches others are using to solve this problem, I'm particularly curious if electron itself has any way of exposing this information without resorting to outside libraries.


回答1:


No, Electron doesn't provide an API to obtain information about other apps. You'll need to access the native platform APIs directly to obtain that information. For example Tockler seems to do so via shell scripts, though personally I prefer accessing native APIs directly via native Node addons/modules or node-ffi.




回答2:


In a limited way, yes, you can get some of this information using the electron's desktopCapturer.getSources() method.

This will not get every program running on the machine. This will only get whatever chromium deems to be a video capturable source. This generally equates to anything that is an active program that has a GUI window (e.g., on the task bar on windows).

desktopCapturer.getSources({
  types: ['window', 'screen']
}, (error, sources) => {
  if (error) throw error
  for (let i = 0; i < sources.length; ++i) {
    log(sources[i]);
  }
});


来源:https://stackoverflow.com/questions/39930279/how-can-you-get-information-about-other-apps-running-or-in-focus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!