Code: Select all
RunDynamicScript(script, scriptname := "", WorkingDir := "") {
static scriptdir := A_MyDocuments "\AutoHotkey\Dynamic Scripts", defaults := "#NoEnv`n#SingleInstance Off`nSetBatchLines -1`nSendMode Input`n`n"
. "Menu Tray, Tip, % ""AutoHotkey Dynamic Script (PID: "" DllCall(""GetCurrentProcessId"", ""UInt"") "")""`n`n", pid := DllCall("GetCurrentProcessId", "UInt"), count := 0
If !FileExist(scriptdir) {
FileCreateDir %scriptdir%
If (ErrorLevel = true)
return -4
} scriptno := ++count
If (scriptname = "")
scriptname := "AutoHotkey Dynamic Script"
else if scriptname contains \,/,:,*,?,",<,>,| ; "
scriptname := "AutoHotkey Dynamic Script"
DllCall("QueryPerformanceCounter", "Int64*", timestamp), scriptpath := scriptdir "\" scriptname " #" scriptno " " pid " " Format("{:x}", timestamp) ".ahk"
If (scriptfile := DllCall("CreateFile", "Str", scriptpath, "UInt", 0xC0000000, "UInt", 1, "Ptr", 0, "UInt", 2, "UInt", 0x4000100, "Ptr", 0, "Ptr")) = -1
return -1
If FileOpen(scriptfile, "h", "UTF-8").Write(defaults script) = 0
return -2
Run "%A_AhkPath%" /CP65001 "%scriptpath%", %WorkingDir%, UseErrorLevel, scriptpid
return ErrorLevel = 0 ? scriptpid : -3 ;, DllCall("CloseHandle", "Ptr", scriptfile)
}
---------------------------
AutoHotkey Dynamic Script #1 3932 7aa2ab4c13.ahk
---------------------------
Script file not found:
C:\Users\Main\Documents\AutoHotkey\Dynamic Scripts\AutoHotkey Dynamic Script #1 3932 7aa2ab4c13.ahk
---------------------------
OK
---------------------------
If I add a few miscellaneous lines of code in, I get something like this:
---------------------------
AutoHotkey Dynamic Script #1 7836 7a85764b5c.ahk
---------------------------
Error at line 0.
Script file "C:\Users\Main\Documents\AutoHotkey\Dynamic Scripts\AutoHotkey Dynamic Script #1 7836 7a85764b5c.ahk" cannot be opened.
The program will exit.
---------------------------
OK
---------------------------
Does anyone know what the problem is?