Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: clipboard_manager.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- This script provides a simple clipboard manager for managing clipboard operations on Windows systems.
- Requirements:
- - Python 3.x
- - pyperclip library (install via pip: pip install pyperclip)
- Usage:
- 1. Run the script in a Python environment.
- 2. Follow the prompts to perform clipboard operations.
- Functions:
- 1. get_clipboard(): Retrieves the content of the clipboard.
- 2. set_clipboard(content): Sets the content of the clipboard to the specified value.
- 3. clear_clipboard_history(): Clears the Windows clipboard history.
- 4. view_clipboard_history(): Simulates Win + V to open the clipboard history menu.
- 5. main(): Provides a command-line interface for interacting with the clipboard manager.
- Additional Notes:
- - This script is designed for Windows systems.
- - Ensure that the pyperclip library is installed before running the script.
- - The clipboard history feature relies on the Windows clipboard history functionality.
- - Use this script to conveniently manage clipboard content without navigating through system menus.
- """
- import pyperclip
- import subprocess
- import os
- import sys
- import ctypes
- def get_clipboard():
- """Retrieve the content of the clipboard."""
- return pyperclip.paste()
- def set_clipboard(content):
- """Set the content of the clipboard to the specified value."""
- pyperclip.copy(content)
- def clear_clipboard_history():
- """Clears the Windows clipboard history."""
- command = 'Restart-Service -Name "cbdhsvc*" -Force'
- try:
- subprocess.run(['powershell', '-Command', command], check=True)
- print("\nClipboard history cleared successfully!\n")
- except subprocess.CalledProcessError as e:
- print(f"\nError:\n{e}\n")
- def view_clipboard_history():
- """Simulates Win + V to open the clipboard history menu."""
- ctypes.windll.user32.keybd_event(0x5B, 0, 0, 0) # Press Win key
- ctypes.windll.user32.keybd_event(0x56, 0, 0, 0) # Press V key
- ctypes.windll.user32.keybd_event(0x56, 0, 2, 0) # Release V key
- ctypes.windll.user32.keybd_event(0x5B, 0, 2, 0) # Release Win key
- def main():
- """
- Provides a command-line interface for interacting with the clipboard manager.
- Displays a menu of available actions and prompts the user to make a selection.
- The function executes the corresponding action based on the user's choice.
- Actions:
- - 1. Get clipboard content: Retrieves and displays the current content of the clipboard.
- - 2. Set clipboard content: Prompts the user to enter new content to set in the clipboard.
- - 3. View clipboard history: Simulates the Win + V keyboard shortcut to open the clipboard history menu.
- - 4. Clear clipboard history: Clears the Windows clipboard history using PowerShell.
- - 5. Exit the program: Exits the clipboard manager application.
- Note:
- - The user's input is validated to ensure it corresponds to one of the available options.
- - Invalid input prompts the user to select a valid option.
- - Upon selecting an action, the function executes the corresponding operation and provides feedback to the user.
- - The program terminates upon selecting the "Exit" option.
- """
- print("\t:: Welcome to Clipboard Manager ::\n\n")
- print("Select an action:\n")
- print("\t1. Get clipboard content")
- print("\t2. Set clipboard content")
- print("\t3. View clipboard history")
- print("\t4. Clear clipboard history")
- print("\t5. Exit the program")
- choice = input("\n\tEnter your selection (1-5): ")
- if choice == '1':
- clipboard_content = get_clipboard()
- print("\nClipboard content:\n", clipboard_content)
- elif choice == '2':
- new_content = input("\nEnter the new content for the clipboard: ")
- set_clipboard(new_content)
- print("\nClipboard content set successfully!\n")
- elif choice == '3':
- print("\nSimulating Win + V to Open the clipboard history...\n")
- view_clipboard_history()
- elif choice == '4':
- print("\nClearing the clipboard history...\n")
- clear_clipboard_history()
- elif choice == '5':
- print("\nExiting the program...\tGoodbye!\n")
- sys.exit()
- else:
- print("\nInvalid choice!\nPlease select a valid option (1, 2, 3, 4 or 5)\n")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement