Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Executable Wrapper Template


  • Please log in to reply
No replies to this topic
mtyacorn
  • New members
  • 1 posts
  • Last active: Sep 19 2015 06:43 AM
  • Joined: 16 Sep 2015

I've been using this general process to write scripts that will launch an executable (if it isn't already running) and keep the script running only while the executable is still running. I've mostly used this to create hotkeys for specific games. Here's a link to a pastebin copy of the code: http://pastebin.com/XJ0iHKVw.

#SingleInstance force
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

EXECUTABLE_PATH=
EXECUTABLE_NAME=
EXECUTABLE_TIMEOUTMSG=

; Config file is the script name with .ini instead of .ahk or .exe
SplitPath, A_ScriptName,,,, ConfigFile
ConfigFile := ConfigFile . ".ini"
gConfigIsNew := false
if (!FileExist(ConfigFile))
	gConfigIsNew := true

IniReadWrite(file, category, key, defaultValue) {
	IniRead, output, %file%, %category%, %key%
	if (output = "ERROR" or !output) {
			IniWrite, %defaultValue%, %file%, %category%, %key%
			output := defaultValue
	}
	return output
}

;---------------------------------------------;
; Read config options from the INI file here. ;
;---------------------------------------------;

;---------------------------------------------;

; Open the INI file to be edited by the user, if this is the first time the script has been run
if (gConfigIsNew)
	Run, %ConfigFile%

Process, Exist, %EXECUTABLE_NAME%
if (!ErrorLevel) { ; if the executable isn't already running
	Run, %EXECUTABLE_PATH% ; launch the executable
	loop {
		Process, Wait, %EXECUTABLE_NAME%, 120 ; wait for it to load
		if (ErrorLevel) ; if the executable is running
			break ; break loop
		; If it took more than two minutes, ask the user if they wish to continue waiting
		MsgBox, 0x40034, Timed Out, %EXECUTABLE_TIMEOUTMSG% Continue waiting?
		IfMsgBox No
			ExitApp
	}
}

Hotkey, IfWinActive, ahk_exe %EXECUTABLE_NAME%
;-----------------------------------------;
; Place dynamically created hotkeys here. ;
;-----------------------------------------;

;-----------------------------------------;
Hotkey, IfWinActive

;-------------------------------;
; Place auto-execute code here. ;
;-------------------------------;

;-------------------------------;

Process, WaitClose, %EXECUTABLE_NAME% ; wait for the executable to close
ExitApp ; then exit app

;------------------------------------;
; Place hotkeys and other code here. ;
;------------------------------------;