Script only works once

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Krash
Posts: 13
Joined: 24 Feb 2016, 21:38

Script only works once

24 Feb 2016, 21:43

Trying to create hotkey to delete IE11 history. It runs once, then requires manual input to finish.

Code: Select all


; Delete history in IE11

#persistent
SetTitleMatchMode, 2
Return

#IfWinActive ahk_class IEFrame

f12::

Send, ^+{Del}
Send, d

The Send, d doesn't send.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Script only works once

24 Feb 2016, 22:33

Krash wrote:The Send, d doesn't send.
Or, maybe AHK sends "d" before the window is created. Try using WinWait / WinWaitActive.

Also, it's just a good habit to put a return at the end of all your hotkey subroutines.

Code: Select all

...
Send, d
return
Krash
Posts: 13
Joined: 24 Feb 2016, 21:38

Re: Script only works once

24 Feb 2016, 22:47

Tried SetKeyDelay, no help, and had return at end, also didn't help. Will try your suggestion.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Script only works once

24 Feb 2016, 22:52

Yeah, try WinWaitActive.
My comment about the return was just a very minor point.
Krash
Posts: 13
Joined: 24 Feb 2016, 21:38

Re: Script only works once

24 Feb 2016, 22:53

This works -

Code: Select all


; Delete history in IE11

#persistent
SetTitleMatchMode, 2
Return

#IfWinActive ahk_class IEFrame

f12::

Send, ^+{Del}
WinWaitActive [, , Delete, 1, ,]
Send, d
Return

Thanks. Image
Krash
Posts: 13
Joined: 24 Feb 2016, 21:38

Re: Script only works once

24 Feb 2016, 23:41

Ok, am trying to close the annoying confirm popup at bottom of browser window. Tried this, but doesn't work. Closes previous popup, but not current one -

Code: Select all


; Delete history in IE11

#persistent
SetTitleMatchMode, 2
Return

#IfWinActive ahk_class IEFrame

f12::

Send, ^+{Del}
WinWaitActive [, , Delete, 1, ,]
Send, d
WinWait 5
Click 1111, 705, 1
Return

It's not a window, doesn't have a window name, but it does have a ClassNN.
Krash
Posts: 13
Joined: 24 Feb 2016, 21:38

Re: Script only works once

25 Feb 2016, 13:21

Still unable to delay mouse click. Cursor moves to correct coordinates, but it's already clicked before popup opens.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Script only works once

25 Feb 2016, 14:55

Maybe something like this:

Code: Select all

CoordMode, Mouse, Window        ; Set coordmode for when clicking the mouse
CoordMode, Pixel, Window        ; Set coordmode for ImageSearch
return                          ; End of Auto-execute section

#IfWinActive ahk_class IEFrame
f12::
Send, ^+{Del}
WinWaitActive, Delete Browsing History ahk_exe iexplore.exe,, 1
if (ErrorLevel)                 ; If WinWaitActive timed out, return
    return
ControlClick, Button9,,,,, NA   ; Click the Delete button
WinWaitActive, ahk_class IEFrame,, 1
if (ErrorLevel)
    return
WinGetPos, WinX, WinY, WinW, WinH ; Get area of the IE window
Loop, 20                        ; Try to find the image 20 times before giving up
{
    ImageSearch, FoundX, FoundY, 0, 0, WinW, WinH, % A_ScriptDir "\MyImage.png"
    if (ErrorLevel = 0)         ; Image found
    {
        FoundX += 10            ; ImageSearch found the top left of the image, so add an offset
        FoundY += 25
        MouseGetPos, MouseX, MouseY
        MouseClick, L, %FoundX%, %FoundY%,, 0
        MouseMove, MouseX, MouseY, 0
        break
    }
    else if (ErrorLevel = 1)    ; Image not found
        Sleep, 500              ; try again in 0.5 seconds
    else if (ErrorLevel = 2)
    {
        MsgBox, 16, ImageSearch Error, 
        (Join LTrim
            There was a problem that prevented ImageSearch from conducting the search 
            (such as failure to open the image file or a badly formatted option).
        )
        break
    }
}
return
#If
I had to resort to using ImageSearch to find the X, which is not ideal, but this is working for me.

This is an example of the image I searched for:
MyImage.png
MyImage.png (236 Bytes) Viewed 4417 times
That image will only work if you have the same resolution monitor as me. You will need to take a screenshot and crop it so the image matches what is displayed on your monitor.
Last edited by kon on 25 Feb 2016, 15:15, edited 1 time in total.
Krash
Posts: 13
Joined: 24 Feb 2016, 21:38

Re: Script only works once

25 Feb 2016, 15:14

Did a workaround -

Code: Select all


Send, ^+{Del}
WinWaitActive [, , Delete, 1, ,]
Send, d
WinWait 5
Click 1111, 705, 40
Return

It werks gud!
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Script only works once

25 Feb 2016, 16:58

Whoa, buddy, what does WinWait 5 mean? Looks scary to me...
try it and see
...
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Script only works once

25 Feb 2016, 17:03

derz00 wrote:Whoa, buddy, what does WinWait 5 mean? Looks scary to me...
It means, "Look for a window named '5' and when it inevitably times out proceed with the rest of the script."
It's essentially doing the same thing as Sleep would (unless there is actually a window named "5").
He doesn't seem to have read my post... :|
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Script only works once

25 Feb 2016, 17:32

So how long is your timeout?
try it and see
...
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Script only works once

25 Feb 2016, 17:35

Hmm, now that I look, the docs say "Leave [timeout] blank to wait indefinitely."
I don't see how his script would work...

I had remembered incorrectly, I thought it defaulted to 0.5 sec.
Krash
Posts: 13
Joined: 24 Feb 2016, 21:38

Re: Script only works once

26 Feb 2016, 12:12

Well, it was working yesterday, but not today. The WinWait 5 is supposed to create a 5 second delay. It doesn't, but without it the script leaves browser and goes to desktop. The 40 clicks seemed to have solved the problem, but now it's not working. It's gone back to the original problem of only running once. Will have another look at kon's code when I have time tonight.
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Script only works once

26 Feb 2016, 15:25

To create a clumsy delay with WinWait, you would do it like this:

Code: Select all

WinWait, , , 5
I can't prove whether or not this would work.
This is how you delay 5 seconds:

Code: Select all

Sleep, 5000
Much more reliable, accurate, and specific. (Someone reviewing your code would actually know what you mean.) :)
So it goes, we all have a lot to learn... :clap:
try it and see
...
Krash
Posts: 13
Joined: 24 Feb 2016, 21:38

Re: Script only works once

26 Feb 2016, 21:27

Well, this seems to be working like it was last night -

Code: Select all


Send, ^+{Del}
WinWaitActive [, , Delete, 1, ,]
Send, d
Sleep, 1000
Click 1111, 705, 40
Return

Will try it for a while and see what happens.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Script only works once

26 Feb 2016, 23:13

Krash wrote:

Code: Select all

WinWaitActive [, , Delete, 1, ,]
No. That won't work. You're doing something weird if you think that works.
Look at the examples in the docs; they look nothing like that.
Krash
Posts: 13
Joined: 24 Feb 2016, 21:38

Re: Script only works once

26 Feb 2016, 23:25

I got that from the docs, and it does work. That was the easy part. Send, ^+{Del} opens a window with text 'Delete' in it, Send, d clicks Delete button. The problem has been to relocate the cursor and click the x to close the notification popup. Everything's working now, but sometimes it hangs after window opens.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Script only works once

26 Feb 2016, 23:44

Your code is still wrong and will not work. If you think it detects the window this will prove you wrong: try placing a MsgBox on the next line and see if the message box ever appears (I don't think it will because your code will hang on the WinWaitActive line indefinitely - it will never find the window because you mangled the WinWaitActive command).

You copied the prototype. The [ and ] indicate optional parameters; don't include them in your code literally. Scroll down to the bottom of the page to see the real example.
Krash
Posts: 13
Joined: 24 Feb 2016, 21:38

Re: Script only works once

27 Feb 2016, 13:53

Thanks for the link, will have a look soon as I get some time. The script is working fine today. Image

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, mikeyww, Peiya, ShatterCoder, sofista and 300 guests