how could I get a script to automatically reload upon waking up my computer?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
someguyinKC
Posts: 68
Joined: 25 Oct 2018, 11:33

how could I get a script to automatically reload upon waking up my computer?

Post by someguyinKC » 31 Mar 2024, 07:35

I have a script that I have running on four different computers. I often tweak the script on one computer and then later, say when I go home and use another computer, I need to click "reload" to get the latest version. (I'm using google drive to sync them across the computers.)

I have a run at startup program that is working well, but is there a way to get a script to "run at wakeup", so to speak? so I can have a computer asleep and when I wake it up and log in, it will reload the script to get the newest version?
thank you,

Someguy

lexikos
Posts: 9665
Joined: 30 Sep 2013, 04:07
Contact:

Re: how could I get a script to automatically reload upon waking up my computer?

Post by lexikos » 31 Mar 2024, 20:05

I use

Code: Select all

OnMessage(WM_POWERBROADCAST := 0x218, OnPowerBroadcast)
OnPowerBroadcast(wParam, *) {
    if (wParam = 7) { ; PBT_APMRESUMESUSPEND: resume triggered by user input
        ;...
    }
}
but be aware that your network adapter might not be ready at that time. You would probably also need to wait for Google Drive to sync the file.

You might want to use wParam = 18 (PBT_APMRESUMEAUTOMATIC) instead.

someguyinKC
Posts: 68
Joined: 25 Oct 2018, 11:33

Re: how could I get a script to automatically reload upon waking up my computer?

Post by someguyinKC » 05 Apr 2024, 07:18

@Lexicos
I tried this script by adding it to a running script.

Code: Select all

OnMessage(WM_POWERBROADCAST := 0x218, OnPowerBroadcast)
OnPowerBroadcast(wParam, *) {
    if (wParam = 7) { ; 7 is for "system is resuming from a suspended state triggered by user input"
        Reload ;...
	}
}
I saved the script and reloaded it manually. I then put my computer to sleep and waited awhile and awakened it. it did not reload the script like I wanted it to.

any suggestions?
thank you,

Someguy

vmech
Posts: 361
Joined: 25 Aug 2019, 13:03

Re: how could I get a script to automatically reload upon waking up my computer?

Post by vmech » 05 Apr 2024, 10:55

someguyinKC wrote:
05 Apr 2024, 07:18
any suggestions?
Just try to use debugging, like as:

Code: Select all

OnMessage(WM_POWERBROADCAST := 0x218, OnPowerBroadcast)
OnPowerBroadcast(wParam, *) {
    MsgBox wParam ; For debugging purpose
    if (wParam = 7) { ; 7 is for "system is resuming from a suspended state triggered by user input"
        Reload ;...
	}
}
Also, I think it would be a good idea to check both types of wakeup messages:

Code: Select all

if (wParam = 18 or wParam = 7)
:arrow: https://learn.microsoft.com/en-us/windows/win32/power/wm-powerbroadcast

Code: Select all

/**
 * PBT_APMRESUMEAUTOMATIC = 18 (0x12)
 * Operation is resuming automatically from a low-power state. This message is sent every time the system resumes.
 * 
 * PBT_APMRESUMESUSPEND = 7 (0x7)
 * Operation is resuming from a low-power state. This message is sent after PBT_APMRESUMEAUTOMATIC if the resume is triggered by user input, such as pressing a key.
 */
It is also of some concern that message processing in the application must return True to the operating system. But using the Reload function in a script makes this condition difficult to satisfy.
:arrow: https://learn.microsoft.com/en-us/windows/win32/power/wm-powerbroadcast#return-value
Please post your script code inside [code] ... [/code] block. Thank you.

lexikos
Posts: 9665
Joined: 30 Sep 2013, 04:07
Contact:

Re: how could I get a script to automatically reload upon waking up my computer?

Post by lexikos » 11 Apr 2024, 19:31

vmech wrote:
05 Apr 2024, 10:55
It is also of some concern that message processing in the application must return True to the operating system.
"An application should return TRUE if it processes this message."

It doesn't specify what constitutes "processing", or that it should ever return FALSE, or that the return value is used in any way. One way of looking at it is that in order to return any value, the application would need to process the message. Therefore, the application should always return TRUE or not process the message. It really has no meaning at all. If we suppose that the default processing of the message returns FALSE and that the sender assumes a return value of 0 if the application terminates before replying, terminating would be the same as not processing the message. Processing the message is not mandatory, so there is no need for concern.

However, if you did need to reply to a message before terminating, you could generally just call Reload and then immediately return. If the new process somehow starts and terminates the old process faster than it can return (seems unlikely), it could perhaps use ReplyMessage prior to Reload.

Liam_Lucas
Posts: 1
Joined: 14 Apr 2024, 06:34

Re: how could I get a script to automatically reload upon waking up my computer?

Post by Liam_Lucas » 14 Apr 2024, 22:54

someguyinKC wrote:
31 Mar 2024, 07:35
I have a script that I have running on four different computers. I often tweak the script on one computer and then later, say when I go home and use another computer, I need to click "reload" to get the latest version. (I'm using google drive to sync them across the computers.)

I have a run at startup program that is working well, but is there a way to get a script to "run at wakeup", so to speak? so I can have a computer asleep and when I wake it up and log in, it will reload the script to get the newest version?
You can achieve this by scheduling a task in the Task Scheduler to run your script on system wake-up. This way, whenever you wake up the computer and log in, the script will automatically reload to get the latest version.

User avatar
xMaxrayx
Posts: 214
Joined: 06 Dec 2022, 02:56
Contact:

Re: how could I get a script to automatically reload upon waking up my computer?

Post by xMaxrayx » 15 Apr 2024, 04:47

use "task scheduler" and launch an another custom reload script.

I did project with task scheduler to stop steam downloading If i'm connected to my pocket wifi.
https://github.com/xmaxrayx/steam-auto-stop-downloading
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/

Post Reply

Return to “Ask for Help (v2)”