Need help running scripts if they're not already running. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
CryptidScripts
Posts: 2
Joined: 25 May 2022, 23:43

Need help running scripts if they're not already running.

Post by CryptidScripts » 25 May 2022, 23:57

Hello!

I am trying to make a script in my startup folder that checks if scripts are running or not, then starts them if they are not running, I have checked the boards and gotten to the point where the script DOES start the others, but it doesn't check, it just starts everything, prompting me to have to dismiss a bunch of "An older version of this script is already running" messages. Can anyone help me?

Code: Select all

Run, cmd.exe /c "If exist C:\AHK\list.dat del C:\AHK\List.dat",,Hide
Run, cmd.exe /c "dir C:\AHK /B *.AHK > C:\AHK\List.dat",,Hide
    SetTitleMatchMode, 2
    DetectHiddenWindows, On

Loop, Read, C:\AHK\list.dat
{
	Loop, Parse, A_LoopReadLine, %A_Tab%
	{
		IfWinNotExist, A_LoopField
		{
		Run, C:\AHK\%A_LoopField%
		} else {
		Return
		}
	}
}
The intent is to make a script that I expect to be reloaded frequently, so every time it's reloaded, it runs a command to check for a list, delete if found, then generate the list anew so that the list is up to date with any scripts in that folder.

Then it reads that list and for each item in the list, check if the script is running, if not, run it, if so, do nothing.

what actually happens is that it does the list correctly, though the list contains the control script despite it not being in that folder, then it doesn't bother checking if a script is running or not, it just runs everything in that list... and the list itself? I'm in version 1.1.33.10 and do not have the ability to update due to network policies at work.

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Need help running scripts if they're not already running.  Topic is solved

Post by Rohwedder » 26 May 2022, 01:48

Hallo,
without knowing your list.dat the easiest way is to insert #SingleInstance, Force in each script.
Regarding the full title of the hidden script window:

Code: Select all

#SingleInstance, Force
Titel := A_ScriptFullPath
if !A_IsCompiled
    Titel .= " - AutoHotkey v" A_AhkVersion
else if SubStr(A_LineFile, 1, 1) = "*" && A_LineFile != "*#1"
    Titel .= " - " A_LineFile
DetectHiddenWindows On
WinGetTitle, Title, ahk_id %A_ScriptHwnd%
MsgBox,% "The title of my hidden window is`nCalculated:`n" Titel "`nRetrieved:`n" Title

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Need help running scripts if they're not already running.

Post by BoBo » 26 May 2022, 03:21

Kinda similar approach. It executes a document/script if it's not already running, but idles if it's already executed. HTH
viewtopic.php?f=10&p=463740#p463740

CryptidScripts
Posts: 2
Joined: 25 May 2022, 23:43

Re: Need help running scripts if they're not already running.

Post by CryptidScripts » 26 May 2022, 23:32

Rohwedder wrote:
26 May 2022, 01:48
Hallo,
without knowing your list.dat the easiest way is to insert #SingleInstance, Force in each script.
So the list is just a straight output from the DIR /B command, like

script1.AHK
script2.AHK
etc

However Your initial suggestion here is what i ended up going with, Why bother with having it check if a script is running if I can make that fact irrelevant with less code! I appreciate the insight and help.

Post Reply

Return to “Ask for Help (v1)”