How to run multiple instances of this script? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
need4speed
Posts: 143
Joined: 22 Apr 2016, 06:50

How to run multiple instances of this script?  Topic is solved

19 Jun 2019, 19:24

I put a simple script together. The script works as expected, but for some reason when running a second instance, the second script gets closed, pressing "ESC" brings up an error message, probably caused by "pid".

So how to fix the script? Also if I made more mistakes or if somethings should be done in another way, plz feel free to comment. Cheers.

Code: Select all

#SingleInstance, Off ; Allow multiple instances
#Persistent
#Warn

mpc_filepath:="D:\mpc-be64\mpc-be64.exe"
mpc_args:="/play /randomize /new M:\Movies /new V:\Videos"
 
Run, %mpc_filepath% " . " %mpc_args%,,UseErrorLevel,pid
if (ErrorLevel)
    MsgBox,0x10, Error, Video Player could not be launched.

SetTimer, CheckPlayerRunning, 200

Return ;------------------------------ auto-exec region -----------------------------------------------------

#IfWinActive, ahk_exe mpc-be64.exe
    ~MButton::
        while (GetKeyState("MButton", "P")) {
            SendInput, {Right}
            Sleep, 50
        }
    return
#IfWinActive

~ESC::  ; Close Process & Exit Script
	Loop
	{
		Process, Exist, mpc-be64.exe
			if ErrorLevel
				Process, Close, mpc-be64.exe
			else
                ExitApp
	}	
return

; Activate Window
CheckPlayerRunning:
Process, Exist, mpc-be64.exe
If !ErrorLevel
    ExitApp
else
{
MouseGetPos, , , id, control
WinGetClass, class, ahk_id %id%
    if (class = "MPC-BE")
        WinActivate, ahk_id %id%    
}
return
edit: fixed, script should work now
Last edited by need4speed on 20 Jun 2019, 16:07, edited 2 times in total.
gregster
Posts: 9021
Joined: 30 Sep 2013, 06:48

Re: How to run multiple instances of this script?

19 Jun 2019, 22:38

There might be other problems , but this line stands out as wrong:

Code: Select all

if WinExist (ahk_pid %pid%)
First, there is no space allowed between function name and its parameter list (in parentheses). Also, the syntax of the WinTitle parameter is wrong. Try this:

Code: Select all

if WinExist("ahk_pid " . pid)
need4speed
Posts: 143
Joined: 22 Apr 2016, 06:50

Re: How to run multiple instances of this script?

20 Jun 2019, 06:23

Thanks gregster, I've fixed that, though my problem still persists. I wish AHK was more stringent/verbose regarding my sloppiness.

edit: finally fixed the script, works now as expected.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Google Adsense [Bot], peter_ahk, Ragnar, septrinus, yuu453 and 288 guests