dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

screenshot.py (3069B)


      1 from talon import Module, screen, ui, actions, clip, app, settings
      2 from datetime import datetime
      3 import os, subprocess
      4 
      5 active_platform = app.platform
      6 default_command = None
      7 if active_platform == "windows":
      8 
      9     default_folder = os.path.expanduser(os.path.join("~", r"OneDrive\Desktop"))
     10     # this is probably not the correct way to check for onedrive, quick and dirty
     11     if not os.path.isdir(default_folder):
     12         default_folder = os.path.join("~", "Desktop")
     13 elif active_platform == "mac":
     14     default_folder = os.path.join("~", "Desktop")
     15 elif active_platform == "linux":
     16     default_folder = "~"
     17     default_command = "scrot -s"
     18 
     19 mod = Module()
     20 screenshot_folder = mod.setting(
     21     "screenshot_folder",
     22     type=str,
     23     default=default_folder,
     24     desc="Where to save screenshots. Note this folder must exist.",
     25 )
     26 screenshot_selection_command = mod.setting(
     27     "screenshot_selection_command",
     28     type=str,
     29     default=default_command,
     30     desc="Commandline trigger for taking a selection of the screen. By default, only linux uses this.",
     31 )
     32 
     33 
     34 def get_screenshot_path():
     35     filename = "screenshot-%s.png" % datetime.now().strftime("%Y%m%d%H%M%S")
     36     folder_path = screenshot_folder.get()
     37     path = os.path.expanduser(os.path.join(folder_path, filename))
     38     return os.path.normpath(path)
     39 
     40 
     41 @mod.action_class
     42 class Actions:
     43     def screenshot():
     44         """takes a screenshot of the entire screen and saves it to the desktop as screenshot.png"""
     45         img = screen.capture_rect(screen.main_screen().rect)
     46         path = get_screenshot_path()
     47 
     48         img.write_file(path)
     49         app.notify(subtitle="Screenshot: %s" % path)
     50 
     51     def screenshot_window():
     52         """takes a screenshot of the current window and says it to the desktop as screenshot.png"""
     53         img = screen.capture_rect(ui.active_window().rect)
     54         path = get_screenshot_path()
     55         img.write_file(path)
     56         app.notify(subtitle="Screenshot: %s" % path)
     57 
     58     def screenshot_selection():
     59         """triggers an application is capable of taking a screenshot of a portion of the screen"""
     60         command = screenshot_selection_command.get()
     61         if command:
     62             path = get_screenshot_path()
     63             command = command.split()
     64             command.append(path)
     65             subprocess.Popen(command)
     66             app.notify(subtitle="Screenshot: %s" % path)
     67         else:
     68             if active_platform == "windows":
     69                 actions.key("super-shift-s")
     70             elif active_platform == "mac":
     71                 actions.key("ctrl-shift-cmd-4")
     72             # linux is handled by the command by default
     73             # elif active_platform == "linux":
     74 
     75     def screenshot_clipboard():
     76         """takes a screenshot of the entire screen and saves it to the clipboard"""
     77         img = screen.capture_rect(screen.main_screen().rect)
     78         clip.set_image(img)
     79 
     80     def screenshot_window_clipboard():
     81         """takes a screenshot of the window and saves it to the clipboard"""
     82         img = screen.capture_rect(ui.active_window().rect)
     83         clip.set_image(img)