Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import ttk
- import subprocess
- import configparser
- import os
- class AliasButtonExample(tk.Tk):
- def __init__(self):
- super().__init__()
- self.geometry('1000x675')
- self.config(bg='green')
- self.title("Najeeb CMD ALIASES COMMANDS RUNNER")
- self.console_frame = ConsoleFrame(self)
- self.console_frame.grid(row=0, column=0, columnspan=3, padx=5, pady=5, sticky="nsew")
- self.aliases = {}
- self.create_widgets()
- def create_widgets(self):
- style = ttk.Style()
- style.configure("TButton", foreground="blue", background="lightgray", font=('Arial', 10, 'bold'))
- style.configure("TLabel", foreground="darkblue", background="lightgray", font=('Arial', 10))
- tk.Label(self, text="Command:").grid(row=1, column=0, padx=5, pady=5)
- tk.Label(self, text="Najeeb Aliases:").grid(row=3, column=0, sticky="n", padx=5, pady=5)
- self.command_entry = tk.Entry(self, width=100)
- self.command_entry.grid(row=1, column=1, padx=5, pady=5)
- self.run_button = ttk.Button(self, text="Run Command", command=self.run_command, style="TButton")
- self.run_button.grid(row=1, column=2, rowspan=1, padx=5, pady=5, sticky="nsew")
- self.aliases_frame = tk.Frame(self)
- self.aliases_frame.grid(row=4, column=0, columnspan=3, padx=5, pady=5, sticky="nsew")
- self.aliases_text = tk.Text(self.aliases_frame, wrap="word", state="normal", height=10, width=106, font=('Arial', 12))
- self.aliases_text.grid(row=0, column=0, padx=5, pady=5, sticky="nsew")
- self.aliases_scrollbar = tk.Scrollbar(self.aliases_frame, orient="vertical", command=self.aliases_text.yview)
- self.aliases_scrollbar.grid(row=0, column=1, sticky="ns")
- self.aliases_text.config(yscrollcommand=self.aliases_scrollbar.set)
- self.search_label = tk.Label(self, text="Search Aliases:")
- self.search_label.grid(row=5, column=0, padx=5, pady=5, sticky="e")
- self.search_entry = tk.Entry(self, width=100)
- self.search_entry.grid(row=5, column=1, padx=5, pady=5)
- self.search_button = ttk.Button(self, text="Search", command=self.search_aliases, style="TButton")
- self.search_button.grid(row=5, column=2, padx=5, pady=5)
- self.save_button = ttk.Button(self, text="Save Console Text", command=self.save_console_text, style="TButton")
- self.save_button.grid(row=3, column=1, padx=5, pady=5)
- self.grid_columnconfigure(1, weight=1)
- self.read_aliases()
- def run_command(self):
- command = self.command_entry.get()
- process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- stdout, stderr = process.communicate()
- output = stdout.decode()
- error = stderr.decode()
- self.console_frame.console_window.append_text(output)
- if error:
- self.console_frame.console_window.append_text(error, tag="error")
- def read_aliases(self):
- try:
- config = configparser.ConfigParser()
- config.read("Aliases.ini")
- for section in config.sections():
- for option in config.options(section):
- try:
- command = config.get(section, option)
- alias = option
- # Custom parsing for Windows CMD syntax
- command = self.parse_cmd_command(command)
- self.aliases[alias] = command
- self.aliases_text.insert("end", f"{alias}: {command}\n")
- except configparser.InterpolationSyntaxError as e:
- print(f"Error reading alias '{option}' in section '{section}': {e}")
- except configparser.Error as e:
- print(f"Error reading configuration file: {e}")
- def parse_cmd_command(self, command):
- # Replace %var% with os.environ['var']
- command = os.path.expandvars(command)
- # Replace %var:~x,y% with proper slicing
- command = self.parse_cmd_substring(command)
- return command
- def parse_cmd_substring(self, command):
- # Implement custom logic to handle substring expansion
- # For simplicity, I'll provide a basic implementation for date and time expansion
- # Date expansion
- command = command.replace('%date%', self.get_current_date())
- # Time expansion
- command = command.replace('%time%', self.get_current_time())
- return command
- def get_current_date(self):
- return os.popen('echo %date%').read().strip()
- def get_current_time(self):
- return os.popen('echo %time%').read().strip()
- def search_aliases(self):
- query = self.search_entry.get().strip().lower()
- self.aliases_text.delete('1.0', tk.END)
- if query:
- for alias, command in self.aliases.items():
- if query in alias.lower() or query in command.lower():
- self.aliases_text.insert(tk.END, f"{alias}: {command}\n")
- def save_console_text(self):
- text_to_save = self.console_frame.console_window.get_text()
- with open("console_output.txt", "w") as file:
- file.write(text_to_save)
- class ConsoleFrame(tk.Frame):
- def __init__(self, parent):
- super().__init__(parent)
- self.console_window = ConsoleWindow(self)
- self.console_window.pack(expand=True, fill="both")
- self.clear_button = ttk.Button(self, text="Clear Console", command=self.clear_console)
- self.clear_button.pack()
- def clear_console(self):
- self.console_window.clear_text()
- class ConsoleWindow(tk.Frame):
- def __init__(self, parent):
- super().__init__(parent)
- self.text_area = tk.Text(self, wrap="word", state="normal", height=15, width=100, font=('Arial', 12))
- self.text_area.pack(expand=True, fill="both")
- self.scrollbar = tk.Scrollbar(self, command=self.text_area.yview)
- self.scrollbar.pack(side="right", fill="y")
- self.text_area.config(yscrollcommand=self.scrollbar.set)
- # Bind the text widget to automatically copy selected text to the clipboard
- self.text_area.bind("<Control-c>", self.copy_to_clipboard)
- def append_text(self, text, tag=None):
- self.text_area.config(state="normal")
- if tag:
- self.text_area.insert("end", text, tag)
- else:
- self.text_area.insert("end", text)
- self.text_area.config(state="disabled")
- self.text_area.see("end")
- def clear_text(self):
- self.text_area.config(state="normal")
- self.text_area.delete("1.0", "end")
- self.text_area.config(state="disabled")
- def get_text(self):
- return self.text_area.get("1.0", "end-1c")
- def copy_to_clipboard(self, event=None):
- self.clipboard_clear()
- text = self.text_area.get(tk.SEL_FIRST, tk.SEL_LAST)
- self.clipboard_append(text)
- if __name__ == "__main__":
- app = AliasButtonExample()
- app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement