Search code examples
windows-7autohotkey

Check if ahk (autohotkey) script is running


I am working on an autohotkey script and I wanted to know if you can check if another script is currently running.

I found out that this is what you have to do to close a running script:

DetectHiddenWindows, On
WinClose, %pathToScript% ahk_class AutoHotkey

So I thought this is what you have to do to check if another script is running:

DetectHiddenWindows, On
IfWinActive, %pathToScript% ahk_class AutoHotkey
{
    //code to do when script is active
}

This is not working though. Can you help me ?


Solution

  • [ test this ]

    1 - Create a random script that stays running, yet hidden, like this:

    F::
    Msgbox "running..." ; to test if it's ok or already closed.
    

    2 - Now you create a script to test what you wanna do. My test can close hidden scripts by their names:

    pathToScript = %A_ScriptDir%\teste.exe
    
    DetectHiddenWindows, On
    IfWinExist, %pathToScript%
    {
        WinWait, %pathToScript%,
        IfWinNotActive, %pathToScript%, , WinActivate, %pathToScript%,
        WinWaitActive, %pathToScript%,
    
        WinClose, %pathToScript% ahk_class AutoHotkey
        MsgBox "OK"
    }
    

    Hope it may help you...