rsh*_*lat 9 windows macos powershell cross-platform
我知道您可以在 PowerShell 版本 5 上执行类似的操作(如下所示),但是有没有办法重现可在 Windows 和 Mac OS 平台上运行的弹出消息框?
$ButtonType = [System.Windows.Forms.MessageBoxButtons]::OK
$MessageIcon = [System.Windows.Forms.MessageBoxIcon]::Information
$MessageBody = "Message Body goes here"
$MessageTitle = "Title"
$Result = [System.Windows.Forms.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
Run Code Online (Sandbox Code Playgroud)
上面代码的输出
Show-MessageBox
下面定义的函数在Windows 和 macOS 上提供消息框功能,以 WinFormsMessageBox
类为模型。
例子:
# Message box with custom message, OK button only, and default title and icon
$null = Show-MessageBox 'some message'
# Message box with custom message and title, buttons OK and cancel, and
# the Stop icon (critical error)
# Return value is the name of the button chosen.
$buttonChosen = Show-MessageBox 'some message' 'a title' -Buttons OKCancel -Icon Stop
Run Code Online (Sandbox Code Playgroud)
语法(为了可读性分布在多行中):
Show-MessageBox [-Message] <string> [[-Title] <string>]
[[-Buttons] {OK | OKCancel | AbortRetryIgnore | YesNoCancel | YesNo | RetryCancel}]
[-Icon {Information | Warning | Stop}]
[-DefaultButtonIndex {0 | 1 | 2}]
Run Code Online (Sandbox Code Playgroud)
笔记:
在 macOS 上,AppleScript 的display alert
命令用于创建消息框。
informational
、、、warning
和critical
,但前两者显示相同的图标(文件夹),而后者仅用感叹号覆盖文件夹图标。为了跨平台一致性,输出值是一个string,即用户按下的按钮的名称。
-DefaultButtonIndex
是0
当用户按下 时将按下的按钮的索引Enter。
Cancel
有按钮,Esc请按下它。No
,Abort
或Cancel
按钮并且它不是默认按钮,Esc请按它。Show-MessageBox
源代码:
function Show-MessageBox {
[CmdletBinding(PositionalBinding=$false)]
param(
[Parameter(Mandatory, Position=0)]
[string] $Message,
[Parameter(Position=1)]
[string] $Title,
[Parameter(Position=2)]
[ValidateSet('OK', 'OKCancel', 'AbortRetryIgnore', 'YesNoCancel', 'YesNo', 'RetryCancel')]
[string] $Buttons = 'OK',
[ValidateSet('Information', 'Warning', 'Stop')]
[string] $Icon = 'Information',
[ValidateSet(0, 1, 2)]
[int] $DefaultButtonIndex
)
# So that the $IsLinux and $IsMacOS PS Core-only
# variables can safely be accessed in WinPS.
Set-StrictMode -Off
$buttonMap = @{
'OK' = @{ buttonList = 'OK'; defaultButtonIndex = 0 }
'OKCancel' = @{ buttonList = 'OK', 'Cancel'; defaultButtonIndex = 0; cancelButtonIndex = 1 }
'AbortRetryIgnore' = @{ buttonList = 'Abort', 'Retry', 'Ignore'; defaultButtonIndex = 2; ; cancelButtonIndex = 0 };
'YesNoCancel' = @{ buttonList = 'Yes', 'No', 'Cancel'; defaultButtonIndex = 2; cancelButtonIndex = 2 };
'YesNo' = @{ buttonList = 'Yes', 'No'; defaultButtonIndex = 0; cancelButtonIndex = 1 }
'RetryCancel' = @{ buttonList = 'Retry', 'Cancel'; defaultButtonIndex = 0; cancelButtonIndex = 1 }
}
$numButtons = $buttonMap[$Buttons].buttonList.Count
$defaultIndex = [math]::Min($numButtons - 1, ($buttonMap[$Buttons].defaultButtonIndex, $DefaultButtonIndex)[$PSBoundParameters.ContainsKey('DefaultButtonIndex')])
$cancelIndex = $buttonMap[$Buttons].cancelButtonIndex
if ($IsLinux) {
Throw "Not supported on Linux."
}
elseif ($IsMacOS) {
$iconClause = if ($Icon -ne 'Information') { 'as ' + $Icon -replace 'Stop', 'critical' }
$buttonClause = "buttons { $($buttonMap[$Buttons].buttonList -replace '^', '"' -replace '$', '"' -join ',') }"
$defaultButtonClause = 'default button ' + (1 + $defaultIndex)
if ($null -ne $cancelIndex -and $cancelIndex -ne $defaultIndex) {
$cancelButtonClause = 'cancel button ' + (1 + $cancelIndex)
}
$appleScript = "display alert `"$Title`" message `"$Message`" $iconClause $buttonClause $defaultButtonClause $cancelButtonClause" #"
Write-Verbose "AppleScript command: $appleScript"
# Show the dialog.
# Note that if a cancel button is assigned, pressing Esc results in an
# error message indicating that the user canceled.
$result = $appleScript | osascript 2>$null
# Output the name of the button chosen (string):
# The name of the cancel button, if the dialog was canceled with ESC, or the
# name of the clicked button, which is reported as "button:<name>"
if (-not $result) { $buttonMap[$Buttons].buttonList[$buttonMap[$Buttons].cancelButtonIndex] } else { $result -replace '.+:' }
}
else { # Windows
Add-Type -Assembly System.Windows.Forms
# Show the dialog.
# Output the chosen button as a stringified [System.Windows.Forms.DialogResult] enum value,
# for consistency with the macOS behavior.
[System.Windows.Forms.MessageBox]::Show($Message, $Title, $Buttons, $Icon, $defaultIndex * 256).ToString()
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
31533 次 |
最近记录: |