Screwy Ruby script problem

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Screwy Ruby script problem Sun May 12, 2013 3:15 pm • by MacPrince
Hi, everybody.

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!!
MacPrince
 
Posts: 3
Joined: Sun May 12, 2013 2:14 pm

Re: Screwy Ruby script problem Mon May 13, 2013 12:37 pm • by Mr_Noodle
Hazel does not run scripts in an interactive shell so you cannot rely on environment variables. In particular, I would make sure to use full paths on everything (like the pdftotext).
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Screwy Ruby script problem Mon May 13, 2013 1:43 pm • by MacPrince
That did it! I changed the pdftotext invocation to:
Code: Select all
`/usr/local/bin/pdftotext "#{path}" - `


And now it works! Thank you so much!
MacPrince
 
Posts: 3
Joined: Sun May 12, 2013 2:14 pm


Return to Support