start/skrotyApp.py

105 lines
3.8 KiB
Python

#!/usr/bin/env python3
import os
import tkinter as tk
from tkinter import filedialog, messagebox
import subprocess
def browse_exec():
path = filedialog.askopenfilename(title="Wybierz plik wykonywalny (AppImage/skrypt)")
if path:
entry_exec.delete(0, tk.END)
entry_exec.insert(0, path)
def browse_icon():
path = filedialog.askopenfilename(title="Wybierz ikonę", filetypes=[("Pliki graficzne", "*.png *.svg *.ico *.jpg *.jpeg")])
if path:
entry_icon.delete(0, tk.END)
entry_icon.insert(0, path)
def choose_location():
location = var_location.get()
if location == "local":
return os.path.expanduser("~/.local/share/applications")
elif location == "system":
return "/usr/share/applications"
elif location == "desktop":
return os.path.join(os.path.expanduser("~"), "Pulpit")
else:
return os.path.expanduser("~")
def create_desktop_entry():
name = entry_name.get().strip()
exec_path = entry_exec.get().strip()
icon_path = entry_icon.get().strip()
terminal = var_terminal.get()
if not name or not exec_path:
messagebox.showerror("Błąd", "Nazwa aplikacji i ścieżka do pliku wykonywalnego są wymagane.")
return
terminal_value = "true" if terminal else "false"
desktop_content = f"""[Desktop Entry]
Type=Application
Name={name}
Exec="{exec_path}"
{"Icon=" + icon_path if icon_path else ""}
Terminal={terminal_value}
"""
filename = name.lower().replace(" ", "_") + ".desktop"
folder = choose_location()
desktop_path = os.path.join(folder, filename)
try:
if folder.startswith("/usr"):
# Zapis przez pkexec do katalogu systemowego
tmpfile = f"/tmp/{filename}"
with open(tmpfile, "w") as f:
f.write(desktop_content)
subprocess.run(["pkexec", "cp", tmpfile, desktop_path], check=True)
subprocess.run(["pkexec", "chmod", "755", desktop_path], check=True)
os.remove(tmpfile)
else:
os.makedirs(folder, exist_ok=True)
with open(desktop_path, 'w') as f:
f.write(desktop_content)
os.chmod(desktop_path, 0o755)
messagebox.showinfo("Sukces", f"Skrót utworzony: {desktop_path}")
except Exception as e:
messagebox.showerror("Błąd", f"Nie udało się zapisać skrótu:\n{e}")
# GUI
root = tk.Tk()
root.title("Tworzenie skrótu .desktop")
tk.Label(root, text="Nazwa aplikacji:").grid(row=0, column=0, sticky="e")
entry_name = tk.Entry(root, width=40)
entry_name.grid(row=0, column=1, pady=2)
tk.Label(root, text="Ścieżka do pliku:").grid(row=1, column=0, sticky="e")
entry_exec = tk.Entry(root, width=40)
entry_exec.grid(row=1, column=1, pady=2)
tk.Button(root, text="Przeglądaj", command=browse_exec).grid(row=1, column=2)
tk.Label(root, text="Ikona (opcjonalnie):").grid(row=2, column=0, sticky="e")
entry_icon = tk.Entry(root, width=40)
entry_icon.grid(row=2, column=1, pady=2)
tk.Button(root, text="Przeglądaj", command=browse_icon).grid(row=2, column=2)
var_terminal = tk.BooleanVar()
tk.Checkbutton(root, text="Uruchamiane w terminalu", variable=var_terminal).grid(row=3, column=1, pady=4)
tk.Label(root, text="Miejsce zapisu:").grid(row=4, column=0, sticky="e")
var_location = tk.StringVar(value="local")
tk.Radiobutton(root, text="Lokalnie (~/.local/share/applications)", variable=var_location, value="local").grid(row=4, column=1, sticky="w")
tk.Radiobutton(root, text="Systemowo (/usr/share/applications)", variable=var_location, value="system").grid(row=5, column=1, sticky="w")
tk.Radiobutton(root, text="Pulpit", variable=var_location, value="desktop").grid(row=6, column=1, sticky="w")
tk.Button(root, text="Utwórz skrót", command=create_desktop_entry, bg="#4CAF50", fg="white").grid(row=7, column=1, pady=10)
root.mainloop()