AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Newbie, execute script based on webpage button press.
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
armed23ogm



Joined: 23 Apr 2009
Posts: 13

PostPosted: Sun Oct 04, 2009 5:17 pm    Post subject: Newbie, execute script based on webpage button press. Reply with quote

Hello, I haven't used AutoHotkey in a while and was wondering how to do this:

I want to execute a script based on whether the "Google Search" button is pressed or if the "I'm Feeling Lucky" button is pressed on www.Google.com. I want to do this without using the image search command. (My real application is a little different, but if I figure out how to do this simple example, I will be able to expand on it)

Thanks for any help!
Back to top
View user's profile Send private message
tank



Joined: 21 Dec 2007
Posts: 3700
Location: Louisville KY USA

PostPosted: Sun Oct 04, 2009 6:10 pm    Post subject: Reply with quote

there are 2 methods one involves parsing out the URL to see if you stay in the same domain. Based on your description tho this isnt what you need

The next option is much more complex and not for the feint of heart. I wont write you an example unless i get a comfirmation from you that you understand DOM very well events and either javascript or vbscript as my example will rely heavily on these principles andf im not inclined to teach you events or DOM
I will however show you how to capture an onclick event for a button using AHK

Please let me know for sure if this is worth myt effort or if your not readdy to learn something much more complicated
_________________

We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Back to top
View user's profile Send private message
armed23ogm



Joined: 23 Apr 2009
Posts: 13

PostPosted: Sun Oct 04, 2009 6:21 pm    Post subject: Reply with quote

Thanks for the reply tank. Yeah this may be more complicated than I anticipated. My real application is to detect a button press in an offline program, so the parsing of the domain name would not be a viable option. I'll probably read up on using Javascript with AutoHotkey to make this work. Thanks again.
Back to top
View user's profile Send private message
jethrow



Joined: 24 May 2009
Posts: 1907
Location: Iowa, USA

PostPosted: Sun Oct 04, 2009 6:52 pm    Post subject: Reply with quote

I wanted to see if I could do this:
Code:
COM_Init() ; requires COM Standard Library
pwb := COM_CreateObject("InternetExplorer.Application")
COM_Invoke(pwb, "Visible", "True")
COM_Invoke(pwb, "Navigate", "www.google.com")
While, % COM_Invoke(pwb, "ReadyState") <> 4
   Sleep, 10

GS := COM_Invoke(pwb, "document.all.btnG")
IFL := COM_Invoke(pwb, "document.all.btnI")
sink1 := COM_ConnectObject(GS, "GS_")
sink2 := COM_ConnectObject(IFL, "IFL_"), done:=False
While(!done)
   Sleep, 10

; Cleanup
COM_DisconnectObject(sink1), COM_DisconnectObject(sink2)
COM_Release(pwb), COM_Release(GS), COM_Release(IFL), COM_Term()
Return


GS_OnClick() {
   SetTimer, Search, -10 ; EDIT - changed from GoSub per tank's recommendation below
}
IFL_OnClick() {
   SetTimer, Lucky, -10
}
Search:
   MsgBox, Google Search
   done:=True
   Return
Lucky:
   MsgBox, I'm Feeling Lucky
   done:=True
   Return

_________________
Very Happy - in case I forgot to smile
Basic Webpage Controls
COM Object Reference


Last edited by jethrow on Sun Oct 04, 2009 7:21 pm; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
tank



Joined: 21 Dec 2007
Posts: 3700
Location: Louisville KY USA

PostPosted: Sun Oct 04, 2009 6:59 pm    Post subject: Reply with quote

is it active content such as a cd or something?
Using javascript via ahk is not any more straight forward than what i would propose and in fact may be a little more problematic
do you understand onclick events and DOM enuff to do this with javascript?
if so then this might be a road you want to persue

Code:
;------------------------------------------------------------------------------
; Requires COM.ahk Standard Library
; by Sean
; http://www.autohotkey.com/forum/topic22923.html
; Requires iWeb.ahk
; by tank
; http://www.autohotkey.com/forum/viewtopic.php?p=197475#197475
;------------------------------------------------------------------------------
/* written by tank to help with topic
http://www.autohotkey.com/forum/viewtopic.php?t=49594
Newbie, execute script based on webpage button press.
10/4/2009
*/
#Persistent
COM_CoInitialize()
OnExit,Close

If   pwb:=iWeb_getwin("google") {
   If   feel_lucky:=COM_Invoke(pwb,"document.all[btnI].")
      psink:=COM_ConnectObject(feel_lucky,"buttons_") ;buttons_ is a function prefix
   COM_Release(pwb)
}
Return

somecode:
{
   MsgBox some code to execute here
   Return
}

buttons_onClick()
{
   SetTimer,somecode,-10 ; do this to execute a thread this is the recommended method to execute some code
}

Close:
COM_CoUninitialize()
ExitApp

_________________

We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Back to top
View user's profile Send private message
tank



Joined: 21 Dec 2007
Posts: 3700
Location: Louisville KY USA

PostPosted: Sun Oct 04, 2009 7:03 pm    Post subject: Reply with quote

jethrow good work ( you posted before i did Twisted Evil ) but IMO
use
Code:
settimer,label,-10

events you wouldn't want the event to hold up the execution of the button while some code that may be lengthy executes. the page would not navigate till the event is done
_________________

We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Back to top
View user's profile Send private message
armed23ogm



Joined: 23 Apr 2009
Posts: 13

PostPosted: Sun Oct 04, 2009 7:06 pm    Post subject: Reply with quote

It's a program where my Mom writes her notes for work. The program has a lot of repetitive tasks and I told my Mom I would try to help her automate some of the stuff. Right now I have all the automated tasks as hotkeys, but I thought I could improve it by detecting when buttons are pressed and executing certain lines of the script.

I will have to do some research on the scripting you guys posted, it is all foreign to me.

Thanks.
Back to top
View user's profile Send private message
tank



Joined: 21 Dec 2007
Posts: 3700
Location: Louisville KY USA

PostPosted: Sun Oct 04, 2009 7:07 pm    Post subject: Reply with quote

Just to be clear
the code posted will ONLY work with a web page loaded in IE
Detecting a button in other programs is done totally differently and sometimes not at all

this also assumes that the application is not flash or java (java is different than JavaScript)
_________________

We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Back to top
View user's profile Send private message
rasco22862



Joined: 26 Feb 2009
Posts: 29

PostPosted: Thu Mar 04, 2010 2:27 am    Post subject: Reply with quote

Thanks! Just what i was looking for! But the script only works for once. I mean, the second time that I press the "Search" Button for example, "SomeCode" doesn´t trigger. I´m looking kind of a steady-state. Every time that the seach button is pressed, execute some code. How can i achieve this?

Thanks again!
Back to top
View user's profile Send private message
tank



Joined: 21 Dec 2007
Posts: 3700
Location: Louisville KY USA

PostPosted: Thu Mar 04, 2010 3:17 am    Post subject: Reply with quote

psink is destroyed with navigation upon a new load you would need to connect the object again
_________________

We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Back to top
View user's profile Send private message
rasco22862



Joined: 26 Feb 2009
Posts: 29

PostPosted: Fri Mar 05, 2010 11:46 pm    Post subject: Reply with quote

Thanks again. I tried a lot, but i dont know how to control the flow of the script. What i want is that "somecode" executes every time an user clicks the button of a specific window(with a specific title of corse). This windows can be closed, opened and reloaded several times. Any suggestion?


Thanks!
Back to top
View user's profile Send private message
rasco22862



Joined: 26 Feb 2009
Posts: 29

PostPosted: Sun Mar 07, 2010 5:27 pm    Post subject: Reply with quote

Confused Any help?
Back to top
View user's profile Send private message
jethrow



Joined: 24 May 2009
Posts: 1907
Location: Iowa, USA

PostPosted: Sun Mar 07, 2010 10:40 pm    Post subject: Reply with quote

How about something like this?
Code:
#Persistent
SetTimer, ConnectGoogle, 100
Return

ConnectGoogle:
If WinExist( "Google - Windows Internet Explorer" ) {
   If Not pwb {
      pwb := IEGet( "Google - Windows Internet Explorer" )
      sink := COM_ConnectObject( doc:=pwb.document, "Doc_" )
      sink1 := COM_ConnectObject( doc.all.btnG, "GS_" )
      sink2 := COM_ConnectObject( doc.all.btnI, "IFL_" )
      done:=False
      While( !done )
         Sleep, 10
      GoSub, DisconnectGoogle
   }
} Else
   GoSub, DisconnectGoogle
Return

DisconnectGoogle:
   COM_DisconnectObject( sink ), COM_DisconnectObject( sink1 ), COM_DisconnectObject( sink2 ), pwb := ""
Return
Doc_OnReadyStateChange() {
   SetTimer, PageChange, -10
}
PageChange:
   While, pwb.ReadyState <> 4
      Sleep, 10
   done := True
Return
GS_OnClick() {
   SetTimer, Search, -10
}
IFL_OnClick() {
   SetTimer, Lucky, -10
}
Search:
   MsgBox, Google Search
   done:=True
Return
Lucky:
   MsgBox, I'm Feeling Lucky
   done:=True
Return

IEGet( Name="" ) {
   COM_Error( False )
   IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame
   Name := Name="New Tab" ? "about:Tabs" : RegExReplace( Name," - (Windows|Microsoft) Internet Explorer" )
   Loop, % ( oShell := COM_CreateObject( "Shell.Application" ) ).Windows.Count
      If pweb := oShell.Windows( A_Index-1 )
         If ( pweb.LocationName=Name && InStr( pweb.FullName, "iexplore.exe" ) ) {
            COM_Error( True )
            Return, pweb
         }
}

_________________
Very Happy - in case I forgot to smile
Basic Webpage Controls
COM Object Reference
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
rasco22862



Joined: 26 Feb 2009
Posts: 29

PostPosted: Tue Mar 09, 2010 2:56 am    Post subject: Reply with quote

Thanks jethrow! I get an illegal character error message in line 9 in the "pwb.document" expression. What is this about?

Thanks again
Back to top
View user's profile Send private message
jethrow



Joined: 24 May 2009
Posts: 1907
Location: Iowa, USA

PostPosted: Tue Mar 09, 2010 2:58 am    Post subject: Reply with quote

I wrote this for AHKL & COM_L - see my signature.
_________________
Very Happy - in case I forgot to smile
Basic Webpage Controls
COM Object Reference
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group