Returning HazelSwitchFile from shell script?

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

Moderator: Mr_Noodle

Returning HazelSwitchFile from shell script? Tue Apr 15, 2014 8:31 pm • by Smudge
I prefer to use bash, perl, or python for rule action scripting instead of Applescript but it seems that I can't return with a new HazelSwitchFile. The Hazel help says that the script must return (exit actually) with 0 for success and anything else is a failure. The HazelSwitchFile appears to be only accepted by Hazel via Applescript.

I'm not very proficient with Applescript but from what I've been able to find, the curly braces in the "return {HazelSwitchFile:...}" statement is simply returning a comma-separated list. If I output the same list format from a shell script, it doesn't work.

Is this a correct observation or am I just missing something here?

If this is the case, would it be possible to add support for this in a future version? It should be possible to check the execute result code for pass/fail (which it appears you are doing now) but also accept/parse the script's output for Hazel-friendly variables/lists/etc.

Thanks
Smudge
 
Posts: 34
Joined: Mon Jul 15, 2013 10:44 pm

The problem here is that AppleScript allows returning rich data types. In this case, it's a record, which allows having an arbitrary set of key-value pairs. Shellscripts are much more limited and can only return a single number. I've been considering various "out-of-band" mechanisms of doing this with shellscripts. Unfortunately, there's no standard way of doing this and it would probably end up resulting in a more complex shellscript. In the end, it is probably easier to do it in AppleScript.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Please keep considering the support for shell scripts.

There is a standard way to get the results from a shell script using the standard output (STDOUT) and standard error (STDERR) for verbose text and the process exit code. The "single number" you are referring to is the process exit code. It is great that you are using it to determine if the script passed or failed as that is the intended use.

My request is if you would add support to capture the standard output (STDERR probably isn't necessary). By default the output would be ignored and Hazel would just use the exit code as it is now but the user has the option to enable it.

Enabling it would require them to choose how Hazel handles the output.
  • Capture all of the output and put it into a single Hazel token which could then be used in later rules
  • Parse each line of the output and capture ONLY lines that contains Hazel-friendly text to be inserted into multiple tokens

I can see in the Hazel DEBUG output that you are already passing along the standard output of the script but just not doing anything with it yet.
Code: Select all
2014-04-15 20:28:21.943 hazelworker[57439] DEBUG: == script output ==
argv: ['/Users/Smudge/bin/processFile.py', '/Users/Shared/TV/Processing/invader.zim.s01e01.avi']

Processing 'invader.zim.s01e01.avi'

GuessIt found: {
    [1.00] "mimetype": "video/x-msvideo",
    [1.00] "episodeNumber": 1,
    [1.00] "container": "avi",
    [0.50] "title": "The Nightmare Begins",
    [0.70] "series": "Invader ZIM",
    [1.00] "type": "episode",
    [1.00] "season": 1
}

High enough confidence.  Continuing on...
{hazelExportTokens:{series:'Invader ZIM'},{season:1},{episodeNumber:1}}

Renamed 'invader.zim.s01e01.avi' to 'Invader ZIM S01E01 The Nightmare Begins.avi'
{hazelSwitchFile:'/Users/Shared/TV/Processing/Invader ZIM S01E01 The Nightmare Begins.avi'}

== End script output ==
2014-04-15 20:28:21.943 hazelworker[57439] DEBUG: Shellscript /Users/Smudge/bin/processFile.py executed successfully with file /Users/Shared/TV/Processing/invader.zim.s01e01.avi


So in this example, Hazel would ignore the lines of output except
{hazelExportTokens:{series:'Invader ZIM'},{season:1},{episodeNumber:1}}
{hazelSwitchFile:'/Users/Shared/TV/Processing/Invader ZIM S01E01 The Nightmare Begins.avi'}

For these lines, it would create custom tokens for series, season, and episodeNumber then use the renamed file for further rule actions.


And just to throw a bit more into the request. Currently only the filename is passed to the script but it would be very useful to have Hazel pass any matched tokens from the rule set.
One idea is that you should be able to set them as prefixed environment variables when you call the shell script. For example if it matches to a custom token named 'foo', you would set the environment variable of '_hazel_foo' to the token value.

So there is plenty of info for you to think about. Hopefully you can consider this request for enhancement as it would greatly increase the flexibility of Hazel.

Thanks
Smudge
 
Posts: 34
Joined: Mon Jul 15, 2013 10:44 pm

I'm not sure if it's a good idea to scrape stderr and stdout. There will be too much noise and you would have to deal with escaping whatever special text you use as a delimiter. I'd rather set up a dedicated pipe for this type of thing so as to not have to deal with the noise/mess of using stderr/stdout.

And yes, passing in variables is also on the list. There, though, there is no standard format nor is there a standard library for parsing that type of thing. Setting up environment variables is a possibility though again, mixing it with the standard pool can get messy. In any case, chances are this functionality will be introduced in AppleScript first since it has richer data types.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support