Skip to content

Instantly share code, notes, and snippets.

@acsr
Last active May 31, 2024 16:16
Show Gist options
  • Save acsr/529807c39d430642a60564fb81230911 to your computer and use it in GitHub Desktop.
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)
#!/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 developer@acsr.de
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, acsr.github@dev.acsr.de 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="")
@acsr
Copy link
Author

acsr commented May 31, 2024

Rev 2: Code enhanced for re-adding the removed timestamp as brief ISO stamp prefix. Quick & dirty no validation at all. Guck hin was du tust ;-) – Look at what you're doing ;-)

@acsr
Copy link
Author

acsr commented May 31, 2024

Rev 3: clarify docstring of extract_datetime()

@acsr
Copy link
Author

acsr commented May 31, 2024

Rev 4: Fix python filename

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment