• Welcome to Valhalla Legends Archive.
 

[Perl] Getting Window Title

Started by hismajesty, August 31, 2004, 07:30 AM

Previous topic - Next topic

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.

Banana fanna fo fanna

Perl, IMO, doesn't have any win32 support without...the win32 module.

hismajesty

Well, the Win32 module doesn't like me very much, obviously! :(

MyndFyre

Quote from: hismajesty[yL] on August 31, 2004, 10:46 AM
Well, the Win32 module doesn't like me very much, obviously! :(

Can you blame it?
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mr. Neo

#4
I found a nice little perl script that does similar to what you'd like.  


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

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


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.

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

Theres plenty more but thats just a sample of what all it spits out without a grep.  Heres a grep for just notepad.

C:\Documents and Settings\andykr\Desktop>perl spy.pl | grep Notepad
+0x00270376, 'spy.pl - Notepad', Notepad

And heres what you get with multiple instances of a certain program.

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


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.

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. :)

Mr. Neo

#6

#!/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);
   }
}


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:

C:\Documents and Settings\andykr\Desktop>perl winamp.pl
Current song is:  4. Black Sabbath - Megalomania
Current song is:  2. AFI - Silver and Cold


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.

hismajesty

#7
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?

Mr. Neo

The first part could go into its own sub sure.  

hismajesty

Then not sure what the problem is, bug the code worked by itself so thanks it's really helpful.

idoL

There should be a Perl forum here.

Kp

Quote from: Snake on September 01, 2004, 11:40 PMThere should be a Perl forum here.

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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

idoL

Quote from: Kp on September 02, 2004, 08:59 AM
Quote from: Snake on September 01, 2004, 11:40 PMThere should be a Perl forum here.

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.

It was wishfull thinking, Kp.

Adron

Quote from: Kp on September 02, 2004, 08:59 AM
Quote from: Snake on September 01, 2004, 11:40 PMThere should be a Perl forum here.

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.

Yes... We rarely remove forums once created. Which esoteric ones were you referring to?

Kp

Quote from: Adron on September 05, 2004, 08:02 AMYes... We rarely remove forums once created. Which esoteric ones were you referring to?

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
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!