Get window title using python?

I am trying to write a python program that checks every X seconds if there is a “window title” for “last.fm” ( http://www.last.fm/download ) if it did (or is this the first time I run the program ), he should use the string captured from the window title to search for lyrics and display them to the user.

I am currently using KDE4 as my desktop environment and I just need to “point in the right direction” on how I can capture the line that belongs to the window title for the last.fm client.

Thanks!

+4
source share
5 answers

You can use the wmctrl utility through the subprocess module. You can enter wmctrl -l into the terminal and see the output you can get from it.

+4
source

I think that with the help of an automation system you can achieve this as a subset. for example, try dogtail ( https://fedorahosted.org/dogtail/ ), it can focus on windows by name, click on buttons by name, so in src code you can see how to get the name.

+2
source

Check out the X11 utilities, specifically xlsclients and xprop .

As an example, these are the shell commands that I used to get information about my firefox window:

 id_=$(xlsclients -al|grep "Command: firefox-bin" -A1 -B4|head -n1|cut -d ' ' -f 2|tr -d ':') xprop -id "$_id" 

Output:

 SM_CLIENT_ID(STRING) = "1181f048b9000125508490000000037360008" WM_CLASS(STRING) = "firefox-bin", "Firefox-bin" WM_COMMAND(STRING) = { "firefox-bin" } WM_CLIENT_LEADER(WINDOW): window id # 0x0 _NET_WM_PID(CARDINAL) = 4265 WM_LOCALE_NAME(STRING) = "no_NO" WM_CLIENT_MACHINE(STRING) = "gnom.ifi.uio.no" WM_NORMAL_HINTS(WM_SIZE_HINTS): program specified size: 10 by 10 WM_PROTOCOLS(ATOM): protocols WM_DELETE_WINDOW, WM_TAKE_FOCUS, _NET_WM_PING WM_ICON_NAME(STRING) = "firefox-bin" _NET_WM_ICON_NAME(UTF8_STRING) = 0x66, 0x69, 0x72, 0x65, 0x66, 0x6f, 0x78, 0x2d, 0x62, 0x69, 0x6e WM_NAME(STRING) = "Firefox" _NET_WM_NAME(UTF8_STRING) = 0x46, 0x69, 0x72, 0x65, 0x66, 0x6f, 0x78 
+1
source

Try using dcop and pilot kwin. You can probably list all the window names.

The following is an example of using dcop: http://docs.kde.org/stable/en/kdegraphics/ksnapshot/dcop.html

0
source

another answer is perhaps to check if the application publishes a song change to DBus. if so, you can just listen to the event and act on it.

0
source

All Articles