on every loop it checks its recording by reading the obs log file if it isn't - it starts. The problem is that it runs fine with the first instance of obs, but when it is closed and reopened, the RunWait line doesn't really wait for the new instance to load and goes directly to this - If (process_PID != ErrorLevel) - which screws up the next if - it executes the hotkey before the program is loaded.
Any ideas why it ignores the Wait in RunWait ?
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#persistent
SetTitleMatchMode,2
/*
Summary
The script runs indefinetely every N seconds
Checks if obs is running if not, runs it
Starts recording
Checks the latest RECORDING line in
the latest log created by obs
If said line contains STOP
Starts recording
*/
LoopEveryThisSeconds = 10000 ; set the intervals the script should run
RecordHotKey = F9 ; set configured hotkey from obs here
ObsBit = 32 ; set bit version here 32/64
ObsPath = D:\Programs\obs-studio\bin\%ObsBit%bit ; set your path to obs executable here
LogPath = %A_AppData%\obs-studio\logs
line_regex = Recording\s(\w+)
InitialRun := false
; MsgBox %LogPath% ; debug path
SetTimer, obs, %LoopEveryThisSeconds%
obs:
; runs obs if not already running
Process,Exist, obs%ObsBit%.exe ; Sets errorlevel to process PID
IfWinNotExist, % "ahk_pid " errorlevel ; Expression for ahk_pid
{
RunWait, %ObsPath%\obs%ObsBit%.exe, , , ErrorLevel
}
If (process_PID != ErrorLevel)
{
InitialRun := false
}
; gets the process PID
process_PID = %ErrorLevel%
; finds the latest log file
Loop %LogPath%\*.*, 0, 1
{
MostRecentLog := (A_Index = 1 ? A_LoopFileFullPath : MostRecentLog)
FileGetTime MostRecentLogTime, %MostRecentLog%, C
MostRecentLog := (A_LoopFileTimeModified > MostRecentLogTime ? A_LoopFileFullPath : MostRecentLog)
}
; MsgBox %MostRecentLog% debug latest log path
; reads every line from the latest log
Loop, read, %MostRecentLog%
{
RegExMatch(A_LoopReadLine, line_regex, line)
; line1 holds the captured group
}
; MsgBox %line1% ; debug regex match\
If (line1 == "Stop" || !InitialRun)
{
InitialRun := true
ControlSend, ,{%RecordHotKey%},ahk_pid %process_PID%
}