-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsps.lua
More file actions
406 lines (344 loc) · 16.5 KB
/
msps.lua
File metadata and controls
406 lines (344 loc) · 16.5 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
-- Module options:
local register_global_module_table = false
local global_module_name = 'ps'
-- MIT License
-- Copyright (c) 2025 Matteo Speroni
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
local ps = { version = "msps 1.1.0" }
-- This module is only supported on Windows systems
local isUnix = package.config:sub(1, 1) == "/"
if isUnix then
error("msps module is only supported on Windows systems.")
end
if register_global_module_table then
_G[global_module_name] = ps
end
--- Displays a customizable message box using PowerShell and Microsoft.VisualBasic.
--- @param title string The title of the message box. Must be non-empty.
--- @param prompt string The message text to display. Must be non-empty.
--- @param boxType string|nil The type of buttons to display. Valid values: "OKOnly", "OKCancel", "AbortRetryIgnore", "YesNoCancel", "YesNo", "RetryCancel". Default is "OKOnly".
--- @param icon string|nil The icon to display. Valid values: "None", "Critical", "Question", "Exclamation", "Information". Default is "None".
--- @param defaultButton number|nil The default button (1, 2, or 3). Default is 1.
--- @return string|nil result The result of the user's interaction with the message box. Returns `nil` if the user cancels or closes the box.
function ps.msgBox(title, prompt, boxType, icon, defaultButton)
if not title or type(title) ~= "string" then
error("Title must be a string")
end
if title == "" then
error("Title cannot be empty")
end
if not prompt or type(prompt) ~= "string" then
error("Prompt must be a string")
end
if prompt == "" then
error("Prompt cannot be empty")
end
if not boxType or type(boxType) ~= "string" then
error("BoxType must be a string")
end
if not boxType or type(boxType) ~= "string" or boxType == "" then
boxType = "OKOnly"
end
if boxType ~= "OKOnly" and boxType ~= "OKCancel" and boxType ~= "AbortRetryIgnore" and boxType ~= "YesNoCancel" and boxType ~= "YesNo" and boxType ~= "RetryCancel" then
error("BoxType must be one of: OKOnly, OKCancel, AbortRetryIgnore, YesNoCancel, YesNo, RetryCancel")
end
if not icon or type(icon) ~= "string" or icon == "" then
icon = "None"
end
if icon and type(icon) == "string" and icon ~= "Critical" and icon ~= "Question" and icon ~= "Exclamation" and icon ~= "Information" and icon ~= "None" then
error("Icon must be one of: None, Critical, Question, Exclamation, Information")
end
if defaultButton and type(defaultButton) ~= "number" then
error("DefaultButton must be a number")
end
if defaultButton and defaultButton ~= 1 and defaultButton ~= 2 and defaultButton ~= 3 then
error("DefaultButton must be one of: 1, 2, 3")
end
if not defaultButton then
defaultButton = 1
end
local command = string.format([[
powershell -Command "Add-Type -AssemblyName Microsoft.VisualBasic; $title = \"%s\"; $prompt = \"%s\"; $boxType = \"%s\"; $icon = \"%s\"; $defaultButton = %s; switch ($icon) { "Question" { $visualBasicIcon = [Microsoft.VisualBasic.MsgBoxStyle]::Question }; "Critical" { $visualBasicIcon = [Microsoft.VisualBasic.MsgBoxStyle]::Critical }; "Exclamation" { $visualBasicIcon = [Microsoft.VisualBasic.MsgBoxStyle]::Exclamation }; "Information" { $visualBasicIcon = [Microsoft.VisualBasic.MsgBoxStyle]::Information } }; switch ($boxType) { "OKOnly" { $visualBasicButtons = [Microsoft.VisualBasic.MsgBoxStyle]::OKOnly }; "OKCancel" { $visualBasicButtons = [Microsoft.VisualBasic.MsgBoxStyle]::OkCancel }; "AbortRetryIgnore" { $visualBasicButtons = [Microsoft.VisualBasic.MsgBoxStyle]::AbortRetryIgnore }; "YesNoCancel" { $visualBasicButtons = [Microsoft.VisualBasic.MsgBoxStyle]::YesNoCancel }; "YesNo" { $visualBasicButtons = [Microsoft.VisualBasic.MsgBoxStyle]::YesNo }; "RetryCancel" { $visualBasicButtons = [Microsoft.VisualBasic.MsgBoxStyle]::RetryCancel } }; switch ($defaultButton) { 1 { $visualBasicDefaultButton = [Microsoft.VisualBasic.MsgBoxStyle]::DefaultButton1 }; 2 { $visualBasicDefaultButton = [Microsoft.VisualBasic.MsgBoxStyle]::DefaultButton2 }; 3 { $visualBasicDefaultButton = [Microsoft.VisualBasic.MsgBoxStyle]::DefaultButton3 } }; $popuptype = $visualBasicIcon -bor $visualBasicButtons -bor $visualBasicDefaultButton; $result = [Microsoft.VisualBasic.Interaction]::MsgBox($prompt, $popuptype, $title); if ($result) { Write-Output $result; exit 0 } else { exit 1 }"
]], title, prompt, boxType, icon, defaultButton)
local handle = io.popen(command, "r")
if not handle then
error("Failed to execute PowerShell command")
end
local result = handle:read("*a")
local suc, exitcode, code = handle:close()
if code == 0 then
result = result:gsub("\r", ""):gsub("\n", "")
return result
end
return nil
end
--- Displays an input box using PowerShell and Microsoft.VisualBasic.
--- @param title string The title of the input box window. Must be non-empty.
--- @param prompt string The text to display as the prompt in the input box. Must be non-empty.
--- @param default string|nil The default text to display in the input box. Default is an empty string.
--- @return string|nil result The text entered by the user. Returns `nil` if the user cancels the input box.
function ps.inputBox(title, prompt, default)
if not title or type(title) ~= "string" then
error("Title must be a string")
end
if title == "" then
error("Title cannot be empty")
end
if not prompt or type(prompt) ~= "string" then
error("Prompt must be a string")
end
if prompt == "" then
error("Prompt cannot be empty")
end
if default and type(default) ~= "string" then
error("Default value must be a string")
end
if not default then
default = ""
end
local command = string.format([[
powershell -Command "Add-Type -AssemblyName Microsoft.VisualBasic; $prompt = \"%s\"; $title = \"%s\"; $default = \"%s\"; $result = [Microsoft.VisualBasic.Interaction]::InputBox($prompt, $title, $default); if ($result) { Write-Output $result; exit 0 } else { exit 1 }"
]], prompt, title, default)
local handle = io.popen(command, "r")
if not handle then
error("Failed to execute PowerShell command")
end
local result = handle:read("*a")
local suc, exitcode, code = handle:close()
if code == 0 then
result = result:gsub("\r", ""):gsub("\n", "")
return result
end
return nil
end
--- Copies the given text to the system clipboard using PowerShell and System.Windows.Forms.
--- @param text string The text to copy to the clipboard. Must be a non-empty string.
function ps.setText(text)
if not text or type(text) ~= "string" then
error("Text must be a string")
end
if text == "" then
error("Text cannot be empty")
end
local command = string.format([[
powershell -Command "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Clipboard]::SetText('%s'); exit 0"
]], text)
os.execute(command)
end
--- Produces a beep sound using PowerShell and Microsoft.VisualBasic
function ps.beep()
os.execute([[
powershell -Command "Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.Interaction]::Beep(); exit 0"
]])
end
--- Displays a file open dialog using PowerShell and System.Windows.Forms
--- @param title string The title of the file open dialog window. Must be non-empty.
--- @param filter string The filter string for the file types to display. Must be non-empty.
--- @param multiSelect boolean|nil Whether to allow multiple file selection. Default is false.
--- @return table|nil files A table of selected file paths if the user selects files, or `nil` if the user cancels.
function ps.openFile(title, filter, multiSelect)
if not title or type(title) ~= "string" then
error("Title must be a string")
end
if title == "" then
error("Title cannot be empty")
end
if not filter or type(filter) ~= "string" then
error("Filter must be a string")
end
if filter == "" then
error("Filter cannot be empty")
end
if multiSelect == nil then
multiSelect = false
end
if type(multiSelect) ~= "boolean" then
error("MultiSelect must be a boolean")
end
local command = string.format([[
powershell -Command "Add-Type -AssemblyName System.Windows.Forms; $dialog = New-Object System.Windows.Forms.OpenFileDialog; $dialog.Title = '%s'; $dialog.Filter = '%s'; $dialog.Multiselect = $%s; $result = $dialog.ShowDialog(); if ($result -eq [System.Windows.Forms.DialogResult]::OK) { Write-Output ($dialog.FileNames -join '|'); exit 0 } else { exit 1 }"
]], title, filter, multiSelect)
local handle = io.popen(command, "r")
if not handle then
error("Failed to execute PowerShell command")
end
local result = handle:read("*a")
local suc, exitcode, code = handle:close()
if code == 0 then
result = result:gsub("\r\n","")
local files = {}
for f in string.gmatch(result, "([^|]+)") do
table.insert(files, f)
end
return files
end
return nil
end
--- Displays a folder browser dialog using PowerShell and System.Windows.Forms
--- @param description string The description text to display in the folder browser dialog. Must be a string.
--- @param showNewFolderButton boolean|nil Whether to show the "New Folder" button. Default is false.
--- @return string|nil result The selected folder path if the user selects a folder, or `nil` if the user cancels.
function ps.folderBrowser(description, showNewFolderButton)
if not description then
description = ""
end
if type(description) ~= "string" then
error("Description must be a string")
end
if showNewFolderButton == nil then
showNewFolderButton = false
end
if type(showNewFolderButton) ~= "boolean" then
error("ShowNewFolderButton must be a boolean")
end
local command = string.format([[
powershell -Command "Add-Type -AssemblyName System.Windows.Forms; $dialog = New-Object System.Windows.Forms.FolderBrowserDialog; $dialog.Description = \"%s\"; $dialog.ShowNewFolderButton = $%s; $result = $dialog.ShowDialog(); if ($result -eq [System.Windows.Forms.DialogResult]::OK) { Write-Output $dialog.SelectedPath; exit 0 } else { exit 1 }"
]], description, showNewFolderButton)
local handle = io.popen(command, "r")
if not handle then
error("Failed to execute PowerShell command")
end
local result = handle:read("*a")
local suc, exitcode, code = handle:close()
if code == 0 then
result = result:gsub("\r", ""):gsub("\n", "")
return result
end
return nil
end
--- Displays a custom XAML-based dialog using PowerShell and WPF, and retrieves values from specified controls
--- @param xaml string The XAML string defining the UI layout. Must be a valid XAML string. Close button with Name "Close" is required.
--- @param inputs table A table of control names (strings) whose values are to be retrieved.
--- @return string|nil result A JSON string containing the values of the specified controls, or `nil` if the user cancels or closes the dialog.
function ps.customXaml(xaml, inputs)
if not xaml or type(xaml) ~= "string" then
error("Invalid XAML input.")
end
if not inputs or type(inputs) ~= "table" then
error("Invalid inputs table.")
end
for key, value in pairs(inputs) do
if type(key) ~= "number" then
error("Invalid inputs table. Keys must be numeric indices.")
end
if type(value) ~= "string" then
error("Invalid inputs table. Values must be strings.")
end
end
local temp = ""
for i = 1, #inputs do
temp = temp .. '"' .. inputs[i] .. '"'
if i < #inputs then
temp = temp .. ", "
end
end
inputs = "@(" .. temp .. ")"
local command = string.format([=[
Add-Type -AssemblyName PresentationFramework
$xaml = @'
%s
'@
$ControlNames = %s
$reader = New-Object System.Xml.XmlNodeReader ([xml]$xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
$Global:XamlResult = $null
$closeButton = $window.FindName("Close")
$closeButton.Add_Click({
$result = @{}
foreach ($name in $ControlNames) {
$control = $window.FindName($name)
if ($control) {
$value = switch ($control.GetType().Name) {
"TextBox" { $control.Text }
"PasswordBox" { $control.Password }
"DatePicker" { if ($control.SelectedDate -ne $null) { $control.SelectedDate.Ticks } else { $null } }
"CheckBox" { $control.IsChecked }
"ComboBox" { if ($control.SelectedItem -ne $null) { $control.SelectedItem.Content } else { $null } }
"ListBox" { $control.SelectedItems }
"Slider" { $control.Value }
"RadioButton" { $control.IsChecked }
"ProgressBar" { $control.Value }
default {
if ($control.PSObject.Properties.Match("Text")) { $control.Text }
elseif ($control.PSObject.Properties.Match("Content")) { $control.Content }
elseif ($control.PSObject.Properties.Match("Value")) { $control.Value }
else { $null }
}
}
$result[$name] = $value
}
else {
$result[$name] = $null
}
}
$Global:XamlResult = $result | ConvertTo-Json -Depth 100 -Compress | Out-String
$window.Close()
})
$window.ShowDialog() | Out-Null
if ($Global:XamlResult) {
Write-Host $Global:XamlResult
exit 0
} else {
exit 1
}
]=], xaml, inputs)
local tempFile = ".\\temp.ps1"
local f = assert(io.open(tempFile,"w"))
f:write(command)
f:close()
local handle = io.popen("powershell -ExecutionPolicy Bypass -File " .. tempFile)
if not handle then
os.remove(tempFile)
error("Failed to execute PowerShell script.")
end
local result = handle:read("*a")
local suc, exitcode, code = handle:close()
os.remove(tempFile)
if code == 0 then
return result
end
return nil
end
--- Displays a notification icon with a balloon tip.
--- @param title string The title of the notification. Must be non-empty.
--- @param text string|nil The text of the notification. Optional.
--- @param icon string|nil The icon of the notification. One of "None", "Info", "Warning", "Error". Optional.
function ps.notifyIcon(title, text, icon)
if not title or type(title) ~= "string" then
error("Title must be a string")
end
if title == "" then
error("Title cannot be empty")
end
if not text or type(text) ~= "string" then
error("Text must be a string or nil")
end
if not text then
text = ""
end
if not icon or type(icon) ~= "string" or icon == "" then
icon = "None"
end
if icon ~= "None" and icon ~= "Info" and icon ~= "Warning" and icon ~= "Error" then
error("Icon must be one of: None, Info, Warning, Error")
end
local command = string.format([[
powershell -Command "Add-Type -AssemblyName System.Windows.Forms; $balloon = New-Object System.Windows.Forms.NotifyIcon; $path = (Get-Process -Id $pid).Path; $balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path); $balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::%s; $balloon.BalloonTipText = '%s'; $balloon.BalloonTipTitle = '%s'; $balloon.Visible = $true; $balloon.ShowBalloonTip(0); Start-Sleep -Seconds 1; exit 0"
]], icon, text, title)
os.execute(command)
end
return ps