forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrot.py
97 lines (81 loc) · 3.18 KB
/
scrot.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
# -*- coding: utf-8 -*-
"""Take screenshots of screens, areas or windows.
This extension wraps the command line utility scrot to make screenshots from albert. When the \
screenshot was made you will hear a sound which indicates that the screenshot was taken \
successfully.Screenshots will be saved in XDG_PICTURES_DIR or in the temp directory.
Synopsis: <trigger>"""
import os
import subprocess
import tempfile
from shutil import which
from albertv0 import FuncAction, Item, iconLookup
__iid__ = "PythonInterface/v0.1"
__prettyname__ = "SCReenshOT utility"
__version__ = "1.0"
__trigger__ = "scrot "
__author__ = "Benedict Dudel"
__dependencies__ = ["scrot", "xclip"]
for dep in __dependencies__:
if not which(dep):
raise Exception("'%s' is not in $PATH." % dep)
iconPath = iconLookup("camera-photo")
def handleQuery(query):
if query.isTriggered:
return [
Item(
id = "%s-whole-screen" % __prettyname__,
icon = iconPath,
text = "Screen",
subtext = "Take a screenshot of the whole screen",
actions = [
FuncAction(
"Take screenshot of whole screen",
lambda: doScreenshot([])
),
FuncAction(
"Take screenshot of multiple displays",
lambda: doScreenshot(["--multidisp"])
),
]
),
Item(
id = "%s-area-of-screen" % __prettyname__,
icon = iconPath,
text = "Area",
subtext = "Draw a rectangle with your mouse to capture an area",
actions = [
FuncAction(
"Take screenshot of selected area",
lambda: doScreenshot(["--select"])
),
]
),
Item(
id = "%s-current-window" % __prettyname__,
icon = iconPath,
text = "Window",
subtext = "Take a screenshot of the current active window",
actions = [
FuncAction(
"Take screenshot of window with borders",
lambda: doScreenshot(["--focused", "--border"])
),
FuncAction(
"Take screenshot of window without borders",
lambda: doScreenshot(["--focused"])
),
]
),
]
def getScreenshotDirectory():
if which("xdg-user-dir") is None:
return tempfile.gettempdir()
proc = subprocess.run(["xdg-user-dir", "PICTURES"], stdout=subprocess.PIPE)
pictureDirectory = proc.stdout.decode("utf-8")
if pictureDirectory:
return pictureDirectory.strip()
return tempfile.gettempdir()
def doScreenshot(additionalArguments):
file = os.path.join(getScreenshotDirectory(), "%Y-%m-%d-%T-screenshot.png")
command = "sleep 0.1 && scrot --exec 'xclip -selection c -t image/png < $f' %s " % file
proc = subprocess.Popen(command + " ".join(additionalArguments), shell=True)