Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

Thanks!

share|improve this question

1 Answer 1

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

 
discard

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.