How to create a Python clipboard copy script
In this guide, we will teach you how to create a Python clipboard copy script. We will accomplish this by piping
the output of one command to our Python script as an input.
In practice, this will look something like the following
cat very_long_file.txt | our_copy_script
The snippet above shows the concatenation of the first output that was pass into our script as input |
into the copy script
We will be using Python3 and PIP
. If you don’t know how to install PIP
, check out the previous post here Random password generator β Python3. You only need to follow the Installing PIP
part.
Enough information, for now, let’s start building our script
Installing our single dependency
Before we can create a Python clipboard copy script, we will make simpler by using pyperclip
to handle the whole clipboard logic.
In order to get pyperclip
on our system we will run the following command inside a terminal
matthias> python3 -m pip install pyperclip
We consider it a good practice to install Python dependencies inside of a virtual environment
. However, since we want this script to be accessible for every user and everywhere on the system, we install it globally.
Now that we have installed pyperclip
its time to set up our script.
Creating our copy script
Inside your terminal create a new file called copyscript.py
Inside our new file add the following code block
#!/usr/bin/env python3 import sys import pyperclip def main(): final_string = "" for line in sys.stdin: final_string += line if not final_string: print("Nothing to be copied") return pyperclip.copy(final_string) print("Input has been copied to your clipboard") if __name__ == "__main__": main()
Let’s go through this code block.
Our first line #!/usr/bin/env python3
tells the OS to use the specified shell, in our case python3
, to execute our script commands.
Next, we import 2 dependencies into our script, the internal dependency sys
and our downloaded dependency pyperclip
.
Before we head on over to the core part of our script main()
, let’s take a look at the last two lines
if __name__ == "__main__": main()
This code appears quite often in Python scripts and is a guard railing that protects users from accidentally invoking this script when they didn’t intend to.
Letβs say you want to use our script inside another script, e.g. import copyscript
. The second script will run our copy script at runtime with the wrong command-line arguments, which is rarely something you want to happen.
Now it’s finally time for our main part, the main()
function
def main(): final_string = "" for line in sys.stdin: final_string += line if not final_string: print("Nothing to be copied") return pyperclip.copy(final_string) print("Input has been copied to your clipboard")
Inside this function, we define an empty string called final_string
. Because pyperclip
requires a full string to copy we add the individual lines, if any, from our input to this variable.
for line in sys.stdin: final_string += line
Not every file is accessible for every user, therefore we add a check to see if any lines were added to our final_string
variable. If that’s not the case then we stop the execution of the script and don’t copy anything.
if not final_string: print("Nothing to be copied") return
However, if we do have lines added to final_string
we use pyperclip
to copy this to our clipboard.
Save the script and run it with
matthias> chmod a+x copyscript.py matthias> cat my_example_file | /path/to/copyscript.py
We execute the command chmod a+x copyscript.py
to allow every user to run this script.
Making our script globally accessible
For every user to access this script from wherever on the OS filesystem, we execute the following command
matthias> sudo ln -s /full/path/to/copyscript.py /usr/bin/copyscript
After restarting your terminal you should now be able to execute the script whereever you are with
matthias> cat my_example_file_in_different_directory | copyscript
We have successfully created a Python clipboard copy script!