Link to home
Start Free TrialLog in
Avatar of Thomas Zucker-Scharff
Thomas Zucker-ScharffFlag for United States of America

asked on

Script for automating input on windows

I do the same exact thing in the same application on the same computer on a weekly basis.  I would like to automate some of this, especially the updating of database records.

I will explain the situation.  I use EndNote X7.x from Thomson Reuters to manage several thousand publications for close to 200 investigators.  The program is excellent in many ways but falls short in some fairly basic ways (I have suggested some changes to this app for about 5-6 years now).  What I do is import publications that I have emailed to me every sunday.  I need to go back and update the publications.  The program has a facility to look for updates on the web called "Find Reference Updates", but each one asks if you would like to Update all fields and/or update empty fields and then I have to click save or press enter.  For each window all I would need to send is {Enter}{Tab}{Enter}.  Something like this:

Loop
If WinExist {"Review Available Updates" & *}
     send {Enter}
     send {Tab}
     send {Enter}
Endif
End Loop

This is basically how the script would look in AutoHotkey, but I can't get it to work at all.  The first problem is that I haven't been able to figure out how to use wildcards in the window name.


The window I see is below:
User generated image
Any help would be appreciated.  I should emphasize that I am in no way a coder.
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Hi Thomas,

A few things first:

(i) There is no Endif or End Loop statement in AutoHotkey. You signify a block of code via braces.

(ii) The matching of a window title depends on the SetTitleMatchMode option, as follows:

1: A window's title must start with the specified WinTitle to be a match.

2: A window's title can contain WinTitle anywhere inside it to be a match.

3: A window's title must exactly match WinTitle to be a match.

(iii) The command is IfWinExist (not If WinExist — that is, no space).

(iv) If the window exists, you must activate it in order to send keys to it.

So here's the correct AHK code for your situation:

Loop
{
  SetTitleMatchMode,1 ; title must begin with string
  IfWinExist,Review Available Updates
  {
    WinActivate
    Send {Enter}
    Send {Tab}
    Send {Enter}
  }
}

Open in new window


The loop will run forever until you manually terminate the script — I don't know if that's what you want. If you want it to wait a while between checks, you could put this line before the final brace:

Sleep,60000 ; wait 60 seconds (parameter is milliseconds)

Open in new window


Regards, Joe
Avatar of Thomas Zucker-Scharff

ASKER

Joe that looks good.  I will try it tomorrow since I just left work.  I'll mmake that the default script.
Sounds good. I do a lot of work in AutoHotkey (has been my primary programming/scripting/keyboard-macro language for the past few years) and I'm confident we can get this working well for you. One other point: you may put all of the keys on one Send line, so the three Send lines could be:

Send {Enter}{Tab}{Enter}

Regards, Joe
I admit to being a noob when it comes to Autohotkey.  I went to edit the script this AM and can't find it.  If I start the app it exits saying my script is incorrect.  Where is it located?
found it
OK, glad you found it. You may put scripts wherever you want. They're simply plain text files with a file extension of AHK. If you can't remember where you put one, a file search for  *.ahk  will find your AutoHotkey scripts.
I must still be doing something wrong.  It works fine in that it seems to update the records, but it open each one and throws up another dialog
User generated imageThis dialog opens first and then the record in question opens behind it.  If I check the box to not ask again, it does anyway.
I used the following script:
; IMPORTANT INFO ABOUT GETTING STARTED: Lines that start with a
; semicolon, such as this one, are comments.  They are not executed.

; This script has a special filename and path because it is automatically
; launched when you run the program directly.  Also, any text file whose
; name ends in .ahk is associated with the program, which means that it
; can be launched simply by double-clicking it.  You can have as many .ahk
; files as you want, located in any folder.  You can also run more than
; one .ahk file simultaneously and each will get its own tray icon.

; SAMPLE HOTKEYS: Below are two sample hotkeys.  The first is Win+Z and it
; launches a web site in the default browser.  The second is Control+Alt+N
; and it launches a new Notepad window (or activates an existing one).  To
; try out these hotkeys, run AutoHotkey again, which will load this file.

;#z::Run www.autohotkey.com

Loop
{
  SetTitleMatchMode,1 ; title must begin with string
  IfWinExist,Review Available Updates
  {
    WinActivate
    Send {Enter}{Tab}{Enter}
  }
}

Sleep,60000 ; wait 60 seconds (parameter is milliseconds)


;^!n::
;IfWinExist Untitled - Notepad
;	WinActivate
;else
;	Run Notepad
;return
;
;
; Note: From now on whenever you run AutoHotkey directly, this script
; will be loaded.  So feel free to customize it to suit your needs.

; Please read the QUICK-START TUTORIAL near the top of the help file.
; It explains how to perform common automation tasks such as sending
; keystrokes and mouse clicks.  It also explains more about hotkeys.

;WinGet, id, list,,, Program Manager
;Loop, %id%
;{
;    this_id := id%A_Index%
;    WinActivate, ahk_id %this_id%
;    WinGetClass, this_class, ahk_id %this_id%
;    WinGetTitle, this_title, ahk_id %this_id%
;    MsgBox, 4, , Visiting All Windows`n%a_index% of %id%`nahk_id %this_id%`nahk_class %this_class%`n%this_title%`n`nContinue?
;    IfMsgBox, NO, break

Open in new window

Two things. First, the Sleep command is in the wrong place (note my earlier comment: "you could put this line before the final brace"). So it is is never being executed, since it is outside the Loop statement's block of code. It should look like this (but see my comment below about removing it anyway):

Loop
{
  SetTitleMatchMode,1 ; title must begin with string
  IfWinExist,Review Available Updates
  {
    WinActivate
    Send {Enter}{Tab}{Enter}
  }
  Sleep,60000 ; wait 60 seconds (parameter is milliseconds)
}

Open in new window


Second, that dialog box is not coming from AutoHotkey — it is coming from the app (note the title bar: EndNote). The same thing should happen if you hit Enter-Tab-Enter manually with the "Review Available Updates" window on the screen. If it doesn't, then one possibility is that it is sending the three keystrokes too fast and one, or more, is getting lost. I suggest waiting 50 milliseconds between keys (that's an arbitrary figure - long enough to work and short enough not to impact performance heavily). So the new code is this:

Loop
{
  SetTitleMatchMode,1 ; title must begin with string
  IfWinExist,Review Available Updates
  {
    WinActivate
    Send {Enter}
    Sleep,50
    Send {Tab}
    Sleep,50
    Send {Enter}
  }
  Sleep,60000 ; wait 60 seconds (parameter is milliseconds)
}

Open in new window


But I don't understand how the EndNote dialogs are coming onto the screen, so you may not want the 60-second wait. If they're popping onto the screen one-after-the-next, then you likely don't want to wait 60 seconds between checks for that dialog. In that case, remove the <Sleep,60000> line, or change it to a much smaller number, like 1000 (one second). Regards, Joe
Here is what I think is happening.  The command to send the enter is executing too quickly.  If you press Enter when items are highlighted, they will be opened.  I have linked 2 mp4 videos of how it should work and how it does work with the current AHK script (speaking of which I wasn't able to get a different script to execute).  If you have time please take a look.  Can I put a pause to wait for the window to show up before executing the commands (I thought that is what the ifwinexist command would do)?

Video of it working correctly using the keyboard
video of using AutoHotKey script to update

I used the script you suggested except put a sleep,6000 line before the first send

Loop
{
  SetTitleMatchMode,1 ; title must begin with string
  IfWinExist,Review Available Updates
  {
    WinActivate
    Sleep,6000
    Send {Enter}
    Sleep,50
    Send {Tab}
    Sleep,50
    Send {Enter}
  }
  Sleep,60000 ; wait 60 seconds (parameter is milliseconds)
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.  I will experiment.
Although this comment wasn't the actual solution, and I am still experimenting, the other comments Joe made did help me solve the problem to this point. THANKS!!
Joe,

I double click on a script I named endnoteupdates.ahk, which is just the portion of the default script I didn't comment out.  It opens up a new AHK system tray item but doesn't do anything.  I paused the other AHK script (the systray icon turned red), could that be the reason?
You're very welcome! Even though the question is closed, I'm happy to help further. Just post back here with any issues as you continue the experiments. Regards, Joe
Our messages just crossed. I don't what you mean by the default script, but let's talk just about this script:

Loop
{
  SetTitleMatchMode,1 ; title must begin with string
  IfWinExist,Review Available Updates
  {
    WinActivate
    Send {Enter}
    Sleep,5000
    Send {Tab}
    Sleep,5000
    Send {Enter}
    Sleep,3000
  }
}

Open in new window


This will loop forever looking for the Review Available Updates dialog and then will send the three keystrokes separated by however many seconds you want. This should work fine with the right choice of the wait times. Note that I put in a 3-second wait after sending the last Enter so that it doesn't find the (previous) Review Available Updates dialog still there (and I removed the 60-second wait).

Any number of AHK scripts may be running at the same time, so that's not the problem. Exit all of the scripts running now by right-clicking on the tray icon and selecting Exit and start over with the script above. Regards, Joe
I will try.  I experimented and found that the following script updated the first record and then seemed to quit.  I will try the sleeps between sends.

Loop
{
  SetTitleMatchMode,1 ; title must begin with string
  IfWinExist,Review Available Updates
  {
    WinActivate
    Send {Enter}
    Send {Tab}
    Send {Enter}
    Sleep,6000 ; wait 6 seconds (parameter is milliseconds)
  }
}

Open in new window

Try the script I posted in <http:#a40328743>.
Joe,

It turns out there was a combination of errors.  The script is now working fine.  It is as follows:

Loop
{
  SetTitleMatchMode,1 ; title must begin with string
  IfWinExist,Review Available Updates
  {
    WinActivate
    Send {Enter}
    Sleep,100
    Send {Tab}
    Sleep,100
    Send {Enter}
    Sleep,3000 ; wait 3 seconds (parameter is milliseconds)
  }
}

Open in new window


I made the noob mistake of not realizing that I had to reload the script after I changed it.  

Thanks again.
Great news! Yes, you may select from the tray context menu either Reload This Script or Exit (and, in the latter case, run it again). I'm very glad that all is working well for you now. Regards, Joe