Valhalla Legends Forums Archive | General Programming | [Perl] Getting Window Title

AuthorMessageTime
hismajesty
I've never done Perl before, and don't really have a real reason to learn it, but I'm trying to make a simple script to get the WindowTitle. This needs to work, at least, on Windows. For example, if I wanted to get the WindowTitle for notepad, and I passed the handle of notepead, it'd return "<filename> - Notepad" So, would somebody mind pasting an example - or providing a link that states how to do this? I saw that Win32::GUItest had a GetWindowText() function, but from what I can tell that would make my users have to install the Win32 package whichi could lead to tech support problems etc. I could be wrong though as yesterday was the first time I've ever really looked at Perl. I tried using Win32::GUITest, but it's saying the Win32 module isn't installed, but I installed it using the package manager with ActivePerl. *shrug*

Thanks in advance.
August 31, 2004, 12:30 PM
St0rm.iD
Perl, IMO, doesn't have any win32 support without...the win32 module.
August 31, 2004, 2:54 PM
hismajesty
Well, the Win32 module doesn't like me very much, obviously! :(
August 31, 2004, 3:46 PM
Myndfyr
[quote author=hismajesty[yL] link=board=5;threadid=8488;start=0#msg78362 date=1093967183]
Well, the Win32 module doesn't like me very much, obviously! :(
[/quote]

Can you blame it?
August 31, 2004, 10:00 PM
Mr. Neo
I found a nice little perl script that does similar to what you'd like.

[code]
use Win32::GuiTest qw/FindWindowLike GetWindowText GetClassName
GetChildDepth GetDesktopWindow/;

for (FindWindowLike()) {
$s = sprintf("0x%08X", $_ );
$s .= ", '" . GetWindowText($_) . "', " . GetClassName($_);
print "+" x GetChildDepth(GetDesktopWindow(), $_), $s, "\n";
}
[/code]

This was found at http://triumvir.org/prog/perl/guitest/guitest-spy.html. You will also need the Win32::GuiTest module which you can find in an ActivePerl repository (such as this one). Just download and install the module you need and your good to go. If you'd like to not have to have your users install the modules themselves, move them into a directory you'd ship with the script. Then just include a uselib line at the top of your script and require the modules.

To work it, just call it like you would any perl script. It will give you a nice list of every open window and then some information about each. If you'd like to find just a specific program (example notepad) then you'd just call the script like so:
perl FILENAME.pl | grep *

Just change the * to the name of any program. It'll return a line or two and you'll just have to format the string a bit to get what you'd like. That should be easy enough to do. Example output is found at the end of this post.

I think this is what your asking for, if not just let me know and I'll see what else I can dig up. Best of luck,

EDIT: Heres some sample outputs and such to see if its what you'd like.
[code]
C:\Documents and Settings\andykr\Desktop>perl spy.pl
++0x000200E8, 'TF_FloatingLangBar_WndTitle', CiceroUIWndFrame
+++0x000200E4, 'CiceroUIWndFrame', CiceroUIWndFrame
++0x00040046, '', tooltips_class32
+0x000F05B0, '', tooltips_class32
+++0x000505DE, '', tooltips_class32
+0x00080072, '', tooltips_class32
+0x00100056, '', tooltips_class32
++0x00070092, 'Start Menu', DV2ControlHost
+++0x00090074, '', Desktop User Pane
++++0x000C00A6, 'andykr', Static
+++0x000A0090, '', DesktopSFTBarHost
++++0x000900B6, '', SysListView32
+++++0x000B013E, '', SysHeader32
+++0x000C00A8, '', Desktop More Programs Pane
++++0x00090076, 'All &Programs', Button
[/code]
Theres plenty more but thats just a sample of what all it spits out without a grep. Heres a grep for just notepad.
[code]
C:\Documents and Settings\andykr\Desktop>perl spy.pl | grep Notepad
+0x00270376, 'spy.pl - Notepad', Notepad
[/code]
And heres what you get with multiple instances of a certain program.
[code]
C:\Documents and Settings\andykr\Desktop>perl spy.pl | grep Internet
++0x00030328, 'Internet Explorer', SysListView32
++0x008703FE, 'Internet Explorer', SysListView32
++0x00040594, 'Internet Explorer', SysListView32
++0x000B016E, 'Internet Explorer', SysListView32
+0x000601D8, 'Modify message - Microsoft Internet Explorer provided by Compaq', IEFrame
+++0x001B017A, '', Internet Explorer_Server
++++0x00110042, '', Internet Explorer_TridentCmboBx
+0x0003035E, '', Internet Explorer_Hidden
+0x000C02EE, '888paintball.com | Welcome to the best paintball store ever - Microsoft Internet Explorer provided by Compaq', IEFrame
+++0x0005035C, '', Internet Explorer_Server
++++0x00050396, '', Internet Explorer_TridentCmboBx
+0x00090300, '', Internet Explorer_Hidden
+0x000E0296, 'TSW: Yet Another Web Site - Microsoft Internet Explorer provided by Compaq', IEFrame
+++0x000F0306, '', Internet Explorer_Server
+0x00190270, '', Internet Explorer_Hidden
[/code]

As you can see the editting of the string to get the specific elements that youd like would be as easy as a simple split combined with a few regular expressions.
August 31, 2004, 11:54 PM
hismajesty
I did install the win32::guitest module, and it said I didn't or something. Anyway, I may be able to work with that - but I need to be able to get a specific windows title without having to pass command line arguments to do it. This is for a gaim plugin that I want - it doesn't exist and I think it'd be useful for me -- getting the current mp3 of winamp. :)
September 1, 2004, 12:00 AM
Mr. Neo
[code]
#!/usr/bin/perl
# Winamp Song Grabber
use Win32::GuiTest qw/FindWindowLike GetWindowText GetClassName
GetChildDepth GetDesktopWindow/;
@output = "";
my @data = "";

get_windows();
foreach my $element (@output) {
   if ($element =~ m/- Winamp/) {
      @data = split(",",$element);
      $data[1] =~ s/'//g;;
      $data[1] =~ s/- Winamp//;
      print "Current song is: $data[1]\n";
      #$data[1] now is equal to 4. Guns N' Roses - Civil War
   } else { # Not a match for the window title
   }
}

sub get_windows {
   # Gets the windows
   my $s = "";
   my $temp = "";
   
   for (FindWindowLike()) {
      $s = sprintf("0x%08X", $_ );
      $s .= ", '" . GetWindowText($_) . "', " . GetClassName($_);
      $temp = "+". x. GetChildDepth(GetDesktopWindow(), $_). $s;
      unshift(@output,$temp);
   }
}
[/code]

This is just a quick script that I threw together here and it gets the currently playing song in my Winamp (2.9.1) very easily. It returns a nicely formatted string like:
[code]
C:\Documents and Settings\andykr\Desktop>perl winamp.pl
Current song is: 4. Black Sabbath - Megalomania
Current song is: 2. AFI - Silver and Cold
[/code]

You can easily change what the string looks like when returned so it's tuned to your liking.

On a side note, how come when I grep for a Winamp window, I'm always getting back the currently playing song in iTunes? AFI was playing in my Winamp window and Black Sabbath was on iTunes but both were returned with a search parameter of Winamp. Anyone have an idea of why this happens?

Hope this helps.
September 1, 2004, 12:28 AM
hismajesty
Cool thanks!

Edit: Your code works, but when I put it into the plugin code gaim doesn't recognize it. If I remove the code and leave the sub that is called when the command is executed - it recognizes the plugin. Is that first part of the code allowed to go in a seperate sub?
September 1, 2004, 4:12 AM
Mr. Neo
The first part could go into its own sub sure.
September 1, 2004, 11:33 AM
hismajesty
Then not sure what the problem is, bug the code worked by itself so thanks it's really helpful.
September 1, 2004, 12:15 PM
idoL
There should be a Perl forum here.
September 2, 2004, 4:40 AM
Kp
[quote author=Snake link=board=5;threadid=8488;start=0#msg78689 date=1094100012]There should be a Perl forum here.[/quote]

No, there should not be. We have quite enough forums as is, and some of the more esoteric ones already lie partially unused. Forum creation is ultimately the decision of the administrators, with some input from the members, and rarely input from nonmembers.
September 2, 2004, 1:59 PM
idoL
[quote author=Kp link=board=5;threadid=8488;start=0#msg78750 date=1094133574]
[quote author=Snake link=board=5;threadid=8488;start=0#msg78689 date=1094100012]There should be a Perl forum here.[/quote]

No, there should not be. We have quite enough forums as is, and some of the more esoteric ones already lie partially unused. Forum creation is ultimately the decision of the administrators, with some input from the members, and rarely input from nonmembers.
[/quote]

It was wishfull thinking, Kp.
September 4, 2004, 6:35 AM
Adron
[quote author=Kp link=board=5;threadid=8488;start=0#msg78750 date=1094133574]
[quote author=Snake link=board=5;threadid=8488;start=0#msg78689 date=1094100012]There should be a Perl forum here.[/quote]

No, there should not be. We have quite enough forums as is, and some of the more esoteric ones already lie partially unused. Forum creation is ultimately the decision of the administrators, with some input from the members, and rarely input from nonmembers.
[/quote]

Yes... We rarely remove forums once created. Which esoteric ones were you referring to?
September 5, 2004, 1:02 PM
Kp
[quote author=Adron link=board=5;threadid=8488;start=0#msg79145 date=1094389367]Yes... We rarely remove forums once created. Which esoteric ones were you referring to?[/quote]

Assembly Language ((any cpu) and Advanced Programming. They see activity far less often than any of the other forums I read. It's understandable since AP forbids rehashes (which I appreciate) and not that many people seem inclined to understand an assembly language (which is unfortunate, but good for those who do). I'm not complaining about them - just noting that they don't see much use, despite having an apparently wider audience than Perl does. Afaik, this is the first perl thread we've had. :P
September 5, 2004, 3:40 PM
Adron
Ah, yes. Those forums make me rush to read the few times there is a new thread posted.
September 5, 2004, 4:34 PM

Search