0

I am currently working on the Book Automate the Boring Stuff Chapter 9 the Practice Project Extending the Multi-Clipboard.

Code:

#! python3
# mcb.pyw - Saves and loads peices of text to the clipboard
# Usage: py.exe mcb.pyw save <keywoard> - Saves clipboard to key keywoard
#       py.exe mcb.pyw <keywoard> - Loads keywoards to clipboard
#       py.exe mcb.pyw list - Loads all keywoards to clipboard

import shelve
import pyperclip
import sys

mcbShelf = shelve.open('mcb')
helpList = []
# Save clipboard content
if len(sys.argv) == 3:
    if sys.argv[1].lower == 'save':
        mcbShelf[sys.argv[2]] = pyperclip.paste()
    elif sys.argv[1].lower == 'delete':
        if sys.argv[2] == 'all':
            for key, balue in mcbShelf.items():
                helpList.append(key)

            for i in range(len(helpList)):
                del mcbShelf[helpList[i]]

        if sys.argv[2] in mcbShelf:
            del mcbShelf[sys.argv[2]]

    elif sys.argv[1].lower == 'add':
        mcbShelf[sys.argv[2]] = pyperclip.paste()


elif len(sys.argv) == 2:
    # List keywoards and load content
    if sys.argv[1].lower == 'list':
        pyperclip.copy(str(list(mcbShelf.keys())))

    elif sys.argv[1] in mcbShelf:
        pyperclip.copy(mcbShelf[sys.argv[1]])


mcbShelf.close()

When I start the program from my command prompt for example with "py mcb.py list" absolutely nothing happens I just go to the next line in my command prompt without an error message.

I only have this problem in my programs with pyperclip and sys.argv. If I only use pyperclip it seems to work properly any ideas how to fix the issue?

1 Answers1

0

For starters, if you're working out of Automate the Boring Stuff, the file extension should be .pyw which won't display output on the command line.

To manually test if the script is working properly, copy some text before running the script in the command line. When you run the following command, the text currently copied to the clipboard will be saved to the shelf and will be associated with the 'keyword' name, similar to a dictionary key: value relationship

python3 mcb.pyw save <keyword>

You can save as many keyword: text pairs as you want by running this command and saving text to uniquely named keywords.

Running the script with only a keyword argument (python3 mcb.pyw <keyword>) copies the text associated with the keyword onto the clipboard. To see that keyword's text, paste into an open text editor.

To see a list of the keywords just run:

python3 mcb.pyw list

By running this, you've copied the list of shelf keys to the clipboard using pyperclip, so now you can paste into an open text editor to see a list of saved keywords, as there will be no output on the command line

For example:

  1. Copy email password to the clipboard

  2. Run python3 mcb.pyw save password

  3. Copy a quote from a news article

  4. Run python3 mcb.pyw save quote

  5. Run python3 mcb.pyw password

  6. Paste into a text editor to see email password

  7. Run python3 mcb.pyw list

  8. Paste into a text editor to see ['password', 'quote']