Skip to content

Instantly share code, notes, and snippets.

@shanehh
Last active November 23, 2021 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanehh/311e125bf52b0fe8e3336860b402acf9 to your computer and use it in GitHub Desktop.
Save shanehh/311e125bf52b0fe8e3336860b402acf9 to your computer and use it in GitHub Desktop.
cronjob to take a screenshot for my main monitor.. record my digital life.
#!/usr/bin/python
import os
def save_path():
import datetime as dt
from pathlib import Path
# file-naming
# https://softwareengineering.stackexchange.com/questions/61683/standard-format-for-using-a-timestamp-as-part-of-a-filename
timestamp = dt.datetime.now().strftime("%Y-%m-%dT%H-%M-%S%z")
p = (Path("~/Downloads/GooglePhotosSyncer") / timestamp).with_suffix(".png")
return str(p.expanduser())
def is_screen_active():
# https://stackoverflow.com/questions/52750812/detecting-lock-screen-on-mac-through-python3
import Quartz
d = Quartz.CGSessionCopyCurrentDictionary()
return not "CGSSessionScreenIsLocked" in d.keys()
if __name__ == "__main__":
# for cronjob, macos only
if is_screen_active():
# https://stackoverflow.com/questions/4524723/take-screenshot-in-python-on-mac-os-x
os.system(
'screencapture -x -C "{}"'.format(save_path()),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment