@@ -100,18 +100,16 @@ export function openExternalText(platform: Platform, paths: RunPaths, candidateP
100100 )
101101 } else {
102102 const script = [
103- `Start-Process notepad.exe -ArgumentList '${ escapePowerShell ( filePath ) } '` ,
104- `$filename = ' ${ escapePowerShell ( basename ( filePath ) ) } '` ,
103+ `$expected = (Get-Content -Raw -LiteralPath '${ escapePowerShell ( filePath ) } ').Trim() ` ,
104+ '$notepad = Start-Process notepad.exe -PassThru' ,
105105 '$deadline = [DateTime]::UtcNow.AddSeconds(10)' ,
106- '$notepad = $null' ,
107- 'do {' ,
108- ' $notepad = Get-Process -Name notepad -ErrorAction SilentlyContinue | Where-Object { $_.MainWindowTitle.Contains($filename) } | Sort-Object StartTime -Descending | Select-Object -First 1' ,
109- ' if (-not $notepad) { Start-Sleep -Milliseconds 200 }' ,
110- '} while (-not $notepad -and [DateTime]::UtcNow -lt $deadline)' ,
111- 'if (-not $notepad) { throw "Notepad window was not found" }' ,
106+ 'do { $notepad.Refresh(); if ($notepad.MainWindowHandle -eq 0) { Start-Sleep -Milliseconds 200 } } while ($notepad.MainWindowHandle -eq 0 -and [DateTime]::UtcNow -lt $deadline)' ,
107+ 'if ($notepad.MainWindowHandle -eq 0) { throw "Notepad window was not found" }' ,
112108 '$shell = New-Object -ComObject WScript.Shell' ,
113109 'if (-not $shell.AppActivate($notepad.Id)) { throw "Notepad window could not be activated" }' ,
114110 'Start-Sleep -Milliseconds 300' ,
111+ '$shell.SendKeys($expected)' ,
112+ 'Start-Sleep -Milliseconds 200' ,
115113 '$shell.SendKeys("^a")' ,
116114 'Start-Sleep -Milliseconds 500'
117115 ] . join ( '\n' )
@@ -154,6 +152,7 @@ export function chooseNativeFile(platform: Platform, paths: RunPaths, candidateP
154152 'Add-Type -AssemblyName UIAutomationClient' ,
155153 'Add-Type -AssemblyName UIAutomationTypes' ,
156154 'Add-Type -AssemblyName System.Windows.Forms' ,
155+ `Add-Type -TypeDefinition 'using System; using System.Runtime.InteropServices; public static class NativeDialog { [DllImport("user32.dll")] public static extern IntPtr GetDlgItem(IntPtr dialog, int id); [DllImport("user32.dll")] public static extern IntPtr SendMessage(IntPtr window, uint message, IntPtr wParam, IntPtr lParam); }'` ,
157156 '$root = [System.Windows.Automation.AutomationElement]::RootElement' ,
158157 `$processCondition = [System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::ProcessIdProperty, ${ electronPid } )` ,
159158 '$deadline = [DateTime]::UtcNow.AddSeconds(10)' ,
@@ -177,11 +176,9 @@ export function chooseNativeFile(platform: Platform, paths: RunPaths, candidateP
177176 '$windows = $root.FindAll([System.Windows.Automation.TreeScope]::Children, $processCondition)' ,
178177 '$dialog = $windows | Where-Object { $_.Current.ClassName -eq "#32770" } | Select-Object -Last 1' ,
179178 'if (-not $dialog) { throw "Native folder dialog closed before selection" }' ,
180- '$acceptCondition = [System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::AutomationIdProperty, "1")' ,
181- '$acceptButton = $dialog.FindFirst([System.Windows.Automation.TreeScope]::Descendants, $acceptCondition)' ,
182- 'if (-not $acceptButton) { throw "Native folder accept button was not found" }' ,
183- '$acceptButton.SetFocus()' ,
184- '[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")' ,
179+ '$acceptHandle = [NativeDialog]::GetDlgItem([IntPtr]$dialog.Current.NativeWindowHandle, 1)' ,
180+ 'if ($acceptHandle -eq [IntPtr]::Zero) { throw "Native folder accept button was not found" }' ,
181+ '$null = [NativeDialog]::SendMessage($acceptHandle, 0x00F5, [IntPtr]::Zero, [IntPtr]::Zero)' ,
185182 'Start-Sleep -Milliseconds 500'
186183 ]
187184 : [
0 commit comments