@@ -101,43 +101,18 @@ export function openExternalText(platform: Platform, paths: RunPaths, candidateP
101101 } else {
102102 const script = [
103103 `Start-Process notepad.exe -ArgumentList '${ escapePowerShell ( filePath ) } '` ,
104- 'Add-Type -AssemblyName UIAutomationClient' ,
105- 'Add-Type -AssemblyName UIAutomationTypes' ,
106- `$expected = (Get-Content -Raw -LiteralPath '${ escapePowerShell ( filePath ) } ').Trim()` ,
107- '$root = [System.Windows.Automation.AutomationElement]::RootElement' ,
108- '$documentCondition = [System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::ControlTypeProperty, [System.Windows.Automation.ControlType]::Document)' ,
109- '$editCondition = [System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::ControlTypeProperty, [System.Windows.Automation.ControlType]::Edit)' ,
104+ `$filename = '${ escapePowerShell ( basename ( filePath ) ) } '` ,
110105 '$deadline = [DateTime]::UtcNow.AddSeconds(10)' ,
111- '$document = $null' ,
112- '$range = $null' ,
106+ '$notepad = $null' ,
113107 'do {' ,
114- ' $documents = @($root.FindAll([System.Windows.Automation.TreeScope]::Descendants, $documentCondition))' ,
115- ' $edits = @($root.FindAll([System.Windows.Automation.TreeScope]::Descendants, $editCondition))' ,
116- ' foreach ($candidate in (@($documents) + @($edits))) {' ,
117- ' $candidateRange = $null' ,
118- ' $candidateText = $null' ,
119- ' try {' ,
120- ' $candidateRange = $candidate.GetCurrentPattern([System.Windows.Automation.TextPattern]::Pattern).DocumentRange' ,
121- ' $candidateText = $candidateRange.GetText(-1)' ,
122- ' } catch {' ,
123- ' try {' ,
124- ' $candidateValue = $candidate.GetCurrentPattern([System.Windows.Automation.ValuePattern]::Pattern)' ,
125- ' $candidateText = $candidateValue.Current.Value' ,
126- ' } catch {}' ,
127- ' }' ,
128- ' if ($candidateText -and $candidateText.Trim().Contains($expected)) {' ,
129- ' $document = $candidate' ,
130- ' $range = $candidateRange' ,
131- ' break' ,
132- ' }' ,
133- ' }' ,
134- ' if (-not $document) { Start-Sleep -Milliseconds 200 }' ,
135- '} while (-not $document -and [DateTime]::UtcNow -lt $deadline)' ,
136- 'if (-not $document) { throw "Notepad document was not found" }' ,
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" }' ,
137112 '$shell = New-Object -ComObject WScript.Shell' ,
138- 'if (-not $shell.AppActivate($document.Current.ProcessId )) { throw "Notepad window could not be activated" }' ,
139- '$document.SetFocus() ' ,
140- 'if ($range) { $range.Select() } else { $ shell.SendKeys("^a") } ' ,
113+ 'if (-not $shell.AppActivate($notepad.Id )) { throw "Notepad window could not be activated" }' ,
114+ 'Start-Sleep -Milliseconds 300 ' ,
115+ '$ shell.SendKeys("^a")' ,
141116 'Start-Sleep -Milliseconds 500'
142117 ] . join ( '\n' )
143118 execFileSync ( 'powershell.exe' , [ '-NoProfile' , '-NonInteractive' , '-Command' , script ] , {
@@ -205,7 +180,8 @@ export function chooseNativeFile(platform: Platform, paths: RunPaths, candidateP
205180 '$acceptCondition = [System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::AutomationIdProperty, "1")' ,
206181 '$acceptButton = $dialog.FindFirst([System.Windows.Automation.TreeScope]::Descendants, $acceptCondition)' ,
207182 'if (-not $acceptButton) { throw "Native folder accept button was not found" }' ,
208- '$acceptButton.GetCurrentPattern([System.Windows.Automation.InvokePattern]::Pattern).Invoke()' ,
183+ '$acceptButton.SetFocus()' ,
184+ '[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")' ,
209185 'Start-Sleep -Milliseconds 500'
210186 ]
211187 : [
0 commit comments