3
votes

I have one script that I want to monitor 2 other scripts. Basically, if the monitor script sees notepad open, it will then check to see if script1 is suspended if it is it does nothing if it's not it suspends script1 and un-suspends script2.

       If (WinExist("ahk_class Notepad") AND (script1 A_IsSuspended = 1)){
            Suspend script2
            un-suspend script1
       }

How can I check if the other scripts are suspended?

Is there a way to send an suspend on/off instead of the toggle to a script? This is toggle: PostMessage, 0x111, 65305,,, script1.ahk - AutoHotkey

1

1 Answers

4
votes

The only ways to suspend or unsuspend another script are:

  • By sending a menu command message; i.e. toggling.
  • By implementing some form of inter-process communication (message-passing) and having the other script suspend itself.

However, what you can do is check whether a script is suspended before sending it the toggle message. You can do this by checking whether the Suspend Hotkeys menu item has a checkmark.

ScriptSuspend(ScriptName, SuspendOn)
{
    ; Get the HWND of the script's main window (which is usually hidden).
    dhw := A_DetectHiddenWindows
    DetectHiddenWindows On
    if scriptHWND := WinExist(ScriptName " ahk_class AutoHotkey")    
    {
        ; This constant is defined in the AutoHotkey source code (resource.h):
        static ID_FILE_SUSPEND := 65404

        ; Get the menu bar.
        mainMenu := DllCall("GetMenu", "ptr", scriptHWND)
        ; Get the File menu.
        fileMenu := DllCall("GetSubMenu", "ptr", mainMenu, "int", 0)
        ; Get the state of the menu item.
        state := DllCall("GetMenuState", "ptr", fileMenu, "uint", ID_FILE_SUSPEND, "uint", 0)
        ; Get the checkmark flag.
        isSuspended := state >> 3 & 1
        ; Clean up.
        DllCall("CloseHandle", "ptr", fileMenu)
        DllCall("CloseHandle", "ptr", mainMenu)

        if (!SuspendOn != !isSuspended)
            SendMessage 0x111, ID_FILE_SUSPEND,,, ahk_id %scriptHWND%
        ; Otherwise, it's already in the right state.
    }
    DetectHiddenWindows %dhw%
}

Usage is as follows:

SetTitleMatchMode 2  ; Allow filename instead of full path.
ScriptSuspend("script1.ahk", true)   ; Suspend.
ScriptSuspend("script1.ahk", false)  ; Unsuspend.

You can do the same for Pause by replacing ID_FILE_SUSPEND (65404) with ID_FILE_PAUSE (65403). However, you need to send the WM_ENTERMENULOOP (0x211) and WM_EXITMENULOOP (0x212) messages to the script for the Pause Script checkmark to be updated.