使用 PowerShell 脚本为 Cursor 添加右键菜单项

使用 PowerShell 脚本为 Cursor 添加右键菜单项

一、日常工作中的效率提升

  通过右键菜单快速访问常用程序可以显著提高效率。本文将介绍如何使用 PowerShell 脚本为 Cursor 应用程序添加右键菜单项,使你能够方便地在文件、文件夹以及文件夹背景上通过右键菜单直接打开 Cursor。

二、准备工作

  1. Cursor 应用程序已安装:你需要确保 Cursor 应用程序已经安装在你的系统上,并且知道其可执行文件 (Cursor.exe) 的路径。
  2. PowerShell:本文使用的是 Windows PowerShell,确保你的系统上已安装并可以使用。
  3. 管理员权限:由于我们将修改系统注册表,脚本需要以管理员权限运行。

三、PowerShell 脚本内容详解

复制代码
# 检查是否以管理员权限运行
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

# 如果没有管理员权限,重新以管理员权限启动脚本
if (-not $isAdmin) {
    # 使用管理员权限重新启动 PowerShell 脚本
    Start-Process powershell.exe -Verb RunAs -ArgumentList ("-File", $MyInvocation.MyCommand.Path)
    exit
}

# 定义 Cursor 可执行文件的路径
$cursorExePath = [System.IO.Path]::Combine($env:LOCALAPPDATA, "Programs", "cursor", "Cursor.exe")

# 检查 Cursor 可执行文件是否存在
if (Test-Path $cursorExePath) {

    # 定义一个函数用于执行注册表命令
    function Run-RegCommand {
        param (
            [string]$command  # 注册表命令参数
        )
        # 执行 reg.exe 命令并等待完成
        $process = Start-Process -FilePath "reg.exe" -ArgumentList $command -NoNewWindow -Wait -PassThru
        if ($process.ExitCode -ne 0) {
            Write-Host "Failed to execute: reg.exe $command"
            exit 1
        }
    }

    # 安装右键菜单项(背景)
    $backgroundPath = "HKEY_CLASSES_ROOT\Directory\Background\shell\Open with Cursor"
    Run-RegCommand "ADD `"$backgroundPath`" /ve /d `"Open with Cursor`" /f"  # 添加主键
    Run-RegCommand "ADD `"$backgroundPath`" /v Icon /d `"$cursorExePath`" /f"  # 设置图标
    Run-RegCommand "ADD `"$backgroundPath\command`" /ve /d `"\`"$cursorExePath\`" \`"%V\`"`" /f"  # 设置命令
    Write-Host "Context menu for background installed successfully."  # 提示安装成功

    # 安装右键菜单项(文件夹)
    $folderPath = "HKEY_CLASSES_ROOT\Directory\shell\Open with Cursor"
    Run-RegCommand "ADD `"$folderPath`" /ve /d `"Open with Cursor`" /f"  # 添加主键
    Run-RegCommand "ADD `"$folderPath`" /v Icon /d `"$cursorExePath`" /f"  # 设置图标
    Run-RegCommand "ADD `"$folderPath\command`" /ve /d `"\`"$cursorExePath\`" \`"%1\`"`" /f"  # 设置命令
    Write-Host "Context menu for folders installed successfully."  # 提示安装成功

    # 安装右键菜单项(文件)
    $filePath = "HKEY_CLASSES_ROOT\*\shell\Open with Cursor"
    Run-RegCommand "ADD `"$filePath`" /ve /d `"Open with Cursor`" /f"  # 添加主键
    Run-RegCommand "ADD `"$filePath`" /v Icon /d `"$cursorExePath`" /f"  # 设置图标
    Run-RegCommand "ADD `"$filePath\command`" /ve /d `"\`"$cursorExePath\`" \`"%1\`"`" /f"  # 设置命令
    Write-Host "Context menu for files installed successfully."  # 提示安装成功

} else {
    Write-Host "Error: Cursor executable not found at $cursorExePath"  # 提示可执行文件未找到
}

Write-Host "Press any key to exit..."  # 提示按任意键退出
$null = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")  # 等待用户按键
复制代码

四、PowerShell 脚本说明

    1. 检查管理员权限脚本首先检查是否以管理员权限运行,如果没有,则重新以管理员权限启动自身。
    2. 定义 Cursor 可执行文件路径脚本定义了 Cursor 可执行文件的路径,这里假设 Cursor 安装在用户的本地应用程序数据中。
    3. 检查 Cursor 可执行文件是否存在在继续之前,脚本会检查 Cursor 可执行文件是否存在,确保后续操作不会出错。
    4. 定义执行注册表命令的函数Run-RegCommand 函数用于执行 reg.exe 命令,并检查命令是否成功执行。
    5. 安装右键菜单项脚本分别为文件夹背景、文件夹和文件添加右键菜单项,设置菜单项的显示名称、图标和关联的命令。
    6. 错误处理和退出如果 Cursor 可执行文件未找到,脚本会输出错误信息。最后,脚本等待用户按任意键后退出。

五、如何运行 PowerShell 脚本

    1. 复制脚本内容将上述脚本内容复制到一个文本编辑器中,并保存为 .ps1 文件,例如 Add-CursorRightClickMenu.ps1
    2. 以管理员身份运行 PowerShell在 Windows 开始菜单中搜索 PowerShell,右键点击并选择“以管理员身份运行”。
    3. 运行脚本在 PowerShell 窗口中,导航到脚本文件所在的目录,然后运行脚本,例如:
      cd "C:\path\to\your\script"
      .\Add-CursorRightClickMenu.ps1
    4. 按任意键退出脚本执行完成后,按任意键退出。

六、PowerShell 运行时报错处理

6.1、无法加载文件 C:\Users\zuoyang\Desktop\install-open-with-cursor.ps1,因为在此系统上禁止运行脚本。

1
2
3
4
5
PS C:\Users\zuoyang\Desktop> .\install-open-with-cursor.ps1 .\install-open-with-cursor.ps1 : 无法加载文件 C:\Users\zuoyang\Desktop\install-open-with-cursor.ps1,因为在此系统上禁止 运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。 所在位置 行:1 字符: 1
 
.\install-open-with-cursor.ps1
  + CategoryInfo          : SecurityError: (:) [],PSSecurityException
  + FullyQualifiedErrorId : UnauthorizedAccess

因为 PowerShell 的执行策略(Execution Policy)默认设置为不允许运行脚本。需要更改执行策略以允许运行本地编写的脚本。请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。

解决办法1,临时更改执行策略(仅对当前会话有效):

1. 以管理员身份打开 PowerShell:

1
按 `Win + X`,然后选择“Windows PowerShell (管理员)”或“终端 (管理员)”。

2. 设置执行策略为 `Bypass` 或 `Unrestricted`:

1
2
3
```powershell
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
```

或者

1
2
3
```powershell
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted
```

3. 重新运行你的脚本:

1
2
3
```powershell
.\install-open-with-cursor.ps1
```

解决办法2,永久更改执行策略(对所有会话有效)

1. 以管理员身份打开 PowerShell

1
- 按 `Win + X`,然后选择“Windows PowerShell (管理员)”或“终端 (管理员)”。

2. 设置执行策略为 `RemoteSigned` 或 `Unrestricted`:

1
2
3
```powershell
Set-ExecutionPolicy RemoteSigned
```

或者

1
2
3
```powershell
Set-ExecutionPolicy Unrestricted
```

3. 确认更改:

1
- 系统会提示你确认更改,输入 `Y` 或 `A` 进行确认。

4. 重新运行你的脚本:

1
2
3
```powershell
.\install-open-with-cursor.ps1
```

七、注意事项

- 安全性考虑:`Unrestricted` 策略允许运行所有脚本,包括从互联网下载的脚本,这可能会带来安全风险。建议使用 `RemoteSigned`,它只允许运行经过数字签名的远程脚本。
- 恢复默认策略:如果你只是临时需要运行脚本,可以使用 `-Scope Process` 参数来仅影响当前会话,避免全局更改。

八、验证执行策略

你可以通过以下命令查看当前的执行策略:

1
Get-ExecutionPolicy

九、卸载 PowerShell 脚本 添加的鼠标右键快捷方式

复制代码
# 检查当前脚本是否以管理员权限运行
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

# 如果没有以管理员权限运行,则重新启动脚本并请求管理员权限
if (-not $isAdmin) {
    Write-Host "此脚本需要管理员权限。正在以管理员权限重新启动..."
    Start-Process powershell.exe -Verb RunAs -ArgumentList ("-File", $MyInvocation.MyCommand.Path)
    exit  # 退出当前脚本
}

Write-Host "正在以管理员权限运行脚本。正在卸载 'Open with Cursor' 上下文菜单项..."

# 定义一个函数用于执行注册表命令,并处理命令执行失败的情况
function Run-RegCommand {
    param (
        [string]$command  # 参数:要执行的注册表命令
    )
    Write-Host "正在执行命令: reg.exe $command"
    # 使用 reg.exe 执行注册表命令,不打开新窗口并等待命令完成
    $process = Start-Process -FilePath "reg.exe" -ArgumentList $command -NoNewWindow -Wait -PassThru
    # 检查命令执行是否成功(通过检查进程退出代码)
    if ($process.ExitCode -ne 0) {
        Write-Host "执行失败: reg.exe $command"  # 输出错误信息
        exit 1  # 命令失败时退出脚本并返回错误代码 1
    }
}

# 定义删除背景右键菜单项的注册表路径
$backgroundPath = "HKEY_CLASSES_ROOT\Directory\Background\shell\Open with Cursor"
# 删除背景右键菜单中的“Open with Cursor”项
Run-RegCommand "DELETE `"$backgroundPath`" /f"
Write-Host "背景右键菜单项已成功删除。"  # 提示删除成功

# 定义删除文件夹右键菜单项的注册表路径
$folderPath = "HKEY_CLASSES_ROOT\Directory\shell\Open with Cursor"
# 删除文件夹右键菜单中的“Open with Cursor”项
Run-RegCommand "DELETE `"$folderPath`" /f"
Write-Host "文件夹右键菜单项已成功删除。"  # 提示删除成功

# 定义删除文件右键菜单项的注册表路径
$filePath = "HKEY_CLASSES_ROOT\*\shell\Open with Cursor"
# 删除文件右键菜单中的“Open with Cursor”项
Run-RegCommand "DELETE `"$filePath`" /f"
Write-Host "文件右键菜单项已成功删除。"  # 提示删除成功

# 通知用户所有上下文菜单项已成功删除
Write-Host "所有 'Open with Cursor' 上下文菜单项已成功删除。"

# 提示用户按任意键退出脚本
Write-Host "按任意键退出..."
$null = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")  # 等待用户按键
复制代码
posted @   左扬  阅读(355)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
levels of contents
点击右上角即可分享
微信分享提示