Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Turn all scripts on and off



  • Please log in to reply
21 replies to this topic
andloev
  • Members
  • 1 posts
  • Last active: Jan 20 2013 07:44 PM
  • Joined: 18 Jan 2013

Cant seem to find/create a script that turns all my scripts on when I press a button (home) and then turn it off when i press the same button again. 

 

Hope someone can help. FYI I just started with ahk yesterday

 

Thanks in advance



ccatlett2000
  • Members
  • 2 posts
  • Last active: Jan 19 2013 02:15 PM
  • Joined: 18 Jan 2013

It would do something like this: it would have a variable that indicated wheter the scripts are on or not. Then when the button is pushed, it checks the variable and either kills all the scipts or starts them all. It then flips the variable this has been done. Only problem is you would have to add each scipt by hand. If you need help, the tutorial show how to do this.



iDrug
  • Members
  • 389 posts
  • Last active: Oct 11 2015 09:24 PM
  • Joined: 13 Oct 2009

killing scripts != suspending hotkeys + pausing scripts

I think killing is less preferred.

 

And there is also a way to not add each script by hand: actually you could scan process list, define autohotkey.exe processes and the ahk-files they are bound to and then send commands to those processes.

But it will be quite hard for a complete newbie.



iDrug
  • Members
  • 389 posts
  • Last active: Oct 11 2015 09:24 PM
  • Joined: 13 Oct 2009

Actually, there is no need to write your own bicycle, you can search for "AHKControl by Lexikos" script and it already contains a way to scan memory for running *.ahk files, just use his code.



Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012
✓  Best Answer
Here's something based on Lexikos AHKControl and examples from the Help file.

WM_COMMAND := 0x111
CMD_RELOAD := 65400
CMD_EDIT := 65401
CMD_PAUSE := 65403
CMD_SUSPEND := 65404

DetectHiddenWindows, On

Process, Exist
this_pid := ErrorLevel
control_id := WinExist("ahk_class AutoHotkey ahk_pid " this_pid)

; Press Home to toggle Pause & Suspend state for all scripts.
Home::
WinGet, id, list, ahk_class AutoHotkey
Loop, %id%
{
	this_id := id%A_Index%
    If (this_id <> control_id)
	{
		PostMessage, WM_COMMAND, CMD_PAUSE,,, ahk_id %this_id%
		PostMessage, WM_COMMAND, CMD_SUSPEND,,, ahk_id %this_id%
	}
}
return

Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


iDrug
  • Members
  • 389 posts
  • Last active: Oct 11 2015 09:24 PM
  • Joined: 13 Oct 2009

Pulover, thanks for the neat code, but could you explain why everyone specifies first things like

WM_COMMAND := 0x111
CMD_RELOAD := 65400
CMD_EDIT := 65401
CMD_PAUSE := 65403
CMD_SUSPEND := 65404

? Aren't those codes static? Why not someone write a patch for ahk to make it natively understand commands like "WM_COMMAND", so users won't have to use unreadable "0x1111" instead?



Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

I tested and it works perfectly. However, can this code be modified so that if a script is already paused or suspended, then it is not affected (that is, it is not turned back unpaused or unsuspended in this case and remain in its initial state of pause or suspend) so that this script acts ONLY on unpaused or unsuspended scripts?



Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012
@iDrug: Well maybe if those messages were vital to use AHK they would have been added as constants but that's not the case. Those CMD_ ones specifically are internal references of autohotkey but WM_COMMAND is a windows message and has nothing to do with the AHK program so it seems fair to me that anyone who wishes to use those names as constants for the real values should declare them on their scripts. If you need to use a long list of constants (for example if you want to convert VBA scripts to AHK COM) or need them in different scripts you could read them from an INI file, that's a very good way to assign such variables.

@Guest10: Yes, some time ago Lexikos was kind enough to provide me a method to get a script's suspended and paused states as well. I've created a function with that, this should do what you want or you can adapt to better fit your needs.

WM_COMMAND := 0x111
CMD_RELOAD := 65400
CMD_EDIT := 65401
CMD_PAUSE := 65403
CMD_SUSPEND := 65404

DetectHiddenWindows, On

Process, Exist
this_pid := ErrorLevel
control_id := WinExist("ahk_class AutoHotkey ahk_pid " this_pid)

; Press Home to toggle Pause & Suspend state for all scripts.
Home::
Toggle := !Toggle
WinGet, id, list, ahk_class AutoHotkey
Loop, %id%
{
	this_id := id%A_Index%
	If (this_id <> control_id)
	{
		If (ScriptStatus(this_id, "Paused") = !Toggle)
			PostMessage, WM_COMMAND, CMD_PAUSE,,, ahk_id %this_id%
		If (ScriptStatus(this_id, "Suspended") = !Toggle)
			PostMessage, WM_COMMAND, CMD_SUSPEND,,, ahk_id %this_id%
	}
}
return

ScriptStatus(script_id, cmd)
{
	SendMessage, 0x211,,,, ahk_id %script_id%  ; WM_ENTERMENULOOP
	SendMessage, 0x212,,,, ahk_id %script_id%  ; WM_EXITMENULOOP

	mainMenu := DllCall("GetMenu", "uint", script_id)
	fileMenu := DllCall("GetSubMenu", "uint", mainMenu, "int", 0)
	isPaused := DllCall("GetMenuState", "uint", fileMenu, "uint", 4, "uint", 0x400) >> 3 & 1
	isSuspended := DllCall("GetMenuState", "uint", fileMenu, "uint", 5, "uint", 0x400) >> 3 & 1
	DllCall("CloseHandle", "uint", fileMenu)
	DllCall("CloseHandle", "uint", mainMenu)
	If (cmd = "Paused")
		return isPaused
	If (cmd = "Suspended")
		return isSuspended
}

Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


faqbot
  • Members
  • 997 posts
  • Last active:
  • Joined: 10 Apr 2012
Probably the easiest solution of all: If you add this hotkey to all scripts, pressing the Pause key will Suspend all scripts ~Pause::Suspend

:-)

gummby8
  • Members
  • 120 posts
  • Last active: Nov 30 2018 06:35 PM
  • Joined: 05 Jul 2009
Probably the easiest solution of all: If you add this hotkey to all scripts, pressing the Pause key will Suspend all scripts ~Pause::Suspend

icon_smile.gif

 

If you add 

pause::pause

 

to every script it will only pause and unpause the first script that responds. If you have it suspend, then pressing the button multiple times will eventualy suspend all, but suspending then turns off hotkeys so you would have to manualy unsuspend each one.



Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

Thanks, Pulover, it works flawlessly. Sorry to come back again for anther request. Is it possible for the script to remember the initial state of a script (pause, for example) so that after the toggle, it does not turn it back to unpause but to its previous state of pause (if it was paused before running the toggle script)? Pause -> Suspend -> Pause || Normal -> Suspend -> Normal



faqbot
  • Members
  • 997 posts
  • Last active:
  • Joined: 10 Apr 2012
... to every script it will only pause and unpause the first script that responds. If you have it suspend, then pressing the button multiple times will eventualy suspend all, but suspending then turns off hotkeys so you would have to manualy unsuspend each one.

Not correct, try it, you are forgetting the ~ in front of it "When the hotkey fires, its key's native function will not be blocked (hidden from the system)." so the hotkey is received by all scripts. Don't knock it until you tried it.... :-)

Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012
@Guest10: I'm confused... What you want then is to force the suspend state to the Toggle but toggle the pause state independently? If that's so I think you just need to remove the If Statement for the Pause message, in other words remove this line If (ScriptStatus(this_id, "Paused") = !Toggle)

Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

Perferct, sorry for not being clear. I hardly ever use Suspend. So disabling the line If (ScriptStatus(this_id, "Paused") = !Toggle) is perfect for Pause -> Suspend -> Pause || Normal -> Suspend -> Normal

 

I hardly suspend any scripts but if I did, by disabling the above line, I get Suspend -> Suspend -> Normal. If the previous state was Suspend, how can I keep that state so that I get: Suspend -> Suspend -> Suspend (and not Suspend -> Suspend -> Normal)? Thanks



Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012

Now I'm even more confused... If you remove the statement for Pause it does not affect the behavior for suspend so if the script is suspended it will remain suspended on first execution and unsuspended on the next one.

 

Suspended Scripts: Suspend > Normal > Suspend
Unsuspended Scripts: Suspend > Normal > Suspend
 
Paused Scripts: Normal > Paused > Normal
Unpaused Scripts: Paused > Normal > Paused


Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!