Skip to content

Commit 791ee58

Browse files
authored
Merge pull request #877 from Prashant-Bhapkar/main
Copy to Clipboard
2 parents 4d09210 + 346db2c commit 791ee58

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

copy_to_clipboard/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copy To Clipboard
2+
3+
Copying the contents of a text file to the clipboard is an all-too-frequent task, whether it’s a log file someone wants to see, a configuration file you need to share, or even an ssh key. Getting to the file is easy enough on the command line, but then having to open it in an editor just to copy-and-paste takes time you don’t need to waste.
4+
5+
The following copy2clip script works on both Windows and Mac, and uses native functionality that is wrapped in a little bit of Python code to load the files into the clipboard.
6+
7+
### To run it:
8+
> $ copy2clip filename
9+
10+
So now that we know how to copy and paste file content quicker and easier, let’s apply it to file names.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import os
2+
import sys
3+
import platform
4+
import subprocess
5+
6+
# Seeing if the file exists
7+
if os.path.exists(sys.argv[1]):
8+
f = open(sys.argv[1], "r")
9+
f_contents = f.read()
10+
f.close()
11+
else:
12+
print("Usage: copy2clip <file_name>")
13+
exit(1)
14+
15+
whatos = platform.system()
16+
17+
if whatos == "Darwin":
18+
subprocess.run("pbcopy", universal_newlines=True, input=f_contents)
19+
print("success: copied to clipboard")
20+
elif whatos == "Windows":
21+
subprocess.run("clip", universal_newlines=True, input=f_contents)
22+
print("success: copied to clipboard")
23+
else:
24+
print("failed: clipboard not supported")

0 commit comments

Comments
 (0)