Last active
May 31, 2024 16:16
-
-
Save acsr/529807c39d430642a60564fb81230911 to your computer and use it in GitHub Desktop.
Filter out the [[datetime]] default String for MacOS DE from a [[Screenshot]] (code for insertion in Automator Workflow as Service)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""Remove macOS Screenshot DE Timestamps.py | |
Filter out the [[datetime]] default String for MacOS DE from a [[Screenshot]] | |
@Copyright 2023 by Armin Stross-Radschinski, ACSR industrialdesign [email protected] | |
Licence: MIT, do what you like | |
Have fun | |
V 0.2.1 20240531_181322-acsr (adding license in the docstring) | |
20231109_163157 by Armin Stross-Radschinski, [email protected] for acsr/evenios | |
20240531_170156-acsr added recreate Timestamp as Prefix | |
20240531_181322-acsr clarify docstring of extract_datetime() | |
""" | |
import sys | |
import re | |
regex = r"\s*\d\d\d\d-\d\d-\d\d um \d\d.\d\d.\d\d\s*" | |
#example_str = "Something totally different 2022-11-13 um 09.04.17.png" | |
#example_str = "2022-11-13 um 09.04.17 Something totally different.png" | |
subst = "" | |
def extract_datetime(filename): | |
"""We extract the matching pattern and reformat as simple DateTime ISO""" | |
result = re.search(regex, filename) | |
if result: | |
result = result.group(0).strip().replace("-", "").replace(" um ", "_", 2).replace(".", "") | |
return result+"-" | |
else: | |
return "" | |
def remove_datetime(filename): | |
"""You can manually specify the number of replacements by changing the 4th argument""" | |
result = re.sub(regex, subst, filename, 1) | |
if result: | |
return result | |
else: | |
return filename | |
for filename in sys.stdin: | |
datetimeprefix = extract_datetime(filename) | |
croppedfilename = remove_datetime(filename) | |
adddatetimeprefix = datetimeprefix+croppedfilename | |
print(adddatetimeprefix, end="") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rev 3: clarify docstring of extract_datetime()