Tuesday, January 3, 2012

Rename Screenshots OSX

I'll have to figure out how to get this into an automatic folder action somehow, but for now this works:

#!/usr/bin/env ruby
Dir.chdir("Desktop") # works in 1.8.7, later use Dir.home
prefix = "Screen shot-"
dir = Dir.glob("#{prefix}*.*")
dir.each do |file| 
time_date = File.new(file).mtime.strftime("%Y-%m-%d-%H%M-%S") 
new_name = "#{prefix}#{time_date}"
if (file.split(".").count > 1)
extension = file.split(".").last
new_name = "#{new_name}.#{extension}"
end
`mv '#{file}' '#{new_name}'`
end

puts "Renamed #{dir.count} files with prefix #{prefix}"

No comments: