Run a script on restart, coming out of sleep and hibernation

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mmjoshi
Posts: 4
Joined: 18 Aug 2022, 12:34

Run a script on restart, coming out of sleep and hibernation

Post by mmjoshi » 18 Aug 2022, 12:44

I have downloaded the portable version of AHK from PortableApps.com, and am a total newbie to AHK and being a non programmer have no knowledge of writing scripts. My requirement is to run a batch file (.bat) when the computer restarts or comes out of sleep or hibernation. Is it possible to do this with AHK? How?

gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Run a script on restart, coming out of sleep and hibernation

Post by gregster » 18 Aug 2022, 12:52

If I remember correctly, Portable Apps recently still distributed a rather old version of AHK. I would use instead the always up-to-date zip-version from our Downloads page: https://www.autohotkey.com/download/ ("Download AutoHotkey .zip")
Anyway, Portable Apps doesn't offer an official download; I don't see any reason to download from there. At best, you are getting the same as here.

On computer start you could use this: https://www.autohotkey.com/docs/FAQ.htm#Startup (if you are not installing AHK, you could compile your script to an exe-file).
or Windows' task scheduler.

For detecting sleep and hibernation, there are probably other solutions. Others here might know.

As a beginner, I would recommend to work through the tutorial, the FAQ and to read the 'Usage and Syntax' section of the docs.

RussF
Posts: 1264
Joined: 05 Aug 2021, 06:36

Re: Run a script on restart, coming out of sleep and hibernation

Post by RussF » 18 Aug 2022, 13:31

If you use Task Manager to create a task, you can use multiple triggers to run your script - on startup and on unlock (assuming your device locks when sleeping/hibernating). I didn't find anything specific to sleep or hibernation, but most systems present an unlock (sign-in) screen when coming out.

Russ

mmjoshi
Posts: 4
Joined: 18 Aug 2022, 12:34

Re: Run a script on restart, coming out of sleep and hibernation

Post by mmjoshi » 18 Aug 2022, 17:32

gregster wrote:
18 Aug 2022, 12:52
If I remember correctly, Portable Apps recently still distributed a rather old version of AHK. I would use instead the always up-to-date zip-version from our Downloads page: https://www.autohotkey.com/download/ ("Download AutoHotkey .zip")
Anyway, Portable Apps doesn't offer an official download; I don't see any reason to download from there. At best, you are getting the same as here.

On computer start you could use this: https://www.autohotkey.com/docs/FAQ.htm#Startup (if you are not installing AHK, you could compile your script to an exe-file).
or Windows' task scheduler.

For detecting sleep and hibernation, there are probably other solutions. Others here might know.

As a beginner, I would recommend to work through the tutorial, the FAQ and to read the 'Usage and Syntax' section of the docs.
Good point. Will download the zip versi0n from the AHK site. Also will go through the tutorial to learn more about how to use AHK. Thanks.

mmjoshi
Posts: 4
Joined: 18 Aug 2022, 12:34

Re: Run a script on restart, coming out of sleep and hibernation

Post by mmjoshi » 18 Aug 2022, 17:35

RussF wrote:
18 Aug 2022, 13:31
If you use Task Manager to create a task, you can use multiple triggers to run your script - on startup and on unlock (assuming your device locks when sleeping/hibernating). I didn't find anything specific to sleep or hibernation, but most systems present an unlock (sign-in) screen when coming out.

Russ
Didn't know that the Widows task manager can be used to run scripts on startup and unlock. Will try that.

However, my preference will be to use a method independent of Windows so that I can just copy the script file to multiple computers without the need to set up the task feature of Windows Task Manager. Is that at all possible?

User avatar
rommmcek
Posts: 1474
Joined: 15 Aug 2014, 15:18

Re: Run a script on restart, coming out of sleep and hibernation

Post by rommmcek » 18 Aug 2022, 19:00

If you use Ahk to get into Sleep/Hybernation, then it is easy to run anything after awake:

Code: Select all

F1::
    SoundBeep 900
    SoundBeep 300
    DllCall("powrprof\SetSuspendState", UChar,0, UChar,0, UChar,0, UChar) ;Sleep/Suspend
    SoundBeep 300
    SoundBeep 900
    MsgBox Hello World, I'm awake from Sleep!
Return

F2::
    SoundBeep 900
    SoundBeep 300
    DllCall("powrprof\SetSuspendState", UChar,1, UChar,0, UChar,0, UChar) ;Hibernate
    Sleep, 500
    SoundBeep 300
    SoundBeep 900
    MsgBox Hello World, I'm awake from Hybernation!
Return

Esc:: ExitApp

mmjoshi
Posts: 4
Joined: 18 Aug 2022, 12:34

Re: Run a script on restart, coming out of sleep and hibernation

Post by mmjoshi » 18 Aug 2022, 19:57

That's great. What about a situation when the laptop hibernates because of the power plan settings?

User avatar
rommmcek
Posts: 1474
Joined: 15 Aug 2014, 15:18

Re: Run a script on restart, coming out of sleep and hibernation

Post by rommmcek » 18 Aug 2022, 23:29

Disable OS settings for Sleep/Hibernation and use Ahk instead:

Code: Select all

#Persistent
SetTimer aTimeIdle, 500
Return

aTimeIdle:
    if (A_TimeIdle > 3000) {
        SetTimer aTimeIdle, Off ; for testing
        MsgBox  Time to Sleep
    }
Return
Esc:: ExitApp

RussF
Posts: 1264
Joined: 05 Aug 2021, 06:36

Re: Run a script on restart, coming out of sleep and hibernation

Post by RussF » 19 Aug 2022, 05:34

mmjoshi wrote: However, my preference will be to use a method independent of Windows so that I can just copy the script file to multiple computers without the need to set up the task feature of Windows Task Manager. Is that at all possible?
Search the web for "import export task scheduler tasks". Tasks can be migrated to another PC. I'm sure that someone with a little more time and knowledge than I could set up some AHK scripts to automate that process and create an "installation" script.

Russ

Post Reply

Return to “Ask for Help (v1)”