ICYROCK.COM

Home

GNOME experiments - list active windows and applications using Javascript

2016-Dec-31 19:51
gnome-experimentsjavascript

GNOME can be accessed from Javascript using Gjs, a set of Javascript bindings for GNOME. The usual API docs can be used to get the information about what can be called and then transform that into the appropriate Javascript calls.

For example, here's how we can list the windows using GDK X11 and Wnck.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const Gdk = imports.gi.Gdk
const GdkX11 = imports.gi.GdkX11
const Gtk = imports.gi.Gtk
const Wnck = imports.gi.Wnck
 
Gtk.init(null)
const wnckScreen = Wnck.Screen.get_default()
wnckScreen.force_update()
 
const display = Gdk.Display.get_default()
const screen = display.get_default_screen()
 
const paddings = ['']
 
const pad = function(depth) {
  for(let i = paddings.length; i <= depth; i++) {
    paddings.push('- ' + paddings[i - 1])
  }
 
  return paddings[depth]
}
 
const winInfo = function(window) {
  const children = window.get_children()
  const fullscreenMode = window.get_fullscreen_mode()
  const geometry = window.get_geometry()
  const xid = window.get_xid()
 
  const wnckWin = Wnck.Window.get(xid)
  const name = wnckWin != null ? wnckWin.get_name() : 'n/a'
 
  return [xid.toString(16), name, fullscreenMode, geometry, children.length]
}
 
const childrenTree = function(depth, window) {
  print(pad(depth) + winInfo(window))
 
  const x11w = GdkX11.X11Window.foreign_new_for_display(display, window.get_xid())
 
  for(let child of window.get_children()) {
    childrenTree(depth + 1, child)
  }
}
 
for(let window of screen.get_window_stack()) {
  childrenTree(0, window)
}

Sample result:

1
2
3
4
1600002,New Tab - Google Chrome,0,75,167,1326,928,0
4200088,Untitled 1 - LibreOffice Writer,0,10,45,1347,1140,0
3e00007,GNOME Tweak Tool,0,15,895,950,700,0
3000006,[No Name] - VIM,0,10,45,1445,817,0