Fix powershell window issues on windows

This commit is contained in:
qianlifeng 2024-11-04 10:31:47 +08:00
parent 7a804f1844
commit 6c0b7cd7ca
2 changed files with 7 additions and 25 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -1,7 +1,6 @@
package app package app
import ( import (
"bytes"
"context" "context"
"encoding/csv" "encoding/csv"
"encoding/json" "encoding/json"
@ -10,7 +9,6 @@ import (
"image" "image"
"image/color" "image/color"
"os" "os"
"os/exec"
"os/user" "os/user"
"path/filepath" "path/filepath"
"strings" "strings"
@ -240,15 +238,13 @@ func (a *WindowsRetriever) OpenAppFolder(ctx context.Context, app appInfo) error
} }
// Get app installation location using PowerShell // Get app installation location using PowerShell
cmd := exec.Command("powershell", "-Command", fmt.Sprintf(` output, err := util.ShellRunOutput("powershell", "-Command", fmt.Sprintf(`
$packageFamilyName = ($('%s' -split '!')[0]) $packageFamilyName = ($('%s' -split '!')[0])
$package = Get-AppxPackage | Where-Object { $_.PackageFamilyName -eq $packageFamilyName } $package = Get-AppxPackage | Where-Object { $_.PackageFamilyName -eq $packageFamilyName }
if ($package) { if ($package) {
Write-Output $package.InstallLocation Write-Output $package.InstallLocation
} }
`, appID)) `, appID))
output, err := cmd.Output()
if err != nil { if err != nil {
return fmt.Errorf("failed to get UWP app install location: %v", err) return fmt.Errorf("failed to get UWP app install location: %v", err)
} }
@ -291,20 +287,14 @@ func (a *WindowsRetriever) GetUWPApps(ctx context.Context) []appInfo {
` `
// Set command encoding to UTF-8 // Set command encoding to UTF-8
cmd := exec.Command("powershell", "-Command", powershellCmd) output, err := util.ShellRunOutput("powershell", "-Command", powershellCmd)
cmd.Env = append(os.Environ(), "PYTHONIOENCODING=utf-8") if err != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("Error running powershell command: %v", err))
// Capture command output
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("Execution of PowerShell command failed: %v", err))
return apps return apps
} }
// Parse CSV output // Parse CSV output
reader := csv.NewReader(strings.NewReader(out.String())) reader := csv.NewReader(strings.NewReader(string(output)))
records, err := reader.ReadAll() records, err := reader.ReadAll()
if err != nil { if err != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("Error parsing CSV output: %v", err)) util.GetLogger().Error(ctx, fmt.Sprintf("Error parsing CSV output: %v", err))
@ -433,17 +423,9 @@ func (a *WindowsRetriever) GetUWPAppIcon(ctx context.Context, appID string) (plu
} }
`, appID) `, appID)
cmd := exec.Command("powershell", "-WindowStyle", "Hidden", "-NonInteractive", "-NoProfile", "-Command", powershellCmd) output, err := util.ShellRunOutput("powershell", "-Command", powershellCmd)
// 添加启动信息来隐藏窗口
cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: true,
CreationFlags: 0x08000000, // CREATE_NO_WINDOW flag
}
output, err := cmd.Output()
if err != nil { if err != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("Execution of PowerShell command failed: %v", err)) util.GetLogger().Error(ctx, fmt.Sprintf("Error running powershell command: %v", err))
return plugin.WoxImage{}, err return plugin.WoxImage{}, err
} }