Advertisement
Python253

clipboard_manager

Apr 30th, 2024
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.42 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: clipboard_manager.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script provides a simple clipboard manager for managing clipboard operations on Windows systems.
  9.  
  10. Requirements:
  11. - Python 3.x
  12. - pyperclip library (install via pip: pip install pyperclip)
  13.  
  14. Usage:
  15. 1. Run the script in a Python environment.
  16. 2. Follow the prompts to perform clipboard operations.
  17.  
  18. Functions:
  19. 1. get_clipboard(): Retrieves the content of the clipboard.
  20. 2. set_clipboard(content): Sets the content of the clipboard to the specified value.
  21. 3. clear_clipboard_history(): Clears the Windows clipboard history.
  22. 4. view_clipboard_history(): Simulates Win + V to open the clipboard history menu.
  23. 5. main(): Provides a command-line interface for interacting with the clipboard manager.
  24.  
  25. Additional Notes:
  26. - This script is designed for Windows systems.
  27. - Ensure that the pyperclip library is installed before running the script.
  28. - The clipboard history feature relies on the Windows clipboard history functionality.
  29. - Use this script to conveniently manage clipboard content without navigating through system menus.
  30. """
  31.  
  32. import pyperclip
  33. import subprocess
  34. import os
  35. import sys
  36. import ctypes
  37.  
  38. def get_clipboard():
  39.     """Retrieve the content of the clipboard."""
  40.     return pyperclip.paste()
  41.  
  42. def set_clipboard(content):
  43.     """Set the content of the clipboard to the specified value."""
  44.     pyperclip.copy(content)
  45.  
  46. def clear_clipboard_history():
  47.     """Clears the Windows clipboard history."""
  48.     command = 'Restart-Service -Name "cbdhsvc*" -Force'
  49.     try:
  50.         subprocess.run(['powershell', '-Command', command], check=True)
  51.         print("\nClipboard history cleared successfully!\n")
  52.     except subprocess.CalledProcessError as e:
  53.         print(f"\nError:\n{e}\n")
  54.  
  55. def view_clipboard_history():
  56.     """Simulates Win + V to open the clipboard history menu."""
  57.     ctypes.windll.user32.keybd_event(0x5B, 0, 0, 0)  # Press Win key
  58.     ctypes.windll.user32.keybd_event(0x56, 0, 0, 0)  # Press V key
  59.     ctypes.windll.user32.keybd_event(0x56, 0, 2, 0)  # Release V key
  60.     ctypes.windll.user32.keybd_event(0x5B, 0, 2, 0)  # Release Win key
  61.    
  62. def main():
  63.     """
  64.    Provides a command-line interface for interacting with the clipboard manager.
  65.    
  66.    Displays a menu of available actions and prompts the user to make a selection.
  67.    The function executes the corresponding action based on the user's choice.
  68.    
  69.    Actions:
  70.    - 1. Get clipboard content: Retrieves and displays the current content of the clipboard.
  71.    - 2. Set clipboard content: Prompts the user to enter new content to set in the clipboard.
  72.    - 3. View clipboard history: Simulates the Win + V keyboard shortcut to open the clipboard history menu.
  73.    - 4. Clear clipboard history: Clears the Windows clipboard history using PowerShell.
  74.    - 5. Exit the program: Exits the clipboard manager application.
  75.    
  76.    Note:
  77.    - The user's input is validated to ensure it corresponds to one of the available options.
  78.    - Invalid input prompts the user to select a valid option.
  79.    - Upon selecting an action, the function executes the corresponding operation and provides feedback to the user.
  80.    - The program terminates upon selecting the "Exit" option.
  81.    """
  82.     print("\t:: Welcome to Clipboard Manager ::\n\n")
  83.     print("Select an action:\n")
  84.     print("\t1. Get clipboard content")
  85.     print("\t2. Set clipboard content")
  86.     print("\t3. View clipboard history")
  87.     print("\t4. Clear clipboard history")
  88.     print("\t5. Exit the program")
  89.  
  90.     choice = input("\n\tEnter your selection (1-5): ")
  91.  
  92.     if choice == '1':
  93.         clipboard_content = get_clipboard()
  94.         print("\nClipboard content:\n", clipboard_content)
  95.     elif choice == '2':
  96.         new_content = input("\nEnter the new content for the clipboard: ")
  97.         set_clipboard(new_content)
  98.         print("\nClipboard content set successfully!\n")
  99.     elif choice == '3':
  100.         print("\nSimulating Win + V to Open the clipboard history...\n")
  101.         view_clipboard_history()
  102.     elif choice == '4':
  103.         print("\nClearing the clipboard history...\n")
  104.         clear_clipboard_history()
  105.     elif choice == '5':
  106.         print("\nExiting the program...\tGoodbye!\n")
  107.         sys.exit()
  108.     else:
  109.         print("\nInvalid choice!\nPlease select a valid option (1, 2, 3, 4 or 5)\n")
  110.  
  111. if __name__ == "__main__":
  112.     main()
  113.  
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement