Last active
June 22, 2023 02:07
-
-
Save michimani/a005db16228aa6365ae730ae8281c059 to your computer and use it in GitHub Desktop.
This script organizes screen captures taken by Mac into monthly or daily directories. It is assumed to be executed by Automator app.
This file contains 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
import glob | |
import os | |
import re | |
from typing import Final | |
HOME_DIR: Final[str] = "/Users/hoge" # change to your home directory | |
FILE_TYPE: Final[str] = "jpg" # or "png" | |
NEW_FILE_PATTERN: Final = r".*(\d{4})-(\d{2})-(\d{2})_\d{5,6}([^\/]+)?\." + FILE_TYPE | |
SC_DIR: Final = f"{HOME_DIR}/Pictures/__ss_tmp" | |
SC_OUT_DIR: Final = f"{HOME_DIR}/Pictures/000_ScreenShot" | |
DIR_TYPE: Final = "MONTH" # "DAY" or "MONTH" | |
def get_screen_captures(): | |
return glob.glob(SC_DIR + "/*." + FILE_TYPE) | |
def rename_file_path(filename: str) -> str: | |
"""Rename file path. | |
* replace half space to under score | |
* remove period | |
* replace "(N)" of "(N)" to "_N" | |
Example: | |
"sc 2020-03-05_21.39.57.png" -> "sc_2020-03-05_213957.png" | |
""" | |
filename_tmp: str = filename.replace("." + FILE_TYPE, "").replace(SC_DIR, "") | |
filename_tmp = re.sub(r"( |\(|()", "_", re.sub(r"(\.|\)|))", "", filename_tmp)) | |
return SC_DIR + filename_tmp + "." + FILE_TYPE | |
def create_daily_directory(filename: str) -> str: | |
"""Create daily directory from renamed file name if it does not exists. | |
Example: | |
If the file name is "sc_2020-03-05_213957.png", | |
following directories will be created. | |
SC_OUT_DIR/2020/03/05 | |
""" | |
daily_dir = SC_OUT_DIR + re.sub(NEW_FILE_PATTERN, r"/\1/\2/\3", filename) | |
if os.path.exists(daily_dir) is False: | |
print("create directory " + daily_dir) | |
os.makedirs(daily_dir) | |
return daily_dir | |
def create_monthly_directory(filename: str) -> str: | |
"""Create monthly directory from renamed file name if it does not exists. | |
Example: | |
If the file name is "sc_2020-03-05_213957.png", | |
following directories will be created. | |
SC_OUT_DIR/2020/03 | |
""" | |
monthly_dir = SC_OUT_DIR + re.sub(NEW_FILE_PATTERN, r"/\1/\2", filename) | |
if os.path.exists(monthly_dir) is False: | |
os.makedirs(monthly_dir) | |
return monthly_dir | |
def move_file(old_path: str, new_path: str): | |
os.rename(old_path, new_path) | |
if __name__ == "__main__": | |
sc_list = get_screen_captures() | |
if len(sc_list) == 0: | |
print("There are no screen shot file.") | |
exit | |
for sc_file_path in sc_list: | |
renamed_file_path = rename_file_path(sc_file_path) | |
if DIR_TYPE == "DAY": | |
out_dir = create_daily_directory(renamed_file_path) | |
else: | |
out_dir = create_monthly_directory(renamed_file_path) | |
new_file_path = renamed_file_path.replace(SC_DIR, out_dir) | |
move_file(sc_file_path, new_file_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment