2
votes

I have a an autohotkey script that automates an Import Wizard GUI. When I RDP into the machine where this script resides I can run the script it works beautifully. The script runs on a 10 minutes schedule. If I have RDP’d into the machine at the time the schedule happens to run, I can see that the script runs beautifully.

When I log out of RDP (or even just minimize the window) the script runs on schedule and hangs at the very first window in the Import Wizard. If I go back to RDP I see that window sitting there and I see my script hanging as it waits for the next window to appear. If I click OK on the open window (which is exactly what the ahk script does) then my script continues and runs fine.

tldr: my auto hot key script only works when I’m watching!

Here’s some code …

#NoTrayIcon
#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.

saexe_file = C:\test.exe
company_file = Z:\ABC.SAI
import_file = D:\test.imp

Run, %saexe_file%

WinWait Simply Accounting Import - Select Company
WinActivate
Send %company_file%{Enter}

; IT HANGS HERE
WinWait Simply Accounting Import - Select File to Import from
WinActivate, Simply Accounting Import - Select File to Import from
Send %import_file%{Enter}

EDIT 07-12-2013: found the solution:

Instead of disconnecting from RDP, run this batch file …

for /f "usebackq skip=1 tokens=3" %%i in (query user %USERNAME%) do %windir%\System32\tscon.exe %%i /dest:console

3
What is the error message, with what rights did you run the script. - user1944441
@Armin There is no error message. The Select Company window opens. AHK activates it and sends the text and then waits for the Select File window to open. But it never opens because the Select Company window doesn't close. The scheduled task runs using the administrator user account. - drschultz
Then it seems to be a problem with your other program. - user1944441
GUI interactions likely will not work unless there is a user logged in. Are you using a console session? A way around this may be to use a vnc client, so the machine thinks someone is logged in and can perform GUI interactions. - Elliot DeNolf

3 Answers

1
votes

It isn't a problem with your scripts, per se.

When you are working in the RDP session, your account is logged in, and the script runs under your account.

To get it to run when you "aren't looking", you will have to get the system account to operate the execution of the file.

Try running the script using the Windows Task Scheduler, and use the System Account if it asks.

0
votes

Closing or disconnecting the RDP session won't work, as described above.

Instead, execute this command from a command window (you can put this in a bat file if you want):

tscon RDP-Tcp#0 /dest:console

The digit following the # character is your current RDP session #. You can find this in task manager / user's tab

This causes your RDP session to switch to "console" mode, and the GUI continues to function in this mode

Worked for me!

0
votes
SET TEMPFILE="%TEMP%\%RANDOM%.TXT"
query user %USERNAME% >%TEMPFILE%
for /f "usebackq skip=1 tokens=3" %%i in (%TEMPFILE%) do %windir%\System32\tscon.exe %%i /dest:console
del %TEMPFILE%

Needed to isolate the query user due to error "The system cannot find the file query." when called from a single for expression. Note this needs to run elevated.