Created
October 8, 2019 17:25
-
-
Save brokensandals/a884904696da770649b8d47105f1d4fe to your computer and use it in GitHub Desktop.
Script to update folder names of Mac Photos app exports, so that the date comes first.
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 ruby | |
# When you export files from the Mac Photos app, it can generate a directory structure like this: | |
# ./ | |
# ./My Favorite Place - Some City, April 10, 1999 | |
# ./Some Other City, January 1, 1995 | |
# | |
# This script renames all those directories to follow this structure: | |
# ./ | |
# ./1995-01-01 Some Other City | |
# ./1999-04-10 My Favorite Place - Some City | |
# | |
# Only English month names are supported. | |
# | |
# The script accepts one argument, the path to the base directory. | |
require 'date' | |
MONTH_REGEXP = Regexp.new(Date::MONTHNAMES.compact.join('|')) | |
MOMENT_DIR_REGEXP = /\A((?<location>.*?),\s)?(?<month>#{MONTH_REGEXP})\s(?<day>\d+),\s(?<year>\d+)\z/ | |
if ARGV.length != 1 | |
puts "Usage: ruby mac-photos-export-reorganize.rb DIR" | |
exit 1 | |
end | |
$dir = File.expand_path(ARGV[0]) | |
if !Dir.exist?($dir) | |
puts "Directory #{$dir} does not exist" | |
exit 1 | |
end | |
if !File.directory?($dir) | |
puts "#{$dir} is not a directory" | |
exit 1 | |
end | |
Dir["#{$dir}/*"].each do |dir| | |
next if !File.directory?(dir) | |
basename = File.basename(dir) | |
match = MOMENT_DIR_REGEXP.match(basename) | |
unless match | |
puts "Skipping directory: #{dir}" | |
next | |
end | |
new_basename = "%04d-%02d-%02d %s" % [match[:year], Date::MONTHNAMES.index(match[:month]), match[:day], match[:location]] | |
new_name = "#{File.dirname(dir)}/#{new_basename}" | |
puts "Renaming #{dir} to #{new_name}" | |
File.rename(dir, new_name) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment