Why does this script sometimes does nothing?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
sn1perwild
Posts: 70
Joined: 04 Aug 2021, 15:11

Why does this script sometimes does nothing?

17 May 2024, 11:36

I have a script that uses a SetTimer to adjust the dimensions and placement of WhatsApp's Windows Store app every time it is in focus, since for some reason it gest bigger every time I launch it.

The script works but I noticed that occasionally it doesn't. Nothing has changed: the class and exe are the same and there is nothing else in the script that might create a hang. Can someone help me see what might be the issue? I have this script running at all times.

Code: Select all

#Persistent

Menu, Tray, Icon, Icons\shell32_184.ico

hasMoved := false
SetTimer, CheckWhatsApp, 1000

CheckWhatsApp:
IfWinExist, ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe
{
    if (!hasMoved)
    {
        WinMove, ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe,, 2821, 153, 1476, 1044
        hasMoved := true
    }
}
else
{
    hasMoved := false
}
return
User avatar
Chunjee
Posts: 1479
Joined: 18 Apr 2014, 19:05
Contact:

Re: Why does this script sometimes does nothing?

17 May 2024, 12:14

Deprecated: These control flow statements are not recommended for use in new scripts. Use the WinExist function instead.
User avatar
sn1perwild
Posts: 70
Joined: 04 Aug 2021, 15:11

Re: Why does this script sometimes does nothing?

17 May 2024, 12:29

Chunjee wrote:
17 May 2024, 12:14
Deprecated: These control flow statements are not recommended for use in new scripts. Use the WinExist function instead.
Thanks for your reply! You think it might have something to do with the command being deprecated? I tried changing it to the following and will test for a while and report if the problem occurs again.

Code: Select all

if WinExist("ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe")
User avatar
boiler
Posts: 17286
Joined: 21 Dec 2014, 02:44

Re: Why does this script sometimes does nothing?

17 May 2024, 18:30

Maybe it's hanging because you're moving it so often -- every two seconds -- whether it needs to or not and it causes an issue in Windows. Not sure what you're trying to accomplish with hasMoved. It gets reset from false to true every other time (and the opposite the other times), having nothing to do with whether it needs moving or not. Might as well remove all the conditional stuff and change the timer period to two seconds and move it every time.
User avatar
sn1perwild
Posts: 70
Joined: 04 Aug 2021, 15:11

Re: Why does this script sometimes does nothing?

17 May 2024, 18:34

boiler wrote:
17 May 2024, 18:30
Maybe it's hanging because you're moving it so often -- every two seconds -- whether it needs to or not and it causes an issue in Windows. Not sure what you're trying to accomplish with hasMoved. It gets reset from false to true every other time (and the opposite the other times), having nothing to do with whether it needs moving or not. Might as well remove all the conditional stuff and change the timer period to two seconds and move it every time.
I added that hasMoved to stop the script from keeping the window stuck in its place. I want the script to resize and move the window only once when I open the app and let me interact with it. But it should always resize and move it if I open it again.
User avatar
boiler
Posts: 17286
Joined: 21 Dec 2014, 02:44

Re: Why does this script sometimes does nothing?

17 May 2024, 18:57

But it doesn’t move it only once. It moves it every other time because the variable keeps flipping from true to false and vice versa every time through the loop. What about the logic you set up makes you think it only does it once?
User avatar
sn1perwild
Posts: 70
Joined: 04 Aug 2021, 15:11

Re: Why does this script sometimes does nothing?

17 May 2024, 19:23

boiler wrote:
17 May 2024, 18:57
But it doesn’t move it only once. It moves it every other time because the variable keeps flipping from true to false and vice versa every time through the loop. What about the logic you set up makes you think it only does it once?
The hasMoved variable is only set to false when the window is not active. The window is not active all the time. But, on the times it is active, hasMoved stays as true after being moved.

At least that's my understanding and the script indeed works, the window only changes once per time I activate the window, not being moved repeatedly. My concern is how sometimes it doesn't work, so do you think this might have something to do with it?

Then if there's an error in the logic, how come it does work all the other times? And how can I fix it to work reliably?
User avatar
boiler
Posts: 17286
Joined: 21 Dec 2014, 02:44

Re: Why does this script sometimes does nothing?

17 May 2024, 21:50

sn1perwild wrote: The hasMoved variable is only set to false when the window is not active. The window is not active all the time. But, on the times it is active, hasMoved stays as true after being moved.
No, that is not correct. You used WinExist, not WinActive, so it will keep moving it as long as the window exists, whether it is active or not.

sn1perwild wrote: At least that's my understanding and the script indeed works, the window only changes once per time I activate the window
Not only is that not correct because you used WinExist instead of WinActive, there is nothing in the logic of your script that would have it happen only once. Even if you used WinActive instead, it would keep moving it. Like I said, it keeps changing the value of the hasMoved variable from true to false and from false to true as long as the window exists (or as long as it's active if you end up changing it to WinActive). You have nothing in your script to make it stop moving it after it moved it once. Just follow what lines would execute and tell me where it would stop moving it and why (which you will find if you do it correctly, it won't stop moving it).

sn1perwild wrote: My concern is how sometimes it doesn't work, so do you think this might have something to do with it?
...
Then if there's an error in the logic, how come it does work all the other times?
Like I said, it could be because you're moving the window every two seconds (even if you're "moving" it in place, you're having Windows perform the move operation), and maybe doing that over and over causes it to lock up somehow.

sn1perwild wrote: And how can I fix it to work reliably?
You could change the logic to actually have it move once per time it's active and perhaps that would do it. But like I said, just changing it from WinExist to WinActive wouldn't accomplish that because it would keep moving it over and over while it's active. This is all that's needed to have it move exactly once each time it's active, then wait patiently for the next time it's active to move it again:

Code: Select all

Menu, Tray, Icon, Icons\shell32_184.ico

loop {
	WinWaitActive, ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe
	WinMove,,, 2821, 153, 1476, 1044
	WinWaitNotActive
}
User avatar
boiler
Posts: 17286
Joined: 21 Dec 2014, 02:44

Re: Why does this script sometimes does nothing?

17 May 2024, 21:55

Note that I edited it above to remove the WinTitle parameters from the WinMove and WinWaitNotActive. They're not needed because they use the LastFoundWindow which was found in the WinWaitActive command.
User avatar
sn1perwild
Posts: 70
Joined: 04 Aug 2021, 15:11

Re: Why does this script sometimes does nothing?

18 May 2024, 08:54

boiler wrote:
17 May 2024, 21:50
sn1perwild wrote: And how can I fix it to work reliably?
You could change the logic to actually have it move once per time it's active and perhaps that would do it. But like I said, just changing it from WinExist to WinActive wouldn't accomplish that because it would keep moving it over and over while it's active. This is all that's needed to have it move exactly once each time it's active, then wait patiently for the next time it's active to move it again:

Code: Select all

Menu, Tray, Icon, Icons\shell32_184.ico

loop {
	WinWaitActive, ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe
	WinMove,,, 2821, 153, 1476, 1044
	WinWaitNotActive
}
That works, thank you for all the info! I was really seeing it all wrong!

However, I noticed that if I move the window and keep the program running in the background (while focusing on other windows), then I get back to it, the window is moved/resized again. That can be a bit disorienting, so that's why I was attempting to make it so it only moves once per time the program launches (in other words, goes from the window not existing → existing).

Does that make sense? Would that be possible?
User avatar
boiler
Posts: 17286
Joined: 21 Dec 2014, 02:44

Re: Why does this script sometimes does nothing?

18 May 2024, 09:34

For that, you would use WinWait and WinWaitClose:

Code: Select all

Menu, Tray, Icon, Icons\shell32_184.ico

loop {
	WinWait, ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe
	WinMove,,, 2821, 153, 1476, 1044
	WinWaitClose
}
User avatar
sn1perwild
Posts: 70
Joined: 04 Aug 2021, 15:11

Re: Why does this script sometimes does nothing?

20 May 2024, 11:43

boiler wrote:
18 May 2024, 09:34
For that, you would use WinWait and WinWaitClose:

Code: Select all

Menu, Tray, Icon, Icons\shell32_184.ico

loop {
	WinWait, ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe
	WinMove,,, 2821, 153, 1476, 1044
	WinWaitClose
}
Thank you for this suggestion!

I tested it for a day and noticed the problem of it sometimes not working also happens here. I noticed it's usually when I start the system, the first time I launch WhatsApp the script seems to not pick it up. I reload the script then it works. Could there be a reson for it and a way to make it work all the time aside from adding a Reload to the loop?
User avatar
boiler
Posts: 17286
Joined: 21 Dec 2014, 02:44

Re: Why does this script sometimes does nothing?

20 May 2024, 12:57

You can try that. If you’re going to do that, then there’s no reason for the loop because the Reload will start it over before it has a chance to loop, so it’s like a built in loop:

Code: Select all

Menu, Tray, Icon, Icons\shell32_184.ico

WinWait, ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe
WinMove,,, 2821, 153, 1476, 1044
WinWaitClose
Reload
User avatar
sn1perwild
Posts: 70
Joined: 04 Aug 2021, 15:11

Re: Why does this script sometimes does nothing?

21 May 2024, 09:32

boiler wrote:
20 May 2024, 12:57
You can try that. If you’re going to do that, then there’s no reason for the loop because the Reload will start it over before it has a chance to loop, so it’s like a built in loop:

Code: Select all

Menu, Tray, Icon, Icons\shell32_184.ico

WinWait, ahk_class ApplicationFrameWindow ahk_exe ApplicationFrameHost.exe
WinMove,,, 2821, 153, 1476, 1044
WinWaitClose
Reload
Yes, that makes sense!

However, I was looking to investigate why it not always works and while this is a valid workaround, I'm also curious about that. Could something else be interfering with the script? It's the same thing that happened with my original version.
User avatar
boiler
Posts: 17286
Joined: 21 Dec 2014, 02:44

Re: Why does this script sometimes does nothing?

21 May 2024, 10:58

I don't know why that would be happening. So you're saying that this Reload version also intermittently fails to move the window after the program closes and you run it again and the window appears again?
User avatar
sn1perwild
Posts: 70
Joined: 04 Aug 2021, 15:11

Re: Why does this script sometimes does nothing?

21 May 2024, 11:27

boiler wrote:
21 May 2024, 10:58
I don't know why that would be happening. So you're saying that this Reload version also intermittently fails to move the window after the program closes and you run it again and the window appears again?
Yes, just now I noticed even the Reload version has this happen :cry:

As an alternative path, I have tried to find a way to get Task Scheduler launch an AutoHotkey script that resizes the window whenever the app is launched. However, since this is a Microsoft Store app, its exe is nonexistent/inaccessible, so I can't get that to work with the structure I found for Task Scheduler.

I'm open to more out-there suggestions, this is just a silly issue that I was hoping could have a more reliable solution.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 248 guests