Run a script on restart, coming out of sleep and hibernation
Run a script on restart, coming out of sleep and hibernation
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?
Re: Run a script on restart, coming out of sleep and hibernation
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.
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.
Re: Run a script on restart, coming out of sleep and hibernation
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
Russ
Re: Run a script on restart, coming out of sleep and hibernation
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.gregster wrote: ↑18 Aug 2022, 12:52If 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.
Re: Run a script on restart, coming out of sleep and hibernation
Didn't know that the Widows task manager can be used to run scripts on startup and unlock. Will try that.RussF wrote: ↑18 Aug 2022, 13:31If 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
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?
Re: Run a script on restart, coming out of sleep and hibernation
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
Re: Run a script on restart, coming out of sleep and hibernation
That's great. What about a situation when the laptop hibernates because of the power plan settings?
Re: Run a script on restart, coming out of sleep and hibernation
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
Re: Run a script on restart, coming out of sleep and hibernation
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.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?
Russ