一、使用Python+os将应用程序添加进Linux登录项
import os
def add_to_startup(file_path):
home_dir = os.path.expanduser("~")
with open(home_dir + "/.bashrc", "a") as f:
f.write("python " + file_path + "\n")
add_to_startup('~/myapp.py')
二、使用Python+os/winreg将应用程序添加进Windows登录项
import winreg
import os
def add_to_startup(file_path):
key = winreg.HKEY_CURRENT_USER
key_value = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
# 打开键
open_key = winreg.OpenKey(key, key_value,0,winreg.KEY_ALL_ACCESS)
# 设置值和对应的数据
winreg.SetValueEx(open_key, "MyApp", 0, winreg.REG_SZ, file_path)
winreg.CloseKey(open_key)
add_to_startup('d:\\software\\abc.exe')
三、使用Python+subprocess将应用程序添加进MacOS登录项
import subprocess
def add_to_login_item(app_path):
script = f'tell application "System Events" to make login item at end with properties {{path:"{app_path}", hidden:false}}'
subprocess.call(['osascript','-e', script])
add_to_login_item('/Applications/WeChat.app')