forked from matryer/xbar-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreenshot.1d.py
executable file
·145 lines (117 loc) · 4.56 KB
/
screenshot.1d.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
# coding=utf-8
# <xbar.title>Screenshot</xbar.title>
# <xbar.version>v1.1</xbar.version>
# <xbar.author>Brandon Barker, Soumya Ranjan Mohanty</xbar.author>
# <xbar.author.github>ProjectBarks, geekysrm</xbar.author.github>
# <xbar.desc>Allows for screenshots to be uploaded, saved, and added to the clipboard</xbar.desc>
# <xbar.image>http://i.imgur.com/51rg3EJ.png</xbar.image>
# <xbar.dependencies>python</xbar.dependencies>
# <xbar.abouturl>https://github.com/matryer/bitbar-plugins/blob/master/System/screenshot.1d.py</xbar.abouturl>
import os, subprocess, tempfile, hashlib, requests, sys, platform, time
from distutils.version import StrictVersion
SAVE_PATH = "~/Pictures/"
def screenshot(path, copy_to_clipboard=False, show_cursor=False, show_errors=False, interactive=False,
only_main_monitor=False, window_mode=False, open_in_preview=False, selection_mode=True,
sounds=True, delay=5):
params = ""
if copy_to_clipboard:
params += "-c "
if show_cursor:
params += "-C "
if show_errors:
params += "-d "
if interactive:
params += "-i "
if only_main_monitor:
params += "-m "
if window_mode:
params += "-o "
if open_in_preview:
params += "-P "
if selection_mode:
params += "-s "
if sounds:
params += "-x "
if delay != 5 and delay >= 0:
params += "-T {} ".format(delay)
os.system("screencapture {} {}".format(params, path))
return os.path.isfile(path)
def text_to_clipboard(output):
process = subprocess.Popen('pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE)
process.communicate(output.encode('utf-8'))
def notify(title, subtitle, message):
command = "display notification \"{}\" with title \"{}\"".format(message, title)
if len(subtitle) > 0:
command += " subtitle \"{}\"".format(subtitle)
os.system("osascript -e '{}'".format(command))
def upload_image(upload):
# upload is the path of the image
headers = {
'authorization': 'Client-ID 6fcd294cd0e8aa1',
}
files = {
'image': (open(upload, 'rb')),
}
r = requests.post('https://api.imgur.com/3/upload', headers=headers, files=files)
import json
return json.loads(r.text)['data']['link']
class Command(object):
def __init__(self, title, name):
self.title = title
self.name = name
def get_name(self):
return self.name
def get_description(self):
return "{0} |bash={2} param1={1} terminal=false".format(self.title, self.name, os.path.realpath(__file__))
def execute(self):
raise Exception("Abstract Function")
class Upload(Command):
def __init__(self):
super(Upload, self).__init__("Upload Online", "upload")
def execute(self):
temp_path = tempfile.NamedTemporaryFile().name + ".png"
if not screenshot(temp_path):
exit()
notify("Uploading Screenshot", "", "Your image is being uploaded online!")
url = upload_image(temp_path)
os.remove(temp_path)
notify("Copied Screenshot", "", "Image URL copied to clipboard!")
text_to_clipboard(url)
class Clipboard(Command):
def __init__(self):
super(Clipboard, self).__init__("Copy to Clipboard", "clipboard")
def execute(self):
temp_path = tempfile.NamedTemporaryFile().name + ".png"
if not screenshot(temp_path):
exit()
os.system("osascript -e 'set the clipboard to POSIX file \"{}\"'".format(temp_path))
notify("Copied Screenshot", "", "Image copied to clipboard!")
class SaveFile(Command):
def __init__(self):
super(SaveFile, self).__init__("Save to File", "save")
def execute(self):
temp_path = os.path.join(os.path.expanduser(SAVE_PATH), time.strftime("screenshot-%Y%m%d-%H%M%S.png"))
parent = os.path.dirname(temp_path)
if not os.path.isdir(parent):
os.mkdir(parent)
if not screenshot(temp_path):
exit()
os.system("open -R {0}".format(temp_path))
version = platform.mac_ver()[0]
if StrictVersion(version) < StrictVersion("10.9"):
raise Exception("Mac OSX is too old!")
sub_commands = [Upload(), Clipboard(), SaveFile()]
if len(sys.argv) <= 1:
print("📸")
print("---")
for sub_command in sub_commands:
print(sub_command.get_description())
else:
try:
for sub_command in sub_commands:
if sub_command.get_name() != sys.argv[1]:
continue
sub_command.execute()
except Exception, e:
notify("Error", "", str(e))