Duplicate script able to run from different location how to stop it Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Yatendra3192
Posts: 89
Joined: 10 Dec 2017, 06:57

Duplicate script able to run from different location how to stop it

13 Dec 2017, 22:31

Hi All,

I have 100's exact replica of AHK Script on different location (like for eg. D:\Network Drive\30\Timetracker.ahk and D:\Network Drive\31\Timetracker.ahk) when i run script 31 when 30 is already running or if start 30 when 31 is running, they both start running.

I want to exit previous instances running with the name Timetracker.ahk when i run Timetracker.ahk :D
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Duplicate script able to run from different location how to stop it

13 Dec 2017, 22:41

Have you tried adding the following to the top of the scripts?

Code: Select all

#SingeInstance Force
I'm not sure if that applies to the same script running from different locations, but give it a try.

If it doesn't work, you can try adding the following to the top:

Code: Select all

Process, Close, %A_ScriptName%
Yatendra3192
Posts: 89
Joined: 10 Dec 2017, 06:57

Re: Duplicate script able to run from different location how to stop it

13 Dec 2017, 22:55

Hi Osprey

I tried that but this did not resolve the issue but thanks for taking a time out to reply appreciate our help
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Duplicate script able to run from different location how to stop it

13 Dec 2017, 23:49

I just realized that Process, Close works with compiled scripts, but not uncompiled ones. To use it, you'd have to change it to, say, Process, Close, AutoHotkeyU32.exe, or whichever executable you have set to run your scripts.
Yatendra3192
Posts: 89
Joined: 10 Dec 2017, 06:57

Re: Duplicate script able to run from different location how to stop it

13 Dec 2017, 23:58

if i have the same script on different folders and run them they will run separately
what i'm looking for is something that check if script with the same name Timetracker.ahk already running if yes then close that one and start the new one i asked for and it doesn't get effected by different folder location of script
Yatendra3192
Posts: 89
Joined: 10 Dec 2017, 06:57

Re: Duplicate script able to run from different location how to stop it

13 Dec 2017, 23:58

the script i'm running to check this has the same name so isn't it contradict the Process, Close, condition
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Duplicate script able to run from different location how to stop it

14 Dec 2017, 06:17

- I would suspect that something like this script would come in useful at some point.
- You can check the window titles to see if a particular script is open, and use WinClose to close a script.
- The built-in variable, A_ScriptHwnd, might be useful, to compare it with hWnd.

Code: Select all

q:: ;list open AutoHotkey scripts
DetectHiddenWindows, On
WinGet, vWinList, List, ahk_class AutoHotkey
vOutput := ""
Loop, % vWinList
{
	hWnd := vWinList%A_Index%
	WinGetTitle, vWinTitle, % "ahk_id " hWnd
	;WinGetClass, vWinClass, % "ahk_id " hWnd
	;WinGet, vPID, PID, % "ahk_id " hWnd
	vOutput .= hWnd " " vWinTitle "`r`n"
}
MsgBox, % vOutput
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Yatendra3192
Posts: 89
Joined: 10 Dec 2017, 06:57

Re: Duplicate script able to run from different location how to stop it

16 Dec 2017, 00:48

Hi Jeeswg,

It's a nice idea but require to run an additional script , what i'm looking for is something like instant-force who can close the previous same name script even though it's store in different location, and can be embedded into a script it self,
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Duplicate script able to run from different location how to stop it

16 Dec 2017, 04:52

How many instances of Timetracker.ahk run at the same time?
If it's just one, you could do this. Put these lines at the top of the script.

Code: Select all

DetectHiddenWindows, On
SetTitleMatchMode, 2
if hWnd := WinExist("Timetracker.ahk ahk_class AutoHotkey")
	WinClose, % "ahk_id " hWnd
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Yatendra3192
Posts: 89
Joined: 10 Dec 2017, 06:57

Re: Duplicate script able to run from different location how to stop it

16 Dec 2017, 13:14

i just need to run just one, but if it is already running it has to close the previous one.
i tried your code but it's closes itself from running or i think it's doing what we told it to do closing itself.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Duplicate script able to run from different location how to stop it  Topic is solved

16 Dec 2017, 13:28

Oh gosh, you're right. A simple fix to my script above would be to use WinSetTitle twice, to temporarily change its name to something else, but here's a proper script:

Code: Select all

q:: ;close other AHK scripts with same name
DetectHiddenWindows, On
SetTitleMatchMode, 2
WinGet, vWinList, List, \Timetracker.ahk ahk_class AutoHotkey
Loop, % vWinList
{
	hWnd := vWinList%A_Index%
	;WinGetTitle, vWinTitle, % "ahk_id " hWnd
	;WinGetClass, vWinClass, % "ahk_id " hWnd
	;WinGet, vPID, PID, % "ahk_id " hWnd
	if !(hWnd = A_ScriptHwnd)
		WinClose, % "ahk_id " hWnd
}
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Yatendra3192
Posts: 89
Joined: 10 Dec 2017, 06:57

Re: Duplicate script able to run from different location how to stop it

16 Dec 2017, 19:48

Got It! it's working grate. thank you so much for all your help and efforts on solving this jeeswg. greatly appreciated.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ccqcl, Descolada, Google [Bot], inseption86, mebelantikjaya and 342 guests