add shortcut

@echo off

%1 %2
ver|find "5.">nul&&goto :Admin
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :Admin","","runas",1)(window.close)&goto :eof

:Admin
set current_dir=%~dp0
%~d0
cd %~dp0

python bin/add_shortcut_menu.py --config=add

echo "Install successful"

pause

@echo off

%1 %2
ver|find "5.">nul&&goto :Admin
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :Admin","","runas",1)(window.close)&goto :eof

:Admin
set current_dir=%~dp0
%~d0
cd %~dp0

python bin/add_shortcut_menu.py --config=delete

echo Uninstall Successful

pause

"""
Created by gaoxing on 2021.3.3
Open the shortcut by click the mouse's right button
"""

import argparse
import traceback
import os
from sys import version_info
import sys
import ctypes


if version_info.major == 3:
import winreg as reg
else:
import _winreg as reg


# the shortcut name
menu_name = 'ZipPkt'


def add_shortcut_menu():
"""Add a shortcut key for all of the file while click the right button of the mouse"""
# The path of the shortcut
menu_command = os.getcwd() + os.sep + 'bin' + os.sep + 'ZipPkt.exe'
print(menu_command)
# Set the path of registry
key = reg.OpenKey(reg.HKEY_CLASSES_ROOT, r'*\\shell')
# Set the shortcut keys
reg.SetValue(key, menu_name, reg.REG_SZ, menu_name + '(&A)')
sub_key = reg.OpenKey(key, menu_name)
# Set the execute line, menu_command + ' %v', such as atrace2trace.exe atrace
reg.SetValue(sub_key, 'command', reg.REG_SZ, menu_command + ' "%v"')
# Added by gaoxing on 2021.5.11, set the icon
icon_key = reg.OpenKeyEx(reg.HKEY_CLASSES_ROOT, r'*\\shell\\' + menu_name, 0, reg.KEY_WRITE)
reg.SetValueEx(icon_key, "icon", 0, reg.REG_SZ, menu_command)
# close the subkey and key
reg.CloseKey(sub_key)
reg.CloseKey(key)
reg.CloseKey(icon_key)

 

def delete_shortcut_menu():
"""Delete the shortcut key which added before"""
try:
key = reg.OpenKey(reg.HKEY_CLASSES_ROOT, r'*\\shell')
menu_key = reg.OpenKey(key, menu_name)
reg.DeleteKey(menu_key, 'command')
reg.DeleteKey(key, menu_name)
except Exception as e:
print(traceback.print_exc())

 

def check_permission():
""" Check if got the permission of admin """
try:
ctypes.windll.shell32.IsUserAnAdmin()
except:
return False


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Added shortcut key menu')
parser.add_argument('--config', default='add', help='The operation to add or delete shortcut key')
args = parser.parse_args()
if args.config == 'add':
add_shortcut_menu()
else:
delete_shortcut_menu()

 

posted @ 2024-03-23 23:05  乐观的知觉  阅读(12)  评论(0编辑  收藏  举报