Last active
April 1, 2025 13:38
-
-
Save gingerbeardman/99585e86d9a6ed321a73cc5f6ab247a2 to your computer and use it in GitHub Desktop.
Applies different macOS Finder colours to images of specific sizes
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
#!/usr/bin/env zsh | |
# Function to tag files with specified dimensions and color | |
tag_dimension_files() { | |
local width="$1" | |
local height="$2" | |
local color="$3" | |
# Color label mapping | |
local label_index | |
case "$color" in | |
none) label_index=0 ;; | |
orange) label_index=1 ;; | |
red) label_index=2 ;; | |
yellow) label_index=3 ;; | |
blue) label_index=4 ;; | |
purple) label_index=5 ;; | |
green) label_index=6 ;; | |
gray) label_index=7 ;; | |
*) echo "Invalid color. Use: none, orange, red, yellow, blue, purple, green, or gray" | |
return 1 | |
;; | |
esac | |
# Validate numeric inputs | |
if [[ ! "$width" =~ ^[0-9]+$ || ! "$height" =~ ^[0-9]+$ ]]; then | |
echo "Width and height must be numeric" | |
return 1 | |
fi | |
# Counter for tagged files | |
local tagged_count=0 | |
# Find and tag files | |
find . -type f \( -name "*.jpg" -o -name "*.png" -o -name "*.jpeg" -o -name "*.webp" \) -print0 | \ | |
while IFS= read -r -d '' file; do | |
# Get image dimensions using sips | |
dimensions=$(sips -g pixelHeight -g pixelWidth "$file" 2>/dev/null) | |
file_width=$(echo "$dimensions" | awk '/pixelWidth:/{print $2}') | |
file_height=$(echo "$dimensions" | awk '/pixelHeight:/{print $2}') | |
# Check if dimensions match | |
if [[ $file_width -eq $width && $file_height -eq $height ]]; then | |
# Set color label using AppleScript | |
osascript -e "tell application \"Finder\" to set label index of (POSIX file \"$file\" as alias) to $label_index" > /dev/null 2>&1 | |
echo "Tagged: $file (${width}x${height}, $color)" | |
((tagged_count++)) | |
fi | |
done | |
echo "Total files tagged: $tagged_count" | |
} | |
tag_dimension_files "160" "64" "green" | |
tag_dimension_files "192" "64" "yellow" | |
tag_dimension_files "256" "65" "blue" | |
#tag_dimension_files "193" "64" "red" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
by Matt Sephton, March 2025, MIT