-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclsScript.cls
More file actions
200 lines (196 loc) · 9.35 KB
/
clsScript.cls
File metadata and controls
200 lines (196 loc) · 9.35 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
Option Explicit
Private MainJScriptControl As ScriptControl, MainVBScriptControl As ScriptControl
Private nScript As clsScript
Private lCommand As String
Private Sub BetterErrHandler(ByVal ErrObject As Object, Optional ByVal ScriptString As String = "")
Dim Msg As frmMsg
Set Msg = New frmMsg
Msg.Caption = "WDE [" & ErrObject.Source & "]"
Msg.lblMsg.Caption = "Error: " & ErrObject.Number & vbCrLf
Msg.lblMsg.Caption = Msg.lblMsg.Caption & "Description: " & ErrObject.Description & vbCrLf
If ScriptString <> "" Then Msg.lblMsg.Caption = Msg.lblMsg.Caption & "Script: " & ScriptString & vbCrLf
If ErrObject.Line <> 1 Then Msg.lblMsg.Caption = Msg.lblMsg.Caption & "Line: " & ErrObject.Line
If ErrObject.Column <> 0 Then Msg.lblMsg.Caption = Msg.lblMsg.Caption & " / Column: " & ErrObject.Column & vbCrLf
If ErrObject.Text <> "" Then Msg.lblMsg.Caption = Msg.lblMsg.Caption & "Text: " & ErrObject.Text
Msg.Show
Err.Clear
End Sub
Private Sub PrepareScriptControl(ByRef scObject As ScriptControl, ByVal ScriptType As String, Optional ByVal Args As Variant = "")
If scObject.Language <> "" Then scObject.Reset
With scObject
Select Case ScriptType
Case "JScript"
.AllowUI = True
.Language = "JScript"
.AddCode "function alert(m){ Computer.Msg(m); }"
.AddCode "function dump(obj){ var out = ''; for (var i in obj) out += i + ': ' + obj[i] + ""\n""; alert(out); }"
.AddCode "function prompt(m){ return Computer.Prompt(m); }"
.AddCode "function startsWith(str, suffix){ return str.indexOf(suffix, suffix.length) !== -1; }"
.AddCode "function insWith(str, suffix){ return str.indexOf(suffix) !== -1; }"
.AddCode "function endsWith(str, suffix){ return str.indexOf(suffix, str.length - suffix.length) !== -1; }"
.AddCode "var Return = """";"
If Args <> "" Then .AddCode "var Args = """ & Args & """; alert(Args);"
Case "VBScript"
.AllowUI = True
.Language = "VBScript"
.AddCode "Const MF_CHECKED = &H8&"
.AddCode "Const MF_APPEND = &H100&"
.AddCode "Const TPM_LEFTALIGN = &H0&"
.AddCode "Const MF_DISABLED = &H2&"
.AddCode "Const MF_GRAYED = &H1&"
.AddCode "Const MF_SEPARATOR = &H800&"
.AddCode "Const MF_STRING = &H0&"
.AddCode "Const MF_POPUP = &H10&"
.AddCode "Const TPM_RETURNCMD = &H100&"
.AddCode "Const TPM_RIGHTBUTTON = &H2&"
.AddCode "Dim Return"
If Args <> "" Then .AddCode "Const Args = """ & Args & """"
End Select
.AddObject "Application", Application
.AddObject "shGUI", shGUI
.AddObject "Clip", Clip
.AddObject "Computer", Computer
.AddObject "Explorer", Explorer
.AddObject "File", File
.AddObject "iExplore", iExplore
'.AddObject "inet", iNet
.AddObject "Keyboard", Keyboard
.AddObject "Menu", Menu
.AddObject "Mouse", Mouse
.AddObject "Joypad", Joypad
.AddObject "Script", Me
.AddObject "Str", Str
.AddObject "ThisWorkbook", ThisWorkbook
.AddObject "V", Var
.AddObject "Window", Window
End With
End Sub
Private Sub Class_Terminate()
Set MainJScriptControl = Nothing
Set MainVBScriptControl = Nothing
End Sub
Public Property Get LastCommand() As String
LastCommand = lCommand
End Property
Public Function AddCode(ByVal CommandString As String, Optional ByVal getReturn As Boolean = False, Optional ByVal ScriptType As String = "Auto") As String
lCommand = CommandString
If ScriptType = "Auto" Then If iRight(CommandString, ";") Or iRight(CommandString, "}") Then ScriptType = "JScript" Else ScriptType = "VBScript"
On Error Resume Next
Select Case UCase(ScriptType)
Case "JSCRIPT"
If MainJScriptControl Is Nothing Then Reset
MainJScriptControl.AddCode CommandString
If Err.Number <> 0 Then BetterErrHandler MainJScriptControl.Error
If getReturn Then If Not MainJScriptControl.CodeObject.Return Is Nothing Then AddCode = MainJScriptControl.CodeObject.Return
Case "VBSCRIPT"
If MainVBScriptControl Is Nothing Then Reset
MainVBScriptControl.AddCode CommandString
If Err.Number <> 0 Then BetterErrHandler MainVBScriptControl.Error
If getReturn Then If Not MainVBScriptControl.CodeObject.Return Is Nothing Then AddCode = MainVBScriptControl.CodeObject.Return
End Select
On Error GoTo 0
End Function
Public Function Execute(ByVal FilePath As String, Optional ByVal Args As Variant = "", Optional ByVal ByLine As Boolean = False, Optional ByVal getReturn As Boolean = False, Optional ByVal ScriptType As String = "Auto", Optional ByVal StandAlone As Boolean = False) As String
If StandAlone Then
Dim nScript As clsScript
Set nScript = New clsScript
Execute = nScript.Execute(FilePath, Args, ByLine, getReturn, ScriptType)
Set nScript = Nothing
Exit Function
End If
Dim tPath As String
tPath = FilePath
If Not File.Exist(FilePath) Then FilePath = ThisWorkbook.Path & "\scripts\" & tPath
If Not File.Exist(FilePath) Then FilePath = ThisWorkbook.Path & "\scripts\" & tPath & ".js"
If Not File.Exist(FilePath) Then FilePath = ThisWorkbook.Path & "\scripts\" & tPath & ".vbs"
If Not File.Exist(FilePath) Then FilePath = ThisWorkbook.Path & "\scripts\" & tPath & ".bat"
If Not File.Exist(FilePath) Then
Computer.Form "Fichier " & tPath & vbCrLf & "Introuvable", "WDE [Script.Execute]"
Exit Function
End If
If ScriptType <> "Auto" Then
ElseIf iRight(FilePath, ".js") Then
ScriptType = "JScript"
ElseIf iRight(FilePath, ".vbs") Then
ScriptType = "VBScript"
ElseIf iRight(FilePath, ".bat") Then
Shell "CMD /k " & FilePath, vbNormalFocus
Else
Exit Function
End If
If Dir(FilePath) = "" Then Exit Function
Dim ScriptScriptControl As ScriptControl
Set ScriptScriptControl = New ScriptControl
PrepareScriptControl ScriptScriptControl, ScriptType, Args
On Error Resume Next
Dim nIF As Integer
nIF = FreeFile
Dim StringLine As String, PosLine As Long
Dim StringAll As String
Open FilePath For Input As #nIF
PosLine = 0
While Not EOF(nIF)
Line Input #nIF, StringLine
If ByLine Then
PosLine = PosLine + 1
AddCode StringLine, , ScriptType
If Err.Number <> 0 Then
MsgBox "Error: " & Err.Number & vbCrLf & _
"Script: " & FilePath & vbCrLf & _
"Line: " & PosLine & vbCrLf & _
"Command: " & StringLine & vbCrLf & _
Err.Description, vbCritical, "wV [" & Err.Source & "]"
Err.Clear
End If
lCommand = StringLine
Else
If UCase(ScriptType) = "VBSCRIPT" Then
If UCase(Left(StringLine, 4)) = "DIM " And InStr(1, StringLine, " = ", vbTextCompare) > 0 Then
Dim sVar As String
sVar = Right(StringLine, Len(StringLine) - 4)
sVar = Left(sVar, InStr(1, sVar, " = ") - 1)
If InStr(1, StringLine, ": ", vbTextCompare) > 0 Then If InStr(1, StringLine, ": ", vbTextCompare) < InStr(1, StringLine, " = ", vbTextCompare) Then sVar = ""
If sVar <> "" Then
StringLine = Right(StringLine, Len(StringLine) - 4)
StringLine = "Dim " & sVar & ": " & StringLine
End If
End If
End If
StringAll = StringAll & vbCrLf & StringLine
lCommand = StringAll
End If
Wend
Close #nIF
If Not ByLine Then ScriptScriptControl.AddCode StringAll
If Err.Number <> 0 Then BetterErrHandler ScriptScriptControl.Error, FilePath
If getReturn Then If Not ScriptScriptControl.CodeObject.Return Is Nothing Then Execute = ScriptScriptControl.CodeObject.Return
On Error GoTo 0
ScriptScriptControl.Reset
Set ScriptScriptControl = Nothing
End Function
Public Function Func(ByVal FilePath As String, Optional Args As String = "", Optional ByVal ByLine As Boolean = False, Optional ByVal ScriptType As String = "Auto", Optional ByVal StandAlone As Boolean = False) As String
Dim tPath As String
tPath = FilePath
Func = Execute(FilePath, Args, ByLine, True, ScriptType, StandAlone)
End Function
Public Sub Reset()
If Not (MainJScriptControl Is Nothing) Then Set MainJScriptControl = Nothing
If Not (MainVBScriptControl Is Nothing) Then Set MainVBScriptControl = Nothing
Set MainJScriptControl = New ScriptControl
Set MainVBScriptControl = New ScriptControl
PrepareScriptControl MainJScriptControl, "JScript"
PrepareScriptControl MainVBScriptControl, "VBScript"
End Sub
Public Sub Win(ByVal StringPath As String, ByVal hwnd As Long)
Dim tFile As String
tFile = StringPath
If Not File.Exist(StringPath) Then StringPath = Window.ScriptDir & tFile
If Not File.Exist(StringPath) Then StringPath = Window.ScriptDir & tFile & ".js"
If Not File.Exist(StringPath) Then StringPath = Window.ScriptDir & tFile & ".vbs"
If Not File.Exist(StringPath) Then Exit Sub
Dim lHWnd As Long
lHWnd = Window.CurHWnd
Window.CurHWnd = hwnd
Execute StringPath, , False
Window.CurHWnd = -1
End Sub