-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetDisplayDialogTest.ps1
More file actions
203 lines (172 loc) · 8.8 KB
/
Copy pathgetDisplayDialogTest.ps1
File metadata and controls
203 lines (172 loc) · 8.8 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
function Get-DisplayDialog {
Param (
[Parameter(Mandatory = $true,Position=0)][string] $dialogText,
[Parameter(Mandatory = $false)][string] $defaultAnswer,
[Parameter(Mandatory = $false)][bool] $hiddenAnswer = $false, #default for this is false normally
[Parameter(Mandatory = $false)][array] $buttons,
[Parameter(Mandatory = $false)][string] $defaultButtonText,
[Parameter(Mandatory = $false)][int] $defaultButtonInt,
[Parameter(Mandatory = $false)][string] $cancelButtonText,
[Parameter(Mandatory = $false)][int] $cancelButtonInt,
[Parameter(Mandatory = $false)][string] $title,
#[Parameter(Mandatory = $false)][string] $iconText,
#[Parameter(Mandatory = $false)][int] $iconInt,
[Parameter(Mandatory = $false)][string] $iconEnum,
[Parameter(Mandatory = $false)][string] $iconPath,
[Parameter(Mandatory = $false)][int] $givingUpAfter
)
#we're not doing $iconText/$iconInt because that's based on resource name/number which seems really old and not applicable anymore
#but if you've a real need for it, I can be convinced.
$hasDefaultButton = $false
$hasCancelButton = $false
$hasIcon = $false
#default dialog command (this is required so it ALWAYS has to be here)
$displayDialogCommand = "display dialog `"$dialogText`" "
#as we process each condition, we build out the display dialog command
#is there a default answer? if there is, put it on the end of the command
if(-not [string]::IsNullOrEmpty($defaultAnswer)) {
$displayDialogCommand = $displayDialogCommand + "default answer `"$defaultAnswer`" "
}
#is hidden reply true? (we also check for default answer, because without that, hidden reply is functionally stupid to care about)
#note that we already added the default answer, so this is just setting the hidden answer flag if it's true
if((-not[string]::IsNullOrEmpty($defaultAnswer) -and ($hiddenAnswer))){
$displayDialogCommand = $displayDialogCommand + "with hidden answer "
}
#-buttons processing. We first test for a count between 1 and three. If it's 0, we don't care, if it's > 3, pop error and exit
if(($buttons.length) -lt 1) {
#do nothing
} elseif($buttons.Length -eq 1){
#is this the only button? stupid but allowable, we only want one button, no commas and break out of the if
$button = $buttons[0]
$displayDialogCommand = $displayDialogCommand + "buttons `{`"$button`"`}"
} elseif(($buttons.Length) -gt 3) {
#by default display dialog always has a dialog reply. Piping the output to out-null supresses that so we get the
#dialog to display, but nothing else
"display dialog `"you can't list more than three buttons`""|/usr/bin/osascript |Out-Null
Exit
} else {
#buttons are an applescript list of strings, aka {"one","two"} so first, bulid the first brace
#now build the buttons
$buttonList = ""
foreach($button in $buttons) {
#Write-Output $buttons.IndexOf($button)
#Write-host "The index is $buttons.IndexOf($button)"
#if we get here, there's > 1 button in the list
if(($buttons.IndexOf($button)) -eq 0) {
#first button
$buttonList = $buttonList + "`"$button`","
} elseif ($buttons.IndexOf($button) -eq ($buttons.Length) - 1) {
#is this the last button? if so, we want no commas
$buttonList = $buttonList + "`"$button`""
} else {
#middle term, we want a trailing comma
$buttonList = $buttonList + "`"$button`","
}
}
$displayDialogCommand = $displayDialogCommand + "buttons `{$buttonList`} "
}
##default button
#buttons start at 1,if there's no default buttons, defaultButtonInt will = 0
if($defaultButtonInt -gt 0) {
#there's a default button int specified
$hasDefaultButton = $true #we won't check for text in this case
$displayDialogCommand = $displayDialogCommand + "default button $defaultButtonInt "
} elseif ((-not [string]::IsNullOrEmpty($defaultButtonText)) -and (-not $hasDefaultButton)) {
#there's something besides nothing in $hasDefaultButton and we haven't already done something with $defaultButtonInt
$hasDefaultButton = $true
$displayDialogCommand = $displayDialogCommand + "default button `"$defaultButtonText`" "
} elseif (-not [string]::IsNullOrEmpty($defaultButtonText)) {
#just in case we hit a weird condition
$hasDefaultButton = $true
$displayDialogCommand = $displayDialogCommand + "default button `"$defaultButtonText`" "
} else {
#we are going to ignore everything if this is hit
}
##cancel button
#basically just like default button
#may turn this into its own function. May not. feel cute, may delete later
if($cancelButtonInt -gt 0) {
#there's a default button int specified
$hasCancelButton = $true #we won't check for text in this case
$displayDialogCommand = $displayDialogCommand + "cancel button $cancelButtonInt "
} elseif ((-not [string]::IsNullOrEmpty($cancelButtonText)) -and (-not $hasCancelButton)) {
#there's something besides nothing in $hasDefaultButton and we haven't already done something with $defaultButtonInt
$hasCancelButton = $true
$displayDialogCommand = $displayDialogCommand + "cancel button `"$cancelButtonText`" "
} elseif (-not [string]::IsNullOrEmpty($cancelButtonText)) {
#just in case we hit a weird condition
$hasCancelButton = $true
$displayDialogCommand = $displayDialogCommand + "cancel button `"$cancelButtonText`" "
} else {
#we are going to ignore everything if this is hit
}
##title
if(-not [string]::IsNullOrEmpty($title)){
#there's something in the title
$displayDialogCommand = $displayDialogCommand + "with title `"$title`" "
}
##iconEnum and/or path
if(-not [string]::IsNullOrEmpty($iconEnum)){
#$iconEnum wins if both enum and path are filled out
#make sure only using the right icon enum values
if(($iconEnum -ne "note") -and ($iconEnum -ne "caution") -and ($iconEnum -ne "stop") ){
$iconEnum
return "badIconEnumError"
} else {
$hasIcon = $true
$displayDialogCommand = $displayDialogCommand + "with icon $iconEnum "
# $displayDialogCommand
}
} elseif ((-not [string]::IsNullOrEmpty($iconPath)) -and (-not $hasIcon)) {
#no $iconEnum but $iconPath
$hasIcon = $true
$displayDialogCommand = $displayDialogCommand + "with icon `(posix file `"$iconPath`"`) "
} elseif (-not [string]::IsNullOrEmpty($iconPath)){
#weird condition
$hasIcon = $true
$displayDialogCommand = $displayDialogCommand + "with icon `(posix file `"$iconPath`"`) "
} else {
#we are going to ignore everything if this is hit
}
##giving up after
#note that setting giving up after to 0 or negative integers is the same as infinity.
#the default value is 0, so we check for greater than 0
if($givingUpAfter -gt 0){
#$givingUpAfter is a positive integer greater than 0
#convert to string so it works in the command string even though it will be read as an int.
#command string building is weird, sigh.
$givingUpAfterString = [string]$givingUpAfter
$displayDialogCommand = $displayDialogCommand + "giving up after $givingUpAfterString "
}
$dialogReplyString = $displayDialogCommand|/usr/bin/osascript -so
#Write-Output "The dialog reply is: $dialogReply"
return $dialogReplyString
##building the full command will end up being a big fat if then else chain, but it will be worth it
##or we could put everything in an array and switch-case the whole thing
##do checks for wher you have ints/strings/etc for the same thign, like default button, icons, etc.
#"display dialog `"$dialogText`" default answer `"$defaultAnswer`" with icon $iconEnum"|/usr/bin/osascript
}
#we have to do this because we can't modify an array(list) we are iterating through
[System.Collections.ArrayList]$dialogReplyArray = @()
[System.Collections.ArrayList]$dialogReplyArrayList = @()
$dialogReply = [ordered]@{}
$dialogReplyString = Get-DisplayDialog -dialogText "My Simple Dialog"
#test for cancel button
if($dialogReplyString.Contains("execution error: User canceled. `(-128`)")) {
#Write-Output "user hit cancel button"
return "userCancelError"
}
#$dialogReplyString.GetType()
#build initial reply array
$dialogReplyArray = $dialogReplyString.Split(",")
#build dialog reply without trailing/leading spaces
foreach($item in $dialogReplyArray) {
$dialogReplyArrayList.Add($item.trim()) |Out-Null #so we don't see 0/1/etc.
}
#build the hashtable for the return
foreach($item in $dialogReplyArrayList) {
$hashEntry = $item.Split(":")
$dialogReply.Add($hashEntry[0],$hashEntry[1])
}
#return the hashtable
#return $dialogReply