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

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.
}
}

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.

Issue with an Autohotkey Script running in Windows 10

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.

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

Autohotkey : skip comment lines in Loop / FileReadLine

i read a config.ini with a location of some .exe to run, but i want to skip a comment line from my config.ini (cause i want to explain how to use the file), if someone can help me thanks
#NoEnv
#SingleInstance force
Loop
{
FileReadLine, exe, config.ini, %A_Index%
if ErrorLevel
break
Run %exe%
Sleep , 300
}
return
ExitApp
config.ini
// Put here location of your programs << line to skip
// Thanks << line to skip
C:\WINDOWS\notepad.exe
C:\WINDOWS\****.exe
...
Loop
{
FileReadLine, exe, config.ini, %A_Index%
if ErrorLevel
break
else if SubStr(exe,1,2)=="//"
continue
Run %exe%
Sleep , 300
}
Using SubStr() check if first two chars in line are // if so use continue to skip rest of loop and start next iteration.