Skip to content

Instantly share code, notes, and snippets.

@paulgalow
Last active October 13, 2024 17:38
Show Gist options
  • Select an option

  • Save paulgalow/70bff79dcf1f4a0ec74ccff6e50e1bc9 to your computer and use it in GitHub Desktop.

Select an option

Save paulgalow/70bff79dcf1f4a0ec74ccff6e50e1bc9 to your computer and use it in GitHub Desktop.

Revisions

  1. paulgalow revised this gist Apr 15, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rename-photos-videos-timestamp.sh
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@ setFileExtension() {

    # Retrieve EXIF timestamp from media file
    getTimeStamp() {
    local -r timestampRaw=$(mdls "$file" | awk '/^kMDItemContentCreationDate / { print $3 " " $4 }')
    local -r timestampRaw=$(mdls -name kMDItemContentCreationDate -raw "$file" | cut -d ' ' -f -2)
    timestamp=${timestampRaw//:/-}
    }

  2. paulgalow revised this gist Jan 26, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rename-photos-videos-timestamp.sh
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ sendNotification() {
    osascript <<-EndOfMessage
    display notification \
    "$1" \
    with title "Umbenennen (Erstellungsdatum)"
    with title "Rename (Timestamp)"
    EndOfMessage
    }

  3. paulgalow created this gist Jan 26, 2019.
    99 changes: 99 additions & 0 deletions rename-photos-videos-timestamp.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    #!/bin/bash

    # macOS Automator script to rename photos/videos based on creation date
    # Blog post: https://paulgalow.com/macos-quick-action-rename-photos-videos-timestamp

    export PATH=/usr/bin:/bin:/usr/sbin:/sbin

    # Create subfolder to store renamed files
    createDestination() {
    readonly destination="$(dirname "$file")/sorted"
    mkdir -p "$destination"
    }

    # Check for file MIME type
    hasMIMEType() {
    file -I "$file" | grep -q "$1"
    }

    # Send notification to user
    sendNotification() {
    osascript <<-EndOfMessage
    display notification \
    "$1" \
    with title "Umbenennen (Erstellungsdatum)"
    EndOfMessage
    }

    # Determine file extension
    setFileExtension() {
    if hasMIMEType "image/jpeg"
    then
    extension="jpg"
    elif hasMIMEType "video/quicktime"
    then
    extension="mov"
    else
    return 1
    fi
    }

    # Retrieve EXIF timestamp from media file
    getTimeStamp() {
    local -r timestampRaw=$(mdls "$file" | awk '/^kMDItemContentCreationDate / { print $3 " " $4 }')
    timestamp=${timestampRaw//:/-}
    }

    # Rename file, handle timestamp collisions
    renameFile() {
    # Exit function if file type is not supported
    setFileExtension || return 0

    getTimeStamp || return 0

    createDestination || return 1

    # Check if to be renamed file already exists
    if [[ -f "$destination/$timestamp.$extension" ]]
    then
    # If file exists append random string to file
    local -r randomString=$(md5 -q "$file")
    cp "$file" "$destination/$timestamp-$randomString.$extension"
    else
    cp "$file" "$destination/$timestamp.$extension"
    fi
    }

    # Send notification to user after the process has finished
    presentResults() {
    # Count files in destination folder
    local counter=0
    for file in "$destination"/*.*
    do
    (( counter ++ ))
    done

    # Check if renaming was successful
    if [ $counter != 0 ] && [ -n "$destination" ]
    then
    sendNotification "$counter photos/videos have been renamed. Opening destination folder…"
    open "$destination"
    else
    sendNotification "Could not renamd any photos/videos."
    fi
    }

    # Main loop to collect input files from Automator
    processFiles() {
    while read -r file; do
    renameFile
    done
    }

    sendNotification "Processing photos/videos.\\nThis can take a while…"

    processFiles

    presentResults

    exit 0