using automator to execute a bash script

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

Moderator: Mr_Noodle

using automator to execute a bash script Sun Aug 19, 2007 7:07 am • by meesterfox
Hello,

I like hazel quite a bit, and it has saved me quite a bit of time. Now, I just need to get a hazel for the real world, and I can have things like "empty the trash every sunday" done automatically.

I have hazel watching my documents folder. Items that have not been modified for over a month get "archived". but I don't mean zipped up. Due to a recent computer failure, data security and reliability has become important to me. So for older stuff, i want to make sure there are par2 files for it. I have it set to 10% redunancy, which should work for any small glitches. the par2 is created via the command line(via par2cmdline)

I achieve (after much tinkering) usablility of this command line tool via automator, which hazel can work with. It takes selected files, and runs the par2create command on them.

So, file is deemed old enough -> executes automator workflow -> executes shell command.

here's the issue: when i select multiple items in the finder, it works fine. the workflow has 'get selected finder items" first, which then uses that for input to the script.

but getting hazel to do it automatically is proving tricky. I can't use the "get selected finder items" since it's not a finder function. So, I remove that first rule and just run the command... which only works on a single file, and thats it.

this is the script that automator executes:

THE_FILES=("$@")

for (( i = 0; i < ${#THE_FILES[@]}; i++ ))
do
/opt/local/bin/par2create -r12 -n1 -u "${THE_FILES[$i]}"
done


So, basically: if I were to execute this on a bunch of files manually, it works wonderfully. automator dumps the files into the $@ variable and all is well. But doing it via hazel, I am having issues, and I'm not sure if I need to tweak the script, or what the best way to get it o work.

I'm tired, which is probably obvious... and more or less stuck. if anyone has any idea's or information, I would appreciate it.
meesterfox
 
Posts: 2
Joined: Sun Aug 19, 2007 6:48 am

Wed Aug 22, 2007 5:59 pm • by Mr_Noodle
Unfortunately, Hazel doesn't quite work like that. It processes files one by one so any triggered scripts or workflows will only see one file at a time.

One workaround would be to move these files to a special folder which then gets archived wholesale. I don't know anything about par2 but I'm assuming it can take a folder as an argument.

If you are updated to 2.0.1, you can also take advantage of the new "Date Last Matched" attribute. Basically, the tricky part is when Hazel should archive this new folder as you need to have it collect a certain number of files. What you could do is something like "Date Last Matched is not in the last X days" which means the rule won't get run until at least X days since the last time. This basically makes it such that the rule only runs approximately every X days.

Let me know what you end up doing or if you need any more help.
Mr_Noodle
Site Admin
 
Posts: 11250
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Odd.. Thu Aug 23, 2007 12:33 am • by meesterfox
That's odd. Because it DOES work for the first file, but doesn't work for the second or third file, and so forth.

my script is happy to do one file at a time. in fact, the command itself can only do one file at a time, anyway, without specifying the actoual par2 file name. That's why I had to throw in the loop, so it can handle multiple files, one at a time.

So, I know it's executing the command OK, since the first file works. but it's the second and third and fourth that have issues.


I seem to have fixed the problem somehow, by having it run the command directly through a bash script instead of through automator. (i initally had trouble with this, which is why i went to automator. but the issue was with the bash script itself, which is what made automator work as well, but i never thought to go back) My thought was that automator wasn't providing the status of the command and so hazel was running the rule on another file while automator was still proccessing the previous one. And since files get renamed, it didn't work afterwards. that was my thought, at least.

So, now it works. it renames the file, runs the script, and all is peaches.

Thanks for the help!
meesterfox
 
Posts: 2
Joined: Sun Aug 19, 2007 6:48 am

Thu Aug 23, 2007 2:59 pm • by Mr_Noodle
I see now. There is a problem with Automator triggering in rapid sequence. So yes, if possible, use a script directly instead. In any case, glad you got it working.
Mr_Noodle
Site Admin
 
Posts: 11250
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support