Issue with an Autohotkey Script running in Windows 10 - autohotkey

The following script doesn't work properly in Windows 10. The force shutdown screen doesn't go away (which is supposed to happen so the script can continue). I tried using ESC instead of ENTER along with adding two TABs before the ENTER (neither of those methods worked). I am using it to backup the RAM Disk (made with the driver I mentioned in my previous question) before shutting down/rebooting the OS:
#NoEnv
#Persistent
SendMode Input
SetWorkingDir %A_ScriptDir%
SetTimer, RunBeforeShutdown, Off
Gui,+LastFound
hwnd:=WinExist()
DllCall("ShutdownBlockReasonCreate","Uint",hwnd,"Str","")
DllCall("kernel32.dll\SetProcessShutdownParameters", UInt, 0x4FF, UInt, 0)
;puts us first in line for getting the shutdown call, i guess?
OnMessage(0x11, "WM_QUERYENDSESSION")
Return
WM_QUERYENDSESSION(wParam, lParam)
{
ENDSESSION_Logoff = 2147483648
If (lParam == ENDSESSION_Logoff) {
global EventType = "Logoff"
} Else {
global EventType = "Shutdown"
;no way to distinguish between shutdown and restart
}
SetTimer, RunBeforeShutdown, On
Return false
}
runBeforeShutdown:
SetTimer, RunBeforeShutdown, Off
Sleep, 1000
SendInput, {ENTER} ; gets us past the 'Force shudown' screen
Sleep, 1000
#SingleInstance, Force
DllCall("ShutdownBlockReasonDestroy","Uint",hwnd)
; **** Your commands go here ****
RunWait cmd.exe /c backup.bat
; ********
If (EventType == "Logoff") {
Shutdown, 0
} Else {
Shutdown, 1
}
Reload
Return
EDIT: I ended up making a Scheduled Task that runs Drive Snapshot every night with parameters to back up the drive to a drive image.

Related

It maybe seem unnecessary, but I want to reopen a spezific program after it is closed by ahk

This is how far i got:
#IfWinActive ahk_exe zotero.exe
f7::
WinGetTitle, Title, A
WinClose, A
Sleep 1000
IfWinNotExist(zotero)
{
Run, zotero.exe , , Min
}
Return
I am unsure about the if statement in an if statement. How can I make it work?
I would suggest to use:
Loop {
Sleep, 1000 ; just 1 second delay between any check
process, Exist, "Zotero.exe"
if ErrorLevel
Run, zotero.exe , , Min
}

while loop not executing correctly when toggled on/off

Goals:
Run ahk script so that a window stays active. When the user clicks off the window it immediately becomes active again.
This is so an overlay (considered its own window) can be used in a game and that if the overlay is clicked on by accident the game window will become the active window again.
I would also like this to be able to be turned on and off during game play so that the user can alt+tab if necessary.
Problems:
I'm testing my code implementation, so far i have it set up to make a blank notepad file become the active window and stay active.
The problem is the toggle (ctrl+alt+J). I can toggle the code it off just fine but when i toggle it on the window doesn't become active again.
Code:
stop := 0
; 0 = off, 1 = on
while (stop = 0)
{
IfWinNotActive, Untitled - Notepad
{
WinActivate, Untitled - Notepad
}
}
return
^!j::
stop := !stop
if (stop = 0){
MsgBox, stop is off.
}
else{
MsgBox, stop is on.
}
return
The reason it doesn't work after you toggle it off is that While only runs until is evaluates to false. Even if what it would evaluate later becomes true again, it won't restart.
Here's what you can do to make your current code work:
stop := 0
; 0 = off, 1 = on
labelWinCheck: ; label for GoSub to restart while-loop
while (stop = 0)
{
IfWinNotActive, Untitled - Notepad
{
WinActivate, Untitled - Notepad
}
Sleep , 250 ; added sleep (250 ms) so CPU isn't running full blast
}
return
^!j::
stop := !stop
if (stop = 0){
MsgBox, stop is off.
} else {
MsgBox, stop is on.
}
GoSub , labelWinCheck ; restarts while-loop
return
There are a couple of different ways that I would look at to achieve your goal.
Easy: use SetTimer instead of While.
stop := 0
SetTimer , labelWinCheck , 250 ; Repeats every 250 ms
labelWinCheck:
If !WinActive( "Untitled - Notepad" )
WinActivate , Untitled - Notepad
Return
^!j::
SetTimer , labelWinCheck , % ( stop := !stop ) ? "off" : "on"
MsgBox , % "stop is " . ( stop ? "on" : "off" )
Return
Advanced: us OnMessage() to monitor WinActivate events. I don't have a working example of this as that would take a bit of research for me, but here is a link for a solution I made to monitor keyboard events, Log multiple Keys with AutoHotkey. The links at the bottom may especially prove useful.

AHK blocked input

So I am making my script
toggl:=false
while(1){
if(toggl){
Send,E
}
Sleep, 150
}
!r::
toggl:=!toggl
return
!y::ExitApp,101
Problem is that while the loop is running, I can not cancel it because it blocks !y, so I had to restart computer. So any help with this would be nice.
Set #MaxThreads 2 to allow each hotkey to run twice, OR:
Use SetTimer to allow the hotkey to end and continue your loop in a "Pseudo-Thread".
toggle := 0
F12::
toggle := !toggle
if (toggle){
SetTimer, DoLoop, -100
}
return
DoLoop:
Loop {
if (!toggle){
break
}
; [Do your stuff here]
}
return

How to detect whether a process or application is running and terminate the program if it isn't

So I am attempting to determine whether the process MultiMC.exe is running or not and if it isn't I want to end my script.
IfExist, Process MultiMC.exe
ExitApp, 0
Else
ExitApp 1
I've tried this but it isn't working.
Try the following "Process,Exist, act.exe", lifted from http://www.autohotkey.com/board/topic/50026-if-process-not-running-run-it/
I only show this so you see how the "Process,Exist, act.exe" is used in another script.
#persistent
SetTitleMatchMode,2
Loop
{
Process,Exist, act.exe ; Sets errorlevel to process PID
IfWinNotExist, % "ahk_pid " errorlevel ; Expression for ahk_pid
{ ; Block to do something.
Run, C:\Documents and Settings\Pat\Desktop\act.exe
Break ; Stops loop if run or it will continue forever.
}
}

Autohotkey loop not working

My loop in my autohotkey script is only running through once. Can anyone tell me why? Thanks
Loop, 8
{
WinActivate, NDTr
ControlClick, Button3 ;Select Batch, enter info, start collecting data
WinWait, Batch Readings
ControlClick, Edit1
Send {BS}+{BS}+{BS}+{BS}+{BS}+{BS}
Send 1
ControlClick, Edit2
Send {BS}+{BS}+{BS}+{BS}+{BS}+{BS}
Send 15
if A_Index = 4
{
Sleep, 20000
}
else if A_Index = 7
{
Sleep, 20000
}
else if A_Index = 1
{
Sleep, 3000
}
else
{
Sleep, 15000
}
ControlClick, Button1
Sleep, 15000
}
WinWait looks like a likely culprit like anthv123 said. Double check your window's title and make sure it fits the TitleMatchMode that you're expecting.
Common debugging practices include adding different ToolTips in places along the problem code. For example tooltips above and below the WinWait line with texts "before" and "after" would tell you if it's pausing indefinitely at that part (if it never says "after").
Sleeping for 3-20 seconds isn't going to help your patience either.
Try using this to diagnose the issue. If "Batch Readings" takes longer than 5 seconds, you get an error letting you know and the loop continues
WinWait, Batch Readings,,5
if (errorLevel = 1)
Msgbox % "Batch Readings timed out"