I'm using a bookkeeping system that had a minor crash. Now I'm cleaning up, and using AutoHotKey 1.1.26.00 to do it. The process involves going through a long listbox of items, and for each item I have to right-click through a series of windows with both the keyboard and mouse. It's all working...mostly.
Before the last window closes, a dialog box pops up, on which a "No" button is clicked. I've added code to do this. But about 5% of the time the dialog has a different message, and this blocks the AHK script from continuing. I'm trying to get the text of the message, but I cannot determine how. The name of the control is Static2
, but none of the AHK functions seem able to access the text.
I used the following script that I found in the AHK help to determine the name of the control:
#Persistent
SetTimer, WatchCursor, 100
Return
WatchCursor:
MouseGetPos, X, Y, id, control
WinGetTitle, title, ahk_id %id%
WinGetClass, class, ahk_id %id%
ToolTip, ahk_id %id%`nahk_class %class%`n%title%`nControl: %control%`nXpos = %X%`nYpos = %Y%
Return
The script returns the following:
ahk_id 0x170586
ahk_class #32770
Control: Static2
Can I use this information to get the text in Static2
..? If so, how..?
Unfortunately the dialog does not have a window title of its own. I attempted to get the title, but found it matches the parent window. So...I don't know what to try next.
The full code of my AHK script is below and there's a screenshot at the end.
The dialog text which I need to check is usually Do you want to edit the accounting information at this time?
, but as stated, this is occasionally different, and I need to add more code to process that. This happens in the last section of the script:
;Written for AutoHotkey v1.1.26.00
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
DelayMenu1 := 500 ; Wait for the menu to open.
DelayMenu2 := 75 ; As the selection moves down each item in the menu.
DelayMenu3 := 200 ; So the last item selected can be seen.
DelayWin1 := 200 ; After each window opens, a little time for the controls to populate.
ItemPosX := 450 ; X-position of the first item in the list.
ItemPosY := 104 ; Y-position of the first item in the list.
ItemAddY := 14 ; Y-distance between each item.
;Activate the main window.
WinActivate, Dispatcher - Roadnet Transportation Suite
Sleep 500
;Get input from the user.
InputBox, C, Number of rows, How many rows?
If ErrorLevel
{
} Else {
; #################### BEGIN LOOP ####################
Loop, %C%
{
;Wait for the main window.
WinWaitActive, Dispatcher - Roadnet Transportation Suite, , 3
If ErrorLevel
{
MsgBox, Main window is not visible.
Exit
}
Sleep %DelayWin1%
; #################### START ####################
;Right-click the item and wait for the menu to open.
MouseMove, %ItemPosX%, %ItemPosY%
Click, %ItemPosX%, %ItemPosY%, Right
Sleep %DelayMenu1%
;Press the down key to "Start".
Loop, 8
{
SendInput, {DOWN}
Sleep %DelayMenu2%
}
Sleep %DelayMenu3%
SendInput, {ENTER}
;Wait for "Start Route" window.
WinWaitActive, Start Route, , 3
If ErrorLevel
{
MsgBox, Start Route window is not visible.
Exit
}
Sleep %DelayWin1%
;Use the Save keystroke. On other windows this does not work; mouse must be used.
SendInput ^s
Sleep 250
;Wait for the main window.
WinWaitActive, Dispatcher - Roadnet Transportation Suite, , 3
If ErrorLevel
{
MsgBox, Main window is not visible.
Exit
}
Sleep %DelayWin1%
; #################### STOP COMPLETE ####################
;Right-click the item and wait for the menu to open.
MouseMove, %ItemPosX%, %ItemPosY%
Click, %ItemPosX%, %ItemPosY%, Right
Sleep %DelayMenu1%
;Press the down key to "Stop Complete".
Loop, 2
{
SendInput, {DOWN}
Sleep %DelayMenu2%
}
Sleep %DelayMenu3%
SendInput, {ENTER}
;Wait for "Stop Complete" window.
WinWaitActive, Stop Complete, , 3
If ErrorLevel
{
MsgBox, Stop Complete window is not visible.
Exit
}
Sleep %DelayWin1%
;Click the "As Projected" button. The keyboard shortcut does not work; mouse must be used.
Click, 120, 36
Sleep 250
;Click the "Save" button. The keyboard shortcut does not work; mouse must be used.
;Note: At this point the button sticks in, and the mouse becomes an hourglass.
Click, 24, 36
Sleep 250
;Wait for the main window.
WinWaitActive, Dispatcher - Roadnet Transportation Suite, , 3
If ErrorLevel
{
MsgBox, Main window is not visible.
Exit
}
Sleep %DelayWin1%
; #################### COMPLETE ####################
;Right-click the item and wait for the menu to open.
MouseMove, %ItemPosX%, %ItemPosY%
Click, %ItemPosX%, %ItemPosY%, Right
Sleep %DelayMenu1%
;Press the down key to "Complete".
Loop, 9
{
SendInput, {DOWN}
Sleep %DelayMenu2%
}
Sleep %DelayMenu3%
SendInput, {ENTER}
;Wait for "Complete Route" window.
WinWaitActive, Complete Route, , 3
If ErrorLevel
{
MsgBox, Complete Route window is not visible.
Exit
}
Sleep %DelayWin1%
;For troubleshooting.
;MsgBox, %ClassID%`n%ControlName%
;WinGet, active_id, ID, A
;Click the "Save" button. The keyboard shortcut does not work; mouse must be used.
Click, 24, 36
Sleep 250
;Check if the accounting information dialog appeared.
;The message is usually: "Do you want to edit the accounting information at this time?"
DialogAppeared := 0
Loop, 10 ;Check 10 times.
{
;Check for
MouseMove, 340, 120
MouseGetPos, , , ClassID, ControlName
If (ControlName = "Button2") ;Use this button to check for the popup dialog.
{
DialogAppeared := 1
Break
} Else {
Sleep 250
}
}
If (DialogAppeared = 1)
{
;Click the "No" button. There is no keyboard shortcut; mouse must be used.
MouseMove, 340, 120
Click, 340, 120
} Else {
MsgBox, "The accounting information dialog could not be handled."
Exit
}
Sleep 250
; #################### DONE ####################
;Increment the row-Y value before repeating the loop.
ItemPosY := ItemPosY + ItemAddY
}
}
MsgBox, Done.
Exit