-
Notifications
You must be signed in to change notification settings - Fork 72
/
CreateScriptV1.ahk
100 lines (100 loc) · 3.75 KB
/
CreateScriptV1.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
;CreateScript AHK V1 Version
;https://autohotkey.com/board/topic/56142-createscript-create-script-from-main-script/
CreateScript(script){
static mScript
StringReplace,script,script,`n,`r`n,A
StringReplace,script,script,`r`r,`r,A
If RegExMatch(script,"m)^[^:]+:[^:]+|[a-zA-Z0-9#_@]+\{}$"){
If !(mScript){
If (A_IsCompiled){
lib := DllCall("GetModuleHandle", "ptr", 0, "ptr")
If !(res := DllCall("FindResource", "ptr", lib, "str", ">AUTOHOTKEY SCRIPT<", "ptr", Type:=10, "ptr"))
If !(res := DllCall("FindResource", "ptr", lib, "str", ">AHK WITH ICON<", "ptr", Type:=10, "ptr")){
MsgBox Could not extract script!
return
}
DataSize := DllCall("SizeofResource", "ptr", lib, "ptr", res, "uint")
,hresdata := DllCall("LoadResource", "ptr", lib, "ptr", res, "ptr")
,pData := DllCall("LockResource", "ptr", hresdata, "ptr")
If (DataSize){
mScript:=StrGet(pData,"UTF-8")
StringReplace,mScript,mScript,`n,`r`n,A
StringReplace,mScript,mScript,`r,`r`n,A
StringReplace,mScript,mScript,`r`r,`r,A
StringReplace,mScript,mScript,`n`n,`n,A
mScript :="`r`n" mScript "`r`n"
}
} else {
FileRead,mScript,%A_ScriptFullPath%
StringReplace,mScript,mScript,`n,`r`n,A
StringReplace,mScript,mScript,`r`r,`r,A
mScript := "`r`n" mScript "`r`n"
Loop,Parse,mScript,`n,`r
{
If A_Index=1
mScript:=""
If RegExMatch(A_LoopField,"i)^\s*#include"){
temp:=RegExReplace(A_LoopField,"i)^\s*#include[\s+|,]")
If InStr(temp,"%"){
Loop,Parse,temp,`%
{
If (A_Index=1)
temp:=A_LoopField
else if !Mod(A_Index,2)
_temp:=A_LoopField
else {
_temp:=%_temp%
temp.=_temp A_LoopField
_temp:=""
}
}
}
If InStr(FileExist(trim(temp,"<>")),"D"){
SetWorkingDir % trim(temp,"<>")
continue
} else if InStr(FileExist(temp),"D"){
SetWorkingDir % temp
continue
} else If (SubStr(temp,1,1) . SubStr(temp,0) = "<>"){
If !FileExist(_temp:=A_ScriptDir "\lib\" trim(temp,"<>") ".ahk")
If !FileExist(_temp:=A_MyDocuments "\AutoHotkey\lib\" trim(temp,"<>") ".ahk")
If !FileExist(_temp:=SubStr(A_AhkPath,1,InStr(A_AhkPath,"\",1,0)) "lib\" trim(temp,"<>") ".ahk")
If FileExist(_temp:=SubStr(A_AhkPath,1,InStr(A_AhkPath,"\",1,0)) "lib.lnk"){
FileGetShortcut,_temp,_temp
_temp:=_temp "\" trim(temp,"<>") ".ahk"
}
FileRead,_temp,%_temp%
mScript.= _temp "`r`n"
} else {
FileRead,_temp,%temp%
mScript.= _temp "`r`n"
}
} else mScript.=A_LoopField "`r`n"
}
}
}
Loop,Parse,script,`n,`r
{
If A_Index=1
script=
else If A_LoopField=
Continue
If (RegExMatch(A_LoopField,"^[^:\s]+:[^:\s=]+$")){
StringSplit,label,A_LoopField,:
If (label0=2 and IsLabel(label1) and IsLabel(label2)){
script .=SubStr(mScript
, h:=InStr(mScript,"`r`n" label1 ":`r`n")
, InStr(mScript,"`r`n" label2 ":`r`n")-h) . "`r`n"
}
} else if RegExMatch(A_LoopField,"^[^\{}\s]+\{}$"){
StringTrimRight,label,A_LoopField,2
script .= SubStr(mScript
, h:=RegExMatch(mScript,"i)\n" label "\([^\)\n]*\)\n?\s*\{")
, RegExMatch(mScript "`r`n","\n\s*}\s*\K\n",1,h)-h) . "`r`n"
} else
script .= A_LoopField "`r`n"
}
}
StringReplace,script,script,`r`n,`n,All
Return Script
}