Page 1 of 1

Shell and Applescripts failing to execute

PostPosted: Wed Jan 16, 2013 6:22 pm
by wibedtor
Hi, I've been using Hazel for ~18 months, and really like it, so much that I didn't mind paying for the recent upgrade.

I recently migrated from Snow Leopard to Mountain Lion and am rewriting some old Hazel rules and coding some new ones. I'm also having a problem I never had before: Hazel is failing to execute scripts.

I've seen a lot of posts in the forums about scripts working in Terminal and not when called by Hazel; I've seen the advice about making sure you source your .bashrc, etc., and followed it all. I even tried reinstalling Hazel for all users (rather than the active user).

Well, none of that advice has worked.

I gave up on getting the shell script to work, so I decided to try the (apparently popular) workaround and write an Applescript to open the Terminal, then execute the script from there. That's not working either.

The Applescript to open the Terminal and execute the script works—except when I call it in Hazel.

Likewise, the shell script itself works—except when I call it in Hazel.

The Hazel logs say nothing; they don't say the script succeeded or that it failed, they just say that the folder was processed.

It's a really simple script, triggered by a time variable (although I've been testing it by running the rule manually).

Here's the AppleScript:

Code: Select all
tell application "Terminal"
   do script "/path/to/script.sh"
   delay 30
   quit
end tell


Here's the script itself, targeting Storify's RESTful API:

Code: Select all
#!/bin/bash
NOW=$(date +"%Y-%m-%d")
curl -d "api_key=XXXXXXXXXX&username=XXXXX&password=XXXXXX&story={\"title\":\"${NOW}\"}&publish=true" http://api.storify.com/v1/stories/XXXX/create


Any ideas?

Could it be something to do with the fact that the Hazel rule doesn't interact with any particular file but is merely triggered by the time? I don't know, I'm grasping at straws…

Anyway, I suspect this is an issue with Hazel, because I've tried pretty much everything approaching a solution in the forums. I just want to automate a simple script from my Mac without writing my own Launchd code or resorting to Lingon, which is by all accounts now quite broken and crippled; and use cases like this are why I bought Hazel instead of some open source alternative.

Thanks!

Re: Shell and Applescripts failing to execute

PostPosted: Thu Jan 17, 2013 2:28 pm
by a_freyer
I don't have any issue running curl from either an embedded script or a path script. You shouldn't either, so this is a bit weird.

What is the error you're getting?
Is Hazel just not doing anything, or do you receive a notification that the shell script failed?
What do the hazel logs say?
Have you redirected stderr to stdout to a file? (see below)

Code: Select all
curl -d "post data" "http://api..." 2>&1 > "myerrors.txt"

Re: Shell and Applescripts failing to execute

PostPosted: Thu Jan 17, 2013 3:18 pm
by Mr_Noodle
I also suggest specifying the full path to curl and seeing if that clears up anything.

Re: Shell and Applescripts failing to execute

PostPosted: Thu Jan 17, 2013 6:25 pm
by wibedtor
Thanks for the replies.

@Mr_Noodle: Putting the full path to curl was a good suggestion, I probably should've done that before posting, but it unfortunately didn't help.

@a_freyer: See, that's the really weird thing, there's no error that I can see. Here's the extent of the Hazel log:

Code: Select all
2013-01-17 22:16:18.394 hazelworker[15456] Processing folder XXXX (forced)
2013-01-17 22:16:20.411 hazelworker[15456] Done processing folder XXXX


From what the logs suggest it appears Hazel simply doesn't act on the script. I tried calling the script from both the preinstalled bash environment and my almost-fresh-out-of-the-box zsh environment with the same results.

And as you suggested, I tried redirecting the error output to a file, and again, really weird—it works in Terminal (with the script executing fine) but from Hazel, nothing.

Truly strange. Any other ideas?

Re: Shell and Applescripts failing to execute

PostPosted: Thu Jan 17, 2013 6:34 pm
by a_freyer
If you execute within Hazel, the log file is likely being created within the directory where your test file is.

For instance, this is the script I just tested on my machine (Desktop):

Code: Select all
NOW=$(date +"%Y-%m-%d")
curl "http://www.google.com/" 2>&1 > testing.txt


And testing.txt was successfully created on my desktop.

Re: Shell and Applescripts failing to execute

PostPosted: Thu Jan 17, 2013 6:58 pm
by wibedtor
Nope, not there either; first place I checked for it.

Also checked home dir, root dir, & Spotlight… the only time the log file was correctly created was when I tested the revised script from Terminal, and it showed no errors (only the expected JSON response from the remote server).

Re: Shell and Applescripts failing to execute

PostPosted: Thu Jan 17, 2013 7:16 pm
by a_freyer
I'm guessing that you don't see the expected result on Spotify?

Perhaps adding: exit 0 after curl. There is a chance that curl is exiting with an odd error code even in terminal. Beyond that, add an "echo whatever" to the script to verify that you are successfully creating the log file.

I may also suggest checking the end result on spotify to see if this is perhaps working, but that Hazel is not responding back as expected.

Re: Shell and Applescripts failing to execute

PostPosted: Thu Jan 17, 2013 7:27 pm
by a_freyer
I also note at least for the applescript, you forgot "sh"

should be:

Code: Select all
tell application "Terminal"
   do script "sh /path/to/script.sh"
   delay 30
   quit
end tell

Re: Shell and Applescripts failing to execute

PostPosted: Tue Jan 22, 2013 1:32 pm
by Mr_Noodle
Is the file even matching the rule? The logs don't seem to indicate that. I suggest reading the troubleshooting article stickied near the top of this forum so you can at least confirm the rule is being executed properly.

Re: Shell and Applescripts failing to execute

PostPosted: Wed Jan 23, 2013 11:49 am
by wibedtor
Thanks, I'll try these and post the results.