DISCLAIMER: Until today, I have never interacted with Ruby before, so if I'm not following best practices, etc., feel free to correct me...
I was having trouble getting the script from this thread to work for my scenario, and through some googling I found this thread. Someone there had posted a Ruby script that would extract the date from a pdf and rename the file using the extracted name.
However, that didn't work well with the flow of my Hazel rule, so I found some commands that allowed me to simply update the 'date modified' of the file, which I could then use in the renaming process.
Here is the code:
The "Shell:" path should be set to "/usr/bin/ruby", and "SearchStringHere" should be changed to whatever text precedes the date you are trying to extract.
- Code: Select all
require 'osx/cocoa'
require 'fileutils'
require 'time'
include OSX
OSX.require_framework('PDFKit')
path = ARGV[0]
if not path
puts "Missing path."
exit 1
end
pdfDoc = PDFDocument.alloc.initWithURL(NSURL.fileURLWithPath(path))
if not pdfDoc
puts "Failed to open pdf"
exit 1
end
pdfText = pdfDoc.string.to_s
match = pdfText.match(/SearchStringHere\s*(\d+)\/(\d+)\/(\d+)/i)
if not match
puts "Didn't find search string"
exit 1
end
month, day, year = match[1], match[2], match[3]
year = year.to_i
year += 2000 if year < 50
year += 1900 if year < 100
t = Time.parse("#{day}-#{month}-#{year}")
File.utime(t,t, path)
exit 0