Skip to content

Latest commit

 

History

History
51 lines (34 loc) · 1.95 KB

File metadata and controls

51 lines (34 loc) · 1.95 KB

Automated Screen Shot Clean-Up

Objective

Write a python script to move screenshots to trash every once they are older than 24 hours.

Design

  1. Identify screenshots among other files.
  2. Identify timestamp on screenshot.
  3. Move files that are older than 24 hours to ~/.Trash.
  4. Run script periodically to ensure desktop stays tidy.

Tools Used

Python

From Python's standard library, the following objects and relevant class methods were used.

  • datetime for timestamps and datetime objects.
  • os for simplified OS-independent functionality . Notable module functions used include:
    • os.listdir(path='.'): returns a list containing the names of the entries in the directory given by path.

    • os.path.expanduser(path): expands ~ and ~user. If user or $HOME is unknown, do nothing.

    • os.stat(path, *): gets the status of a file or a file descriptor, including creation time attribute in seconds UTC (shown below).

      >>> import os
      >>> statinfo = os.stat('somefile.txt')
      >>> statinfo.st_ctime
      23452345234.4324  # seconds UTC
    • shutil.move(src, dst): recursively move a file or directory (src) to (dst) and return the destination.

    • os.path.join(a, *p): joins two or more paths, inserting '/' as needed.

Operating System (MacOS)

The shebang instructs the user's shell (e.g. bash) where to find the necessary interpreter for script. It's placed in the first line like so:

#!/usr/bin/env python3
''' My Python Script '''

# ...rest of script...

MacOS has its own framework for managing system and user-level recurring background processes (daemons) called launchd. This tool runs successfully with user-level permissions via a simple .plist file and enabled via Apple's complementary command-line launchctl tool.

References

  • Using launchd agents to schedule scripts on macOS link