Advertisement
Guest User

AHK: Executable Wrapper Template

a guest
Sep 16th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #SingleInstance force
  2. SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
  3.  
  4. EXECUTABLE_PATH=
  5. EXECUTABLE_NAME=
  6. EXECUTABLE_TIMEOUTMSG=
  7.  
  8. ; Config file is the script name with .ini instead of .ahk or .exe
  9. SplitPath, A_ScriptName,,,, ConfigFile
  10. ConfigFile := ConfigFile . ".ini"
  11. gConfigIsNew := false
  12. if (!FileExist(ConfigFile))
  13.     gConfigIsNew := true
  14.  
  15. IniReadWrite(file, category, key, defaultValue) {
  16.     IniRead, output, %file%, %category%, %key%
  17.     if (output = "ERROR" or !output) {
  18.             IniWrite, %defaultValue%, %file%, %category%, %key%
  19.             output := defaultValue
  20.     }
  21.     return output
  22. }
  23.  
  24. ;---------------------------------------------;
  25. ; Read config options from the INI file here. ;
  26. ;---------------------------------------------;
  27.  
  28. ;---------------------------------------------;
  29.  
  30. ; Open the INI file to be edited by the user, if this is the first time the script has been run
  31. if (gConfigIsNew)
  32.     Run, %ConfigFile%
  33.  
  34. Process, Exist, %EXECUTABLE_NAME%
  35. if (!ErrorLevel) { ; if the executable isn't already running
  36.     Run, %EXECUTABLE_PATH% ; launch the executable
  37.     loop {
  38.         Process, Wait, %EXECUTABLE_NAME%, 120 ; wait for it to load
  39.         if (ErrorLevel) ; if the executable is running
  40.             break ; break loop
  41.         ; If it took more than two minutes, ask the user if they wish to continue waiting
  42.         MsgBox, 0x40034, Timed Out, %EXECUTABLE_TIMEOUTMSG% Continue waiting?
  43.         IfMsgBox No
  44.             ExitApp
  45.     }
  46. }
  47.  
  48. Hotkey, IfWinActive, ahk_exe %EXECUTABLE_NAME%
  49. ;-----------------------------------------;
  50. ; Place dynamically created hotkeys here. ;
  51. ;-----------------------------------------;
  52.  
  53. ;-----------------------------------------;
  54. Hotkey, IfWinActive
  55.  
  56. ;-------------------------------;
  57. ; Place auto-execute code here. ;
  58. ;-------------------------------;
  59.  
  60. ;-------------------------------;
  61.  
  62. Process, WaitClose, %EXECUTABLE_NAME% ; wait for the executable to close
  63. ExitApp ; then exit app
  64.  
  65. ;------------------------------------;
  66. ; Place hotkeys and other code here. ;
  67. ;------------------------------------;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement