wldasf.github.io

Python Clipboard

Mar 6, 2022

The script allows users to save multiple keywords and access them on-demand. it uses pyperclip to paste the words stored in sys.argv into a file called mcb.

Listing the keywords pastes the content of file mcb into the clipboard.

['third', 'fourth', 'second', 'first']

Code Breakdown:

sys.argv is a list in Python, which contains the command-line arguments passed to the script. The sys.argv list always has the name of the script file on index 0. When the user calls the script with “save keyword” an if statement checks the length of sys.argv if it’s 3 (length starts from 1, index starts from 0). name of the file will be at index 0, save at index 1 and the keyword at index 2. In that case pyperclip will paste the keyword in index 2 to the file mcb.

delete has similar code to save but instead of pasting to mcb it deletes the keyword from the file.

If sys.argv only receives a command and no keyword, check if it’s list or clear. list will copy all keywords from the mcb file to the clipboard by converting all keywords from list to string.

clear will loop over all keywords in the file and delete them one by one.

And finally, if the user wants a saved keyword pasted into the clipboard they can type py mcb.pyw keyword.