Last active
June 11, 2022 07:57
-
-
Save mlsmrc/d82614c595eb721aabd80577bc74a932 to your computer and use it in GitHub Desktop.
Assign tags on MacOS using a cronjob
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
#!/bin/bash | |
EXTENSION_TAG_MAPPING="${HOME}/.tag_mapping" | |
CRONTAG_LOG="/var/log/crontag.log" | |
function __echo() { echo "$(date +%D:%H:%M.%S) - $@" | tee -a "${CRONTAG_LOG}"; } | |
which tag &>/dev/null || { __echo "main: missing tag. install it by using 'brew install tag'. Exiting.."; exit 1; } | |
ls "${EXTENSION_TAG_MAPPING}" &>/dev/null || { __echo "main: missing extension tag mapping file ($EXTENSION_TAG_MAPPING)'. Exiting.."; exit 1; } | |
function mac_tag() | |
{ | |
if [ $# -ne 2 ]; then | |
__echo "usage mac_tag <tag> <file_extension>" | |
exit 1 | |
fi | |
local tagname=$1 | |
local extension=$2 | |
local exit_code=0 | |
find ~ -name *.${extension} 2>/dev/null | while read file; do | |
__echo "${FUNCNAME[0]}: tagging ${file} with ${tagname}" | |
tag --add "${tagname}" "${file}" || { __echo "${FUNCNAME[0]}: error tagging ${file} with ${tagname}"; exit_code=1; } | |
done | |
return ${exit_code} | |
} | |
function tag_files() | |
{ | |
local exit_code=0 | |
cat /dev/null > "${CRONTAG_LOG}" | |
__echo "${FUNCNAME[0]}: start tagging" | |
while read line; do | |
tag=$(echo "${line}" | cut -d ":" -f 1) | |
extension=$(echo "${line}" | cut -d ":" -f 2) | |
[[ "${tag}" == "#"* ]] && continue | |
[ -z "${tag}" ] || [ -z "${extension}" ] && { __echo "${FUNCNAME[0]}: tag or/and extension empty"; continue; } | |
__echo "${FUNCNAME[0]}: working on tagging .${extension} with tag ${tag}" | |
mac_tag "${tag}" "${extension}" || { __echo "${FUNCNAME[0]}: tagging .${extension} with tag ${tag} completed with errors"; exit_code=1; continue; } | |
__echo "${FUNCNAME[0]}: tagging .${extension} with tag ${tag} completed" | |
done < $EXTENSION_TAG_MAPPING | |
__echo "${FUNCNAME[0]}: tagging completed" | |
return ${exit_code} | |
} | |
tag_files || exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment