Write a python script to move screenshots to trash every once they are older than 24 hours.
- Identify screenshots among other files.
- Identify timestamp on screenshot.
- Move files that are older than 24 hours to
~/.Trash. - Run script periodically to ensure desktop stays tidy.
From Python's standard library, the following objects and relevant class methods were used.
datetimefor timestamps and datetime objects.osfor 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$HOMEis 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.
-
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.
- Using launchd agents to schedule scripts on macOS link