Advertisement
Najeebsk

CMDER.pyw

Mar 29th, 2024
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.21 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk
  3. import subprocess
  4. import configparser
  5. import os
  6.  
  7.  
  8. class AliasButtonExample(tk.Tk):
  9.     def __init__(self):
  10.         super().__init__()
  11.         self.geometry('1000x675')
  12.         self.config(bg='green')
  13.         self.title("Najeeb CMD ALIASES COMMANDS RUNNER")
  14.  
  15.         self.console_frame = ConsoleFrame(self)
  16.         self.console_frame.grid(row=0, column=0, columnspan=3, padx=5, pady=5, sticky="nsew")
  17.  
  18.         self.aliases = {}
  19.  
  20.         self.create_widgets()
  21.  
  22.     def create_widgets(self):
  23.         style = ttk.Style()
  24.         style.configure("TButton", foreground="blue", background="lightgray", font=('Arial', 10, 'bold'))
  25.         style.configure("TLabel", foreground="darkblue", background="lightgray", font=('Arial', 10))
  26.  
  27.         tk.Label(self, text="Command:").grid(row=1, column=0, padx=5, pady=5)
  28.         tk.Label(self, text="Najeeb Aliases:").grid(row=3, column=0, sticky="n", padx=5, pady=5)
  29.  
  30.         self.command_entry = tk.Entry(self, width=100)
  31.         self.command_entry.grid(row=1, column=1, padx=5, pady=5)
  32.  
  33.         self.run_button = ttk.Button(self, text="Run Command", command=self.run_command, style="TButton")
  34.         self.run_button.grid(row=1, column=2, rowspan=1, padx=5, pady=5, sticky="nsew")
  35.  
  36.         self.aliases_frame = tk.Frame(self)
  37.         self.aliases_frame.grid(row=4, column=0, columnspan=3, padx=5, pady=5, sticky="nsew")
  38.  
  39.         self.aliases_text = tk.Text(self.aliases_frame, wrap="word", state="normal", height=10, width=106, font=('Arial', 12))
  40.         self.aliases_text.grid(row=0, column=0, padx=5, pady=5, sticky="nsew")
  41.  
  42.         self.aliases_scrollbar = tk.Scrollbar(self.aliases_frame, orient="vertical", command=self.aliases_text.yview)
  43.         self.aliases_scrollbar.grid(row=0, column=1, sticky="ns")
  44.         self.aliases_text.config(yscrollcommand=self.aliases_scrollbar.set)
  45.  
  46.         self.search_label = tk.Label(self, text="Search Aliases:")
  47.         self.search_label.grid(row=5, column=0, padx=5, pady=5, sticky="e")
  48.  
  49.         self.search_entry = tk.Entry(self, width=100)
  50.         self.search_entry.grid(row=5, column=1, padx=5, pady=5)
  51.  
  52.         self.search_button = ttk.Button(self, text="Search", command=self.search_aliases, style="TButton")
  53.         self.search_button.grid(row=5, column=2, padx=5, pady=5)
  54.  
  55.         self.save_button = ttk.Button(self, text="Save Console Text", command=self.save_console_text, style="TButton")
  56.         self.save_button.grid(row=3, column=1, padx=5, pady=5)
  57.  
  58.         self.grid_columnconfigure(1, weight=1)
  59.  
  60.         self.read_aliases()
  61.  
  62.     def run_command(self):
  63.         command = self.command_entry.get().strip()
  64.         if command in self.aliases:
  65.             command = self.aliases[command]
  66.         process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  67.         stdout, stderr = process.communicate()
  68.         output = stdout.decode()
  69.         error = stderr.decode()
  70.         self.console_frame.console_window.append_text(output)
  71.         if error:
  72.             self.console_frame.console_window.append_text(error, tag="error")
  73.  
  74.     def read_aliases(self):
  75.         try:
  76.             config = configparser.ConfigParser()
  77.             config.read(r"C:\Users\Najeeb\Desktop\CMDER\Aliases.ini")
  78.             for section in config.sections():
  79.                 for option in config.options(section):
  80.                     try:
  81.                         command = config.get(section, option)
  82.                         self.aliases[option] = command
  83.                         self.aliases_text.insert("end", f"{option}: {command}\n")
  84.                     except configparser.InterpolationSyntaxError as e:
  85.                         print(f"Error reading alias '{option}' in section '{section}': {e}")
  86.         except configparser.Error as e:
  87.             print(f"Error reading configuration file: {e}")
  88.  
  89.     def search_aliases(self):
  90.         query = self.search_entry.get().strip().lower()
  91.         self.aliases_text.delete('1.0', tk.END)
  92.         if query:
  93.             for alias, command in self.aliases.items():
  94.                 if query in alias.lower() or query in command.lower():
  95.                     self.aliases_text.insert(tk.END, f"{alias}: {command}\n")
  96.  
  97.     def save_console_text(self):
  98.         text_to_save = self.console_frame.console_window.get_text()
  99.         with open("console_output.txt", "w") as file:
  100.             file.write(text_to_save)
  101.  
  102.  
  103. class ConsoleFrame(tk.Frame):
  104.     def __init__(self, parent):
  105.         super().__init__(parent)
  106.         self.console_window = ConsoleWindow(self)
  107.         self.console_window.pack(expand=True, fill="both")
  108.         self.clear_button = ttk.Button(self, text="Clear Console", command=self.clear_console)
  109.         self.clear_button.pack()
  110.  
  111.     def clear_console(self):
  112.         self.console_window.clear_text()
  113.  
  114.  
  115. class ConsoleWindow(tk.Frame):
  116.     def __init__(self, parent):
  117.         super().__init__(parent)
  118.         self.text_area = tk.Text(self, wrap="word", state="normal", height=15, width=100, font=('Arial', 12))
  119.         self.text_area.pack(expand=True, fill="both")
  120.         self.scrollbar = tk.Scrollbar(self, command=self.text_area.yview)
  121.         self.scrollbar.pack(side="right", fill="y")
  122.         self.text_area.config(yscrollcommand=self.scrollbar.set)
  123.  
  124.         # Bind the text widget to automatically copy selected text to the clipboard
  125.         self.text_area.bind("<Control-c>", self.copy_to_clipboard)
  126.  
  127.     def append_text(self, text, tag=None):
  128.         self.text_area.config(state="normal")
  129.         if tag:
  130.             self.text_area.insert("end", text, tag)
  131.         else:
  132.             self.text_area.insert("end", text)
  133.         self.text_area.config(state="disabled")
  134.         self.text_area.see("end")
  135.  
  136.     def clear_text(self):
  137.         self.text_area.config(state="normal")
  138.         self.text_area.delete("1.0", "end")
  139.         self.text_area.config(state="disabled")
  140.  
  141.     def get_text(self):
  142.         return self.text_area.get("1.0", "end-1c")
  143.  
  144.     def copy_to_clipboard(self, event=None):
  145.         self.clipboard_clear()
  146.         text = self.text_area.get(tk.SEL_FIRST, tk.SEL_LAST)
  147.         self.clipboard_append(text)
  148.  
  149.  
  150. if __name__ == "__main__":
  151.     app = AliasButtonExample()
  152.     app.mainloop()
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement