Sign in to follow this  
Followers 0
AlexK

Script to {

5 posts in this topic

Hi All,

I have a script that is supposed to do a very simple operation: activate a particular window, open the edit box (Ctrl-E), select all (Ctrl-A), paste the clipboard contents (Ctrl-V), and press Escape.

Opt("WinTitleMatchMode", 4)
If WinActivate("[REGEXPTITLE:mydeck.+ - Anki]") <> 0 Then
  WinWaitActive("[REGEXPTITLE:mydeck.+ - Anki]","",4)
  If WinActive("[REGEXPTITLE:mydeck.+ - Anki]") <> 0 Then
    Sleep(100)
    Send("^e")
    Sleep(100)
    Send("^a")
    Sleep(100)
    Send("^v")
    Sleep(100)
    Send("{ESCAPE}")
  EndIf
EndIf

The window title may vary - it may be "mydeck (25 items left) - Anki" or "mydeck* - Anki" but anyway the regex always matches and AutoIt does find it, because it always switches to that window reliably.

However the problem is that after switching to the Anki window, 20% of the time AutoIt exits (the little tray icon disappears) without ever sending the keystrokes. I've tried putting in Sleep calls and other various stuff, but being a newbie at AutoIt couldn't find a reliable solution.

It is run from a batch file with the line

AutoIt3.exe ankipaste.au3

I was unable to find reliable repro steps; simply re-running the bat file has an ~80% chance of succeeding.

Is there anything outwardly wrong with the script? Thank you very much.

Share this post


Link to post
Share on other sites



Sorry about the title; clicked Submit before editing it! Unfortunately I can't seem to be able to edit it now.

It should read "Script to {switch to window and paste} aborts prematurely".

Share this post


Link to post
Share on other sites

Thank you omikron48!

Unfortunately the particular fix you linked to didn't work directly; however it helped me figure out where to dig further. It turns out that putting a Sleep(200) right after WinActivate was enough to make sure AutoIt realizes it switched to the desired window correctly.

I guess this is because, according to documentation, AutoIt makes six attempts during WinActivate, then gives up. The window in question being a Python application, maybe it is a little sluggish? So after switching to it maybe it wasn't responding quickly enough to AutoIt's "are you active now?" probing.

In any case, thanks.

Share this post


Link to post
Share on other sites

Yeah. There's also the case where the interface isn't ready to accept input the moment the window is active. I think that's what happened to you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0