forked from ywangd/stash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbpaste.py
33 lines (26 loc) · 853 Bytes
/
pbpaste.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Writes the contents of the system clipboard to a file."""
from __future__ import print_function
import argparse
import os
import sys
import clipboard
def main(args):
ap = argparse.ArgumentParser()
ap.add_argument('file', nargs='?', help='the file to be pasted')
ns = ap.parse_args(args)
status = 0
if ns.file:
if os.path.exists(ns.file):
print("pbpaste: {}: file exists".format(ns.file), file=sys.stderr)
status = 1
else:
try:
with open(ns.file, 'w') as f:
f.write(clipboard.get())
except Exception as err:
print("pbpaste: {}: {!s}".format(type(err).__name__, err), file=sys.stderr)
else:
print(clipboard.get())
sys.exit(status)
if __name__ == "__main__":
main(sys.argv[1:])