Script works as intended as an .ahk file, but not as an .exe?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
TheEggman
Posts: 7
Joined: 05 Feb 2023, 08:25

Script works as intended as an .ahk file, but not as an .exe?

Post by TheEggman » 05 Feb 2023, 08:33

Hi! First time posting here, long time enjoyer.
I'm writing some scripts to easily change the printer selection for my work. These will be called by a C# GUI - so to make things easier on myself I thought I'd just compile some ahk scripts as .exe and run it through C# with Process.start.

I got my script working as intended and went to compile the script as an .exe but it seems it doesn't recognize the if statement and just plainly ignores it. Resulting in multiple instances of the msgbox.
It also straight up ignores the WinActivate function.

TLDR: Script works as intended as .ahk file but not as .exe

Here's my code:

Code: Select all

if WinExist("Print")
	{
		Sleep, 100
		WinActivate, Print
		Sleep, 100
		ControlClick, ComboBox1, Print
		Sleep, 10
		Send, M
		Send, A
		Send, A
		Send, A
		Send, A
		Send, {Enter}

		Sleep, 1000
		MsgBox, 64, A4 Zwart/Wit, U heeft geslecteerd: A4 Zwart/Wit, 5
		Exit
	}

else
	MsgBox, "ERROR, no print menu found, exiting"
	Exit

I hope you guys can help me out! Thanks in advance :wave:

User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Script works as intended as an .ahk file, but not as an .exe?

Post by mikeyww » 05 Feb 2023, 09:13

Welcome to this AutoHotkey forum!

You can display the value of your WinExist function call. This is a more direct way to understand the result.

A way to debug:

1. Run the script below, with no additional code, and no other scripts running.
2. If you get 0x0 as the output, post the script that you ran in a new reply below, and post a screenshot of the Window Spy output that shows the window information for your target window.

Code: Select all

#Requires AutoHotkey v1.1.33
winTitle := "Print"
MsgBox % WinExist(winTitle)
There is usually no fundamental reason for most compiled scripts to yield a different result, but some antivirus software flags or terminates compiled programs.

Why would you get multiple message boxes if you run the script once? Were you showing the entire script?

TheEggman
Posts: 7
Joined: 05 Feb 2023, 08:25

Re: Script works as intended as an .ahk file, but not as an .exe?

Post by TheEggman » 05 Feb 2023, 09:36

Thank you for your quick reply.

If I run the script you provided as a .exe the msgbox returns 0x2114e, not 0x0.

Seems to return the same thing when running as a .ahk.

Yes this is the entire script!

The windowspy info for the Print window:
https://imgur.com/a/Wlu6hvI

I think the multiple boxes result from the Send, {Enter} command executing the script once again for some reason. But I'm not sure on this either.

Hope I provided you with all the info you need :) Thanks again

User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Script works as intended as an .ahk file, but not as an .exe?

Post by mikeyww » 05 Feb 2023, 10:41

WinExist is an instantaneous check. To wait for the window first, use :arrow: WinWaitActive. Include the timeout, and check the ErrorLevel.

TheEggman
Posts: 7
Joined: 05 Feb 2023, 08:25

Re: Script works as intended as an .ahk file, but not as an .exe?

Post by TheEggman » 05 Feb 2023, 11:21

Thanks, this has resolved the multiple instance issue. But still the program doesn't execute all of the code.

I've updated the code to this:

Code: Select all

WinActivate, Print
WinWaitActive, Print, ,2

if(ErrorLevel == 0)
{
		ControlClick, ComboBox1, Print
		Sleep, 10
		Send, M
		Send, A
		Send, A
		Send, A
		Send, A
		MsgBox, 64, A4 Zwart/Wit, U heeft geslecteerd: A4 Zwart/Wit, 5
		Exit
}

else
	MsgBox, 16, ERROR, Error`, Print window not found., 5
Which is already a lot more reliable and cleaner, and runs flawlessly as .ahk but still it refuses to run these commands as an .exe;

Code: Select all

WinActivate, Print
WinWaitActive, Print, ,2
ControlClick, ComboBox1, Print
The only thing it does is pop up the MsgBox at the end, saying "u heeft geselecteerd: A4 Zwart/Wit". Though I do think it activates the Send commands correctly.

I really appreciate your time and quick responses

User avatar
kdaube
Posts: 86
Joined: 02 Nov 2015, 03:11

Re: Script works as intended as an .ahk file, but not as an .exe?

Post by kdaube » 05 Feb 2023, 12:02

«There is usually no fundamental reason for most compiled scripts to yield a different result, but some antivirus software flags or terminates compiled programs.»
Among these AV software is Windows Defender. Publishing AHK exe is a need, because an AHK interpreter can not be assumed for all user of one’s scripts. Hence in my documentation I have a paragraph about the fact, that WD may block the script from executed. My instruction is to run such scripts from a distinct folder, which gets excluded from the WD scan:
Avoid Windows Defender intervention
1 In Windows settings navigate to Windows Security > Virus & Threat protection settings > Manage Settings
2 Go to Exclusions
3 Add the folder you need to exclude:
C:\Users\username\AppData\Roaming\D+DD
Klaus Daube, Zürich, CH

TheEggman
Posts: 7
Joined: 05 Feb 2023, 08:25

Re: Script works as intended as an .ahk file, but not as an .exe?

Post by TheEggman » 05 Feb 2023, 12:21

I've excluded the folder where the .exe file is located (C:\PrintAssist), yet it still yields the same result as before. Am I understanding your suggestion correctly?

User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Script works as intended as an .ahk file, but not as an .exe?

Post by mikeyww » 05 Feb 2023, 12:48

If you are seeing the MsgBox inside your first block, then it means that the commands that precede it have executed unless antivirus blocked them. It's quite possible that those commands are having unintended or unexpected effects. If the same but uncompiled script completely works, then an antivirus block seems likely. You can disable Defender to see whether the issue is rectified. I also recommend simplifying your block so that you are sending only a single key instead of a long sequence, during your testing. This may enable you to determine whether a single Send command works. Use a Send sequence that would enable to you see something happen in your target window.

Excluding your folder does nothing, most likely. You should exclude the compiled script as a process.

TheEggman
Posts: 7
Joined: 05 Feb 2023, 08:25

Re: Script works as intended as an .ahk file, but not as an .exe?

Post by TheEggman » 05 Feb 2023, 17:39

I think it had something to do with my compiler, I compiled it on another PC using the exact same script and it worked like a charm.

I still don't know exactly what it was that went wrong but I got it working in the end by selecting the Base file [v1.1.36.02 U64 AutoHotkeyU64.exe] when compiling and selecting a different folder to compile to. (Maybe the compiler was having difficulty compiling to a same folder with an .exe already existing under the same name)

Thanks for all of your help, I'll mark this thread as Solved from this point on :D

TheEggman
Posts: 7
Joined: 05 Feb 2023, 08:25

Re: Script works as intended as an .ahk file, but not as an .exe?

Post by TheEggman » 06 Feb 2023, 03:59

After further inspection and having a fresh look at the issue it seems the code I provided was looking for any window containing the word 'Print'.

So the folder Print Assist, where the program was located and the GUI I made in C# - named "Print Assist" - were both recognised as the windows I was looking for...

Such a simple solution, such a stupid issue.

Thank you for your time.

User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Script works as intended as an .ahk file, but not as an .exe?

Post by mikeyww » 06 Feb 2023, 07:05

This issue itself would have nothing to do with the compiler, and would not yield any differences between a compiled and uncompiled script.
Script works as intended as .ahk file but not as .exe
I think that this statement would have to be false if your most recent explanation is accurate.

TheEggman
Posts: 7
Joined: 05 Feb 2023, 08:25

Re: Script works as intended as an .ahk file, but not as an .exe?

Post by TheEggman » 06 Feb 2023, 07:39

True, might not have mentioned that I had a folder called ...\AHKS where I saved all of my ahk scripts and compiled them to the folder C:\PrintAssist. So they ran fine because the window title was AHKS and not PrintAssist. :facepalm:

User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Script works as intended as an .ahk file, but not as an .exe?

Post by mikeyww » 06 Feb 2023, 07:57

At least you figured it out. I almost always add a window class or process name to the WinTitle, because there are many other situations where a simple window title is not unique.

Post Reply

Return to “Ask for Help (v1)”