Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to write a script that executes differently depending on the currently active program, and I am wondering if there is any way to find this programatically.

For example, I come from the Mac world, and in Apple's Applescript you can do the following:

set appPath to the path to the frontmost application
set appName to the name of file appPath

and that will give you the name of the currently running application.

Is there anything similar that I can do in ubuntu-land?


share|improve this question
up vote 2 down vote accepted

xprop can tell you the ID of the window that is active

xprop -root|grep '^_NET_ACTI'

And you can use the output of wmctrl to find out the name of all windows, matched up to their IDs

wmctrl -l

wmctrl isn't in the default install, so you'll need to sudo apt-get install wmctrl of course.

Note that there's no left-padding in the output of xprop, so we need to pad it before grepping for it within the output of wmctrl. All together, something like this should do it:

$ WINID=$(xprop -root|grep '^_NET_ACTI' |sed -e 's/.*# //' -e 's/0x/0x0/')
$ wmctrl -l |grep $WINID
0x07c00006  0 my-hostname The Title of The Window
share|improve this answer
Thank you, this worked perfectly! (Haha, that's just about as readable as the applescript, I think.) :p – NHDaly Jun 26 '13 at 14:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.