views:

945

answers:

6

Using Perl, Python, or Ruby, can I write a program, probably calling Win32 API, to "click" on the screen at scheduled time, like every 1 hour?

Details:

This is for experimentation -- and can the clicking be effective on Flash content as well as any element on screen? It can be nice if the program can record where on screen the click needs to happen, or at least draw a red dot on the screen to show where it is clicking on.

Can the click be targeted towards a window or is it only a general pixel on the screen? What if some virus scanning program pops up covering up the place where the click should happen? (although if the program clicks on the white space of a window first, then it can bring that window to the foreground first).

By the way, can Grease Monkey or any Firefox add-on be used to do this too?

+7  A: 

In Python there is ctypes and in Perl there is Win32::API

ctypes Example

from ctypes import *
windll.user32.MessageBoxA(None, "Hey MessageBox", "ctypes", 0);

Win32::Api Example

use Win32::GUI qw( WM_CLOSE );
my $tray = Win32::GUI::FindWindow("WindowISearchFor","WindowISearchFor");
Win32::GUI::SendMessage($tray,WM_CLOSE,0,0);
jitter
oh i installed Strawberry Perl but the above cannot run... do some modules need to be installed? It says: Can't locate Win32/GUI.pm in @INC (@INC contains: C:/strawberry/perl/lib C:/strawberry/perl/site/lib .) at try2.pl line 1.
動靜能量
Get Win32::GUI from http://search.cpan.org/~robertmay/Win32-GUI-1.06/docs/GUI.podFollow the appropriate instructions from http://www.cpan.org/modules/INSTALL.html
jitter
cpan -i Win32::GUI
Alexandr Ciornii
I think you meant Python, not Perl, for ctypes... I fixed it for you.
musicfreak
+8  A: 

If you are trying to automate some task in a website you might want to look at WWW::Selenium. It, along with Selenium Remote Control, allows you to remote control a web browser.

Chas. Owens
can it even click on Flash content?
動靜能量
It can click on anything in the browser.
Chas. Owens
A: 

I find this is easier to approach in Java or C++. Java has a Robot class that allows you to just pass x, y coordinates and click somewhere. Using C++, you can achieve that same functionality using mouse_event() or SendMessage() with the WM_MOUSE_DOWN flag. SendMessage is more technical but it allows you to use FindWindow() and send mouse clicks to a specific window, even if it's minimized.

Using a scripting language like Python or Ruby, I'd guess that you'd end up hooking into one of these Windows API functions anyway.

Kai
+4  A: 

See this:

http://xkcd.com/196/

Dan Lorenc
at least i don't have a girl here, so there is plenty of time to write the script.
動靜能量
+6  A: 

To answer the actual question, in Perl, you would use the SendMouse (and the associated functions) provided by the Win32::GuiTest module.

#!/usr/bin/perl

use strict;
use warnings;

use Win32::GuiTest qw( MouseMoveAbsPix SendMouse );

MouseMoveAbsPix(640,400);
SendMouse "{LEFTCLICK}";

__END__

UPDATE:

What if some virus scanning program pops up covering up the place where the click should happen?

In that case, you would use FindWindowLike to find the window and MouseClick to send a click to that specific window.

Sinan Ünür
+1 for Win32::GuiTest. It's a great tool. I wrote a bot (App::SweeperBot, available as a compiled .exe from http://sweeperbot.org/ ) that uses it to play minesweeper, so I no longer need to play it myself. ;)Win32::GuiTest also provides facilities for screen capture, and the Win32::GUIRobot (which is built on top) provides dedicated routines for finding images on the screen.
pjf
@pjf Thanks. I remember reading your article. Great idea, great implementation.
Sinan Ünür
+1  A: 

If using a different tool is allowed, you should take a look at AutoHotkey or AutoIt. These tools were made for this sort of thing, and I've always been keen on using the right tools for the right jobs.

AutoHotkey is based on AutoIt I believe, and it is my personal preference. You only really need 2 functions for what you're trying to achieve, MouseMove and MouseClick.

John T