This thread is a continuation of this thread: 1/3 How to Check opened PID With Timer and Array?
I can't even formulate my problem clearly enough for you to understand what I need to solve . . .
Simply put:
Modified shortcuts includes the script in their path.
On shortcut opening (double clicking on any modified ones)
The script launch itself and launches what the unmodified normal shortcut should have launched, call it "A".
And additionally check "A" PID until it is closed.
I did it with process waitclose, which works only for one process or file, then I did it with a SetTimer and Array() and Hotkey.
it works, but it was just to test it.
I need it to work with as described above "Modified shortcuts includes the script in their path.
On shortcut opening (double clicking on any modified ones) . . ."
This works for one process or file.
But when the script is launched, trying to launch additional modified shortcut doesn't works.
Here what I could code:
1- with hotkey: working but it is not what I need: I need it without hotkey to launch it, but with shortcut like I show below in code # 2.
!a::
FileSelectFile, OutputVar
Run, %OutputVar%,,,OutputVarPID
if !ProcOnDuty
ProcOnDuty:=Array()
ProcOnDuty.insert(OutputVarPID)
OutputVarPID=
MaxIndex := ProcOnDuty.MaxIndex()
SetTimer, ReCheck
Return
ReCheck:
For index, value in ProcOnDuty
{
Process, Exist, %value%
{
if !errorlevel
{
ProcOnDuty.Remove(index)
MaxIndex := ProcOnDuty.MaxIndex()
If (MaxIndex>0)
MsgBox % "Value: " value " MaxIndex: " MaxIndex " ProcOnDuty.MaxIndex(): " ProcOnDuty.MaxIndex()
; Do whatever you want to do here if you need it
Else
{
SetTimer, ReCheck, Off
;MsgBox,no more exec on duty MaxIndex: %MaxIndex%
; Do whatever you want to do here if you need it
ExitApp
}
}
}
}
return
return
2- a poor attempt to make it working from double click on modifieds shortcuts. This is what I need working.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance ignore
#Persistent
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
DetectHiddenWindows, On
GoSub, Create_files_and_shortcuts
return
Create_files_and_shortcuts:
ShortcutArg=%1%
File1=%A_ScriptDir%\12345.txt
Shortcut1=%A_ScriptDir%\12345.txt.lnk
File2=%A_ScriptDir%\678910.txt
Shortcut2=%A_ScriptDir%\678910.txt.lnk
File3=%A_ScriptDir%\1a2b3c.txt
Shortcut3=%A_ScriptDir%\1a2b3c.txt.lnk
if (ShortcutArg) {
GoSub, OpenFiles
} else {
if (FileExist(File1)) {
} else {
;------ 1- Files & shortcut creation - Start - ------
Msgbox,
(
This script will create 3 empty txt files and their modified shortcuts.
When opening them by double clicking on shortcuts, the script is supposed to check opened Process ID (PID).
According to your action on closing or reopening the files via the shortcuts the script will increase and decrease the number of opened files.
And finally the script close when the 3 files will be closed.
)
FileAppend,, %File1%
}
if (FileExist(Shortcut1)) {
} else {
FileCreateShortcut, %A_AhkPath%, %Shortcut1% , %A_ScriptDir%, "%A_ScriptFullPath%" "%A_ScriptDir%\12345.txt", Modified 12345.txt Shortcut
}
if (FileExist(File2)) {
} else {
FileAppend,, %File2%
}
if (FileExist(Shortcut2)) {
} else {
FileCreateShortcut, %A_AhkPath%, %Shortcut2% , %A_ScriptDir%, "%A_ScriptFullPath%" "%A_ScriptDir%\678910.txt", Modified 678910.txt Shortcut
}
if (FileExist(File3)) {
} else {
FileAppend,, %File3%
}
if (FileExist(Shortcut3)) {
} else {
FileCreateShortcut, %A_AhkPath%, %Shortcut3% , %A_ScriptDir%, "%A_ScriptFullPath%" "%A_ScriptDir%\1a2b3c.txt", Modified 1a2b3c.txt Shortcut
}
ExitApp
}
return
;------ 1- Files & shortcut creation - End - ------
;------ 2- Double click on Shortcut to open files - Start - ------
OpenFiles:
;MsgBox, ShortcutArg is NOT empty=%ShortcutArg%
if (ShortcutArg=Shortcut1 || ShortcutArg= Shortcut2 || ShortcutArg=Shortcut3) {
;MsgBox, ShortcutArg=%ShortcutArg%
Run, %ShortcutArg%,,, OutputVarPID
GoSub, ProcOnDuty
} else {
;MsgBox, ShortcutArg=%ShortcutArg%
}
if (ShortcutArg= File1 || ShortcutArg= File2 || ShortcutArg=File3) {
;MsgBox, YES ShortcutArg=%ShortcutArg%
Run, %ShortcutArg%,,, OutputVarPID
GoSub, ProcOnDuty
} else {
;MsgBox, ShortcutArg=%ShortcutArg%
}
return
;------ 2- Double click on Shortcut to open files - End - ------
;------ 3- Array that check opened PIDs with a timer - Start - ------
ProcOnDuty:
if (!ProcOnDuty) {
ProcOnDuty:=Array()
} else {
}
ProcOnDuty.insert(OutputVarPID)
MaxIndex := ProcOnDuty.MaxIndex()
SetTimer, ReCheck
return
;------ 3- Array that check opened PIDs with a timer - End - ------
;------ 4- Timer - Start - ------
ReCheck:
For index, value in ProcOnDuty
Process, Exist, %value%
if (!errorlevel) {
ProcOnDuty.Remove(index)
MaxIndex := ProcOnDuty.MaxIndex()
if (MaxIndex>0) {
MsgBox % "Value: " value " MaxIndex: " MaxIndex " ProcOnDuty.MaxIndex(): " ProcOnDuty.MaxIndex()
} else {
SetTimer, ReCheck, Off
ExitApp
}
} else {
}
return
;------ 4- Timer - End - ------
I suppose it has to do with thread problem, but I cannot find how to do it correctly, although I tried and tried.
Maybe an example could help me a lot or indications on how to solve it.
Pseudo code could be: Open a file with a shortcut
check if the file is opened
open other file(s)
check all opened files
when one file close don't check it anymore
same for each file
when no more file to check, close the script
Although the script above is working, I need to continue as it is not the solution I need, so I decided to make a continuation thread here: 3/3 How can I do this?: