右键添加sublime快捷方式

本文介绍了一种使用Python脚本自动搜索Sublime Text安装位置,并将该应用添加到Windows注册表的方法,以便用户可以通过右键菜单快速启动Sublime Text。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.使用python添加注册表key。

# -*- coding: utf-8 -*-
"""
Created on Sat May  1 00:18:14 2021

@author: weiyi
"""

import os
import win32api
import win32con

DIR = []
DIR.append(u'C:\Program Files')
DIR.append(u'C:\Program Files (x86)')
DIR.append(u'D:\Program Files')
DIR.append(u'D:\Program Files (x86)')
DIR.append(u'E:\Program Files')
DIR.append(u'E:\Program Files (x86)')
DIR.append(u'F:\Program Files')
DIR.append(u'F:\Program Files (x86)')

name = 'sublime_text.exe'

def search(path,name):
    depth = 0
    for root,dirs,files in os.walk(path):
        depth = depth + 1
        if depth == 3:
            break
        for f in files:
            if f.endswith(name):
                fullname = os.path.join(root, f)
                return fullname
            pass
        pass
    pass

for d in DIR:
    print('start search',d)
    r = search(d, name)
    if r != None:
        break
    

dir_str = ''
if r != None:
    print('find',name,r)
    print('Use this path:(Y/N)')
    use = input()
    if 'Y' in use or 'yes' in use or'YES' in use or 'y' in use or 'Yes' in use:
        dir_str = r
        

if r == None or dir_str == '':
    print('find no result',name,'input path:')
    dir_str = input()

if dir_str != '':
    
    key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT,'*\\shell',0,win32con.KEY_ALL_ACCESS)
    win32api.RegCreateKey(key,'sublime')
    win32api.RegCloseKey(key)
    
    
    key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT,'*\\shell\\sublime',0,win32con.KEY_ALL_ACCESS)
    win32api.RegSetValueEx(key,'Icon',0,win32con.REG_SZ,dir_str+',0')
    win32api.RegSetValue(key,'command',win32con.REG_SZ,dir_str+' %1')                      
    win32api.RegCloseKey(key)
    
    key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT,'\\directory\\shell',0,win32con.KEY_ALL_ACCESS)
    win32api.RegCreateKey(key,'用sublime打开')
    win32api.RegCloseKey(key)
                              
    key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT,'\\directory\\shell\\用sublime打开',0,win32con.KEY_ALL_ACCESS)
    win32api.RegSetValueEx(key,'Icon',0,win32con.REG_SZ,dir_str+',0')
    win32api.RegSetValue(key,'command',win32con.REG_SZ,dir_str+' %1')                      
    win32api.RegCloseKey(key)                    
    

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值