我尝试将舰队 IDE 添加到我的上下文菜单中以打开文件夹,我尝试使用 bat、powershell 和 python 编写注册表项,但没有成功。
这显示在注册表中,但不运行该应用程序
@echo off
setlocal enabledelayedexpansion
:: Function to check if the script is running as administrator
:check_admin
net session >nul 2>&1
if %errorLevel% neq 0 (
echo Running without administrative privileges. Reopening with elevated privileges...
set "script_path=%~0"
set "params=%*"
set "batch_cmd=^
set params=!params:*%~0=!
set params=!params:~1!
call "%~0" !params! %*
exit /b
"
powershell Start-Process -Verb RunAs -FilePath "cmd.exe" -ArgumentList "/c %batch_cmd%"
exit /b
)
exit /b
:: Rest of your script here
:main
:: Your existing script logic goes here
:: Call the check_admin function at the beginning
call :check_admin
:: Continue with the main script logic
echo "This script is running with administrative privileges."
:: Define paths and menu text
set "menu_text=Open with Fleet"
set "app_path=C:\Users\dower\AppData\Local\Programs\Fleet\Fleet.exe"
set "icon_path=C:\Users\dower\AppData\Local\Programs\Fleet\Fleet.ico"
:: Function to add a new context menu entry for folders
:add_context_menu
reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\%menu_text%" /ve /d "%menu_text%" /f
reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\%menu_text%\command" /ve /d "\"%app_path%\" --dir \"%%1\"" /f
reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\%menu_text%\Icon" /ve /d "%app_path%,0" /f
exit /b
:: Function to remove the existing context menu entry
:remove_context_menu
reg delete "HKEY_CLASSES_ROOT\Directory\Background\shell\%menu_text%" /f
exit /b
:: Main script
:: Check if the existing entry needs to be removed
for /f "tokens=* delims=" %%a in ('reg query "HKEY_CLASSES_ROOT\Directory\Background\shell" ^| find /i "%menu_text%"') do (
set "existing_entry=%%a"
)
if defined existing_entry (
call :remove_context_menu
)
:: Add a new context menu entry
call :add_context_menu
echo Successfully added '%menu_text%' context menu entry for folders.
如果我能得到蝙蝠或其他语言的指针以最好地实现这一目标
https://github.com/samfisherirl/Fleet-IDE-context-menu-Open-Folder-With
import os
import sys
import winreg
import ctypes
import time
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def run_as_admin():
ctypes.windll.shell32.ShellExecuteW(
None, "runas", sys.executable, " ".join(sys.argv), None, 1
)
# Define paths and menu text
menu_text = "Open with Fleet"
appdata_local_path = os.path.join(os.getenv('LOCALAPPDATA'), 'Programs')
# Specify the rest of the path to your executable
executable_path = 'Fleet\Fleet.exe'
# Combine the paths
app_path = os.path.join(appdata_local_path, executable_path)
def add_context_menu(location):
"""Adds a new context menu entry for folders."""
# Open registry key
key_path = f"Directory\\{location}\\Open with Fleet"
key = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, key_path)
subkey = winreg.CreateKey(key, "command")
# Set default values
winreg.SetValueEx(key, 'Icon', 0, winreg.REG_SZ, f"\"{app_path}\",0")
winreg.SetValueEx(key, None, 0, winreg.REG_SZ, menu_text)
winreg.SetValueEx(subkey, None, 0, winreg.REG_SZ, f"\"{app_path}\" --dir \"%V\"")
# Close registry keys
winreg.CloseKey(subkey)
winreg.CloseKey(key)
def remove_context_menu(location):
"""Removes the existing context menu entry."""
key_path = f"Directory\\{location}\\Open with Fleet"
try:
# Delete the registry key and its subkeys
winreg.DeleteKey(winreg.HKEY_CLASSES_ROOT, key_path)
except FileNotFoundError:
print(f"Registry key not found: {key_path}")
except Exception as e:
print(f"Error removing context menu entry: {e}")
# Check if running as admin
if not is_admin():
# Re-run the script as admin
run_as_admin()
sys.exit()
# Remove existing entries
remove_context_menu("Background\\shell")
remove_context_menu("shell")
# Add new context menu entry
add_context_menu("Background\\shell")
add_context_menu("shell")
print(f"Successfully added '{menu_text}' context menu entry for folders.")
time.sleep(3)