Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

electron

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.

like image 959
Fred Antell Avatar asked Oct 08 '16 08:10

Fred Antell


2 Answers

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]);
  }
});
like image 132
Andy Baird Avatar answered Oct 28 '22 13:10

Andy Baird


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-napi.

like image 33
Vadim Macagon Avatar answered Oct 28 '22 11:10

Vadim Macagon