So what I'm trying to accomplish is automatically filing my pay stub PDFs downloaded from our organization's (braindead) document system based in the date that's on the stub. I'm doing this based on two rules:
Rule 1 watches the Downloads folder for the particular naming scheme and contents of these PDFs, and moves them to the "Pay Stubs" folder. This part works completely fine.
Rule 2 watches the Pay Stubs folder and, using the same set of criteria, runs a Ruby script to read the date out of the PDF using `pdftotext` and rename it. The script is as follows:
- Code: Select all
#!/usr/bin/ruby
%w(date fileutils rubygems).each{|r| require r}
path = ARGV[0]
text = `pdftotext "#{path}" - `
string = text.gsub(/[\)\*]/,"").match(/Advice\s+Date\s+(\d{2}\/\d{2}\/\d{4})/).to_a[1]
date = Date.parse(string)
newpath = "#{File.dirname(path)}/#{date}.pdf"
FileUtils.mv(path,newpath)
When I run this script on one of these PDFs manually using the Terminal, it is renamed to the right date (2010-12-17.pdf, in my test case) as expected. When Hazel tries to run the script, however, I get this error logged for every such PDF:
- Code: Select all
2013-05-12 14:11:35.132 hazelworker[37978] Processing folder Pay Stubs (forced)
2013-05-12 14:11:37.209 hazelworker[37978] ShowPDF-96e99736-c11d-4c0e-a3ba-957f0585a774157.VAL.pdf: Rule Rename Pay Stubs matched.
2013-05-12 14:11:37.285 hazelworker[37978] [Error] Shell script failed: Error processing shell script /Users/username/Desktop/Test.rb on file /Users/username/Desktop/Pay Stubs/ShowPDF-96e99736-c11d-4c0e-a3ba-957f0585a774157.VAL.pdf.
2013-05-12 14:11:37.285 hazelworker[37978] Shellscript exited with non-successful status code: 1
To attempt to debug why Hazel seems to be choking on this, I began commenting out lines from the bottom up, to see if I could find where it was failing. It failed until I hit the `date = Date.parse...` line. I can't see why that would cause it to fail, especially when it works fine in the Terminal.
Here's the pertinent chunk of raw output from `pdftotext` that I'm matching against:
- Code: Select all
PAY
VOID VOID VOID VOID VOID VOID VOID VOID
Advice No. 00358699 )Advice ))) )) Date * * )))))) ) 12/17/2010 * * )) )) )) )) )) ) ))) )
Any help you can provide would be greatly appreciated, thank you!!