@@ -58,11 +58,14 @@ function runWindowsHotkey(keys: string[]): void {
5858 const script = [
5959 `Add-Type -TypeDefinition 'using System; using System.Runtime.InteropServices; public static class NativeKeyboard { [DllImport("user32.dll")] public static extern void keybd_event(byte key, byte scan, uint flags, UIntPtr extra); }'` ,
6060 ...modifiers . map ( ( modifier ) => `[NativeKeyboard]::keybd_event(${ virtualKeys [ modifier ] } , 0, 0, [UIntPtr]::Zero)` ) ,
61+ 'Start-Sleep -Milliseconds 100' ,
6162 `[NativeKeyboard]::keybd_event(${ keyCode } , 0, 0, [UIntPtr]::Zero)` ,
63+ 'Start-Sleep -Milliseconds 100' ,
6264 `[NativeKeyboard]::keybd_event(${ keyCode } , 0, 2, [UIntPtr]::Zero)` ,
6365 ...modifiers
6466 . toReversed ( )
65- . map ( ( modifier ) => `[NativeKeyboard]::keybd_event(${ virtualKeys [ modifier ] } , 0, 2, [UIntPtr]::Zero)` )
67+ . map ( ( modifier ) => `[NativeKeyboard]::keybd_event(${ virtualKeys [ modifier ] } , 0, 2, [UIntPtr]::Zero)` ) ,
68+ 'Start-Sleep -Milliseconds 500'
6669 ] . join ( '; ' )
6770 execFileSync ( 'powershell.exe' , [ '-NoProfile' , '-NonInteractive' , '-Command' , script ] , {
6871 stdio : 'ignore' ,
@@ -102,7 +105,8 @@ export function openExternalText(platform: Platform, paths: RunPaths, candidateP
102105 '$shell = New-Object -ComObject WScript.Shell' ,
103106 'if (-not $shell.AppActivate($process.Id)) { throw "Notepad window could not be activated" }' ,
104107 'Start-Sleep -Milliseconds 500' ,
105- '$shell.SendKeys("^a")'
108+ '$shell.SendKeys("^a")' ,
109+ 'Start-Sleep -Milliseconds 500'
106110 ] . join ( '\n' )
107111 execFileSync ( 'powershell.exe' , [ '-NoProfile' , '-NonInteractive' , '-Command' , script ] , {
108112 stdio : [ 'ignore' , 'ignore' , 'pipe' ] ,
@@ -138,8 +142,11 @@ export function chooseNativeFile(platform: Platform, paths: RunPaths, candidateP
138142 } else {
139143 const isDirectory = statSync ( filePath ) . isDirectory ( )
140144 const script = [
145+ 'Set-StrictMode -Version Latest' ,
146+ '$ErrorActionPreference = "Stop"' ,
141147 'Add-Type -AssemblyName UIAutomationClient' ,
142148 'Add-Type -AssemblyName UIAutomationTypes' ,
149+ 'Add-Type -AssemblyName System.Windows.Forms' ,
143150 '$root = [System.Windows.Automation.AutomationElement]::RootElement' ,
144151 `$processCondition = [System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::ProcessIdProperty, ${ electronPid } )` ,
145152 '$deadline = [DateTime]::UtcNow.AddSeconds(10)' ,
@@ -155,33 +162,26 @@ export function chooseNativeFile(platform: Platform, paths: RunPaths, candidateP
155162 'Start-Sleep -Milliseconds 300' ,
156163 ...( isDirectory
157164 ? [
158- 'Add-Type -AssemblyName System.Windows.Forms' ,
159165 '[System.Windows.Forms.SendKeys]::SendWait("^l")' ,
160166 'Start-Sleep -Milliseconds 200' ,
161- '$pathInput = [System.Windows.Automation.AutomationElement]::FocusedElement' ,
162- '$valuePattern = $pathInput.GetCurrentPattern([System.Windows.Automation.ValuePattern]::Pattern)' ,
163- `$valuePattern.SetValue('${ escapePowerShell ( filePath ) } ')` ,
167+ `[System.Windows.Forms.SendKeys]::SendWait('${ escapePowerShell ( filePath ) } ')` ,
164168 '[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")' ,
165169 'Start-Sleep -Milliseconds 500' ,
166170 '$windows = $root.FindAll([System.Windows.Automation.TreeScope]::Children, $processCondition)' ,
167171 '$dialog = $windows | Where-Object { $_.Current.ClassName -eq "#32770" } | Select-Object -Last 1' ,
168172 'if (-not $dialog) { throw "Native folder dialog closed before selection" }' ,
169173 '$buttonCondition = [System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::ControlTypeProperty, [System.Windows.Automation.ControlType]::Button)' ,
170174 '$buttons = $dialog.FindAll([System.Windows.Automation.TreeScope]::Descendants, $buttonCondition)' ,
171- '$selectButton = $buttons | Where-Object { $_.Current.Name -in @("Select Folder", "Choose Folder", "Choose this folder", "Select") } | Select-Object -First 1' ,
175+ '$selectButton = $buttons | Where-Object { $_.Current.Name -in @("Select Folder", "Choose Folder", "Choose this folder", "Select a folder", "Select ") } | Select-Object -First 1' ,
172176 'if (-not $selectButton) { throw "Select Folder button was not found" }' ,
173- '$selectButton.GetCurrentPattern([System.Windows.Automation.InvokePattern]::Pattern).Invoke()'
177+ '$selectButton.SetFocus()' ,
178+ '[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")'
174179 ]
175180 : [
176- '$fileNameCondition = [System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::AutomationIdProperty, "1148")' ,
177- '$fileNameInput = $dialog.FindFirst([System.Windows.Automation.TreeScope]::Descendants, $fileNameCondition)' ,
178- 'if (-not $fileNameInput) { throw "File name input was not found" }' ,
179- '$valuePattern = $fileNameInput.GetCurrentPattern([System.Windows.Automation.ValuePattern]::Pattern)' ,
180- `$valuePattern.SetValue('${ escapePowerShell ( filePath ) } ')` ,
181- '$openCondition = [System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::NameProperty, "Open")' ,
182- '$openButton = $dialog.FindFirst([System.Windows.Automation.TreeScope]::Descendants, $openCondition)' ,
183- 'if (-not $openButton) { throw "Open button was not found" }' ,
184- '$openButton.GetCurrentPattern([System.Windows.Automation.InvokePattern]::Pattern).Invoke()'
181+ '[System.Windows.Forms.SendKeys]::SendWait("%n")' ,
182+ 'Start-Sleep -Milliseconds 200' ,
183+ `[System.Windows.Forms.SendKeys]::SendWait('${ escapePowerShell ( filePath ) } ')` ,
184+ '[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")'
185185 ] )
186186 ] . join ( '\n' )
187187 execFileSync ( 'powershell.exe' , [ '-NoProfile' , '-NonInteractive' , '-Command' , script ] , {
0 commit comments