49 lines
1.7 KiB
Go
49 lines
1.7 KiB
Go
|
|
//go:build windows
|
||
|
|
|
||
|
|
// 快捷方式注册:把本体写入开始菜单,使 Flow Launcher、PowerToys Run、
|
||
|
|
// Windows 搜索等工具能索引并快速启动。程序运行时幂等创建,不随退出删除。
|
||
|
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"cursor/internal/logger"
|
||
|
|
"cursor/internal/winexec"
|
||
|
|
)
|
||
|
|
|
||
|
|
// startMenuShortcutPath 返回开始菜单快捷方式的完整路径。
|
||
|
|
func startMenuShortcutPath() string {
|
||
|
|
appData := strings.TrimSpace(os.Getenv("APPDATA"))
|
||
|
|
if appData == "" {
|
||
|
|
homeDir, _ := os.UserHomeDir()
|
||
|
|
appData = filepath.Join(homeDir, "AppData", "Roaming")
|
||
|
|
}
|
||
|
|
return filepath.Join(appData, "Microsoft", "Windows", "Start Menu", "Programs", "Local-Cursor.lnk")
|
||
|
|
}
|
||
|
|
|
||
|
|
// ensureStartMenuShortcut 在开始菜单创建指向当前可执行文件的快捷方式。
|
||
|
|
// 已存在且指向一致时跳过;失败仅记录不阻断启动。
|
||
|
|
func ensureStartMenuShortcut() {
|
||
|
|
exePath, err := os.Executable()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
exePath = filepath.Clean(exePath)
|
||
|
|
lnkPath := startMenuShortcutPath()
|
||
|
|
|
||
|
|
if raw, err := os.ReadFile(lnkPath); err == nil && strings.Contains(strings.ToLower(string(raw)), strings.ToLower(exePath)) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
workDir := filepath.Dir(exePath)
|
||
|
|
psScript := `$ws = New-Object -ComObject WScript.Shell; $s = $ws.CreateShortcut('` + lnkPath + `'); $s.TargetPath = '` + exePath + `'; $s.WorkingDirectory = '` + workDir + `'; $s.Description = 'Local-Cursor'; $s.Save()`
|
||
|
|
cmd := winexec.Command("powershell", "-NoProfile", "-WindowStyle", "Hidden", "-Command", psScript)
|
||
|
|
if err := cmd.Run(); err != nil {
|
||
|
|
logger.Infof("ensureStartMenuShortcut: create failed lnk=%s err=%v", lnkPath, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
logger.Infof("ensureStartMenuShortcut: created lnk=%s target=%s", lnkPath, exePath)
|
||
|
|
}
|