Solved

Script for automating input on windows

Posted on 2014-09-16
20
251 Views
Last Modified: 2014-09-17
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:
This is the dialog when I am updating records
Any help would be appreciated.  I should emphasize that I am in no way a coder.
0
Comment
Question by:Thomas Zucker-Scharff
  • 11
  • 9
20 Comments
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40326347
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
0
 
LVL 26

Author Comment

by:Thomas Zucker-Scharff
ID: 40326601
Joe that looks good.  I will try it tomorrow since I just left work.  I'll mmake that the default script.
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40326621
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
0
 
LVL 26

Author Comment

by:Thomas Zucker-Scharff
ID: 40327885
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?
0
 
LVL 26

Author Comment

by:Thomas Zucker-Scharff
ID: 40327893
found it
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40328027
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.
0
 
LVL 26

Author Comment

by:Thomas Zucker-Scharff
ID: 40328359
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
New erro when running AHK scriptThis 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

0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40328429
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
0
 
LVL 26

Author Comment

by:Thomas Zucker-Scharff
ID: 40328618
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

0
 
LVL 51

Accepted Solution

by:
Joe Winograd, EE MVE earned 500 total points
ID: 40328685
I think you are right. It is sending some keys too fast, although I can't figure out from the videos exactly what the problem is. I think the Checking for Updates dialog takes a few seconds and that may be when the keys are sent. In any case, experiment with the timing of sending the three keys. You changed one wait to 6 seconds. I don't know if that's the right place for it or if you need the other Sleep commands also to be longer. Should be easy to figure out by experimentation.

> I thought that is what the ifwinexist command would do

Yes, it does wait for that window to exist, then activates it (makes it the focus window), then sends the three keys. As mentioned above, it may be that the Checking for Updates dialog intervenes, so you may need to wait that out (Sleep for a few seconds in the script). Regards, Joe
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 26

Author Comment

by:Thomas Zucker-Scharff
ID: 40328706
Thanks.  I will experiment.
0
 
LVL 26

Author Closing Comment

by:Thomas Zucker-Scharff
ID: 40328708
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!!
0
 
LVL 26

Author Comment

by:Thomas Zucker-Scharff
ID: 40328712
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?
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40328718
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
0
 
LVL 26

Author Comment

by:Thomas Zucker-Scharff
ID: 40328721
Thanks
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40328743
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
0
 
LVL 26

Author Comment

by:Thomas Zucker-Scharff
ID: 40328785
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

0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40328806
Try the script I posted in <http:#a40328743>.
0
 
LVL 26

Author Comment

by:Thomas Zucker-Scharff
ID: 40328813
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.
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40328962
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
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to count occurrences of each item in an array.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now