drschultz
drschultz

Reputation: 157

Auto hot key script only works when watching

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

Upvotes: 2

Views: 2331

Answers (3)

Jeff Kremer
Jeff Kremer

Reputation: 31

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.

Upvotes: 0

hanksterr7
hanksterr7

Reputation: 105

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!

Upvotes: 0

bgmCoder
bgmCoder

Reputation: 6371

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.

Upvotes: 1

Related Questions