Page 1 of 1

returning a variable from script back into Hazel rules

PostPosted: Thu Oct 20, 2016 1:50 am
by carpii
I suspect this isn't currently possible, but it's worth asking anyway...

I have a bunch of Hazel rules which detect PDF files downloaded from various sites, and then call a script to deal with them (usually to rename them and move them into a directory on my NAS).

Is there any way I could return a variable from my script, which Hazel is then able to pick up and use in subsequent rules?

As a simple example, I'd like my script to rename the file, then pass the full path back to Hazel, which can then show it in a notification. Granted I could invoke the OSX notification from my script and just use Hazel to do the moving, but like I say, this is only a simple example

If not, it would be a very useful feature

Thanks

Re: returning a variable from script back into Hazel rules

PostPosted: Thu Oct 20, 2016 11:11 am
by Mr_Noodle
For AppleScript/JavaScript, you can export attributes back to Hazel. The help should have more info on how to do that. BTW, is there a reason why you have a script to rename and move files? Is there a reason why you aren't using Hazel's built-in actions to do that?

Re: returning a variable from script back into Hazel rules

PostPosted: Fri Oct 21, 2016 11:56 pm
by carpii
Mr_Noodle wrote:For AppleScript/JavaScript, you can export attributes back to Hazel


Are these pre-defined attributes, or arbitrary ones?
I had a look at the help, but I couldn't really see much, other than the 'attributes' which the OSX file system uses

BTW, is there a reason why you have a script to rename and move files? Is there a reason why you aren't using Hazel's built-in actions to do that?


I do try to limit to it Hazel where possible. Maybe I don't understand how to fully command Hazel rules, but if I take the simplest examples I can think of....

1) A file triggers a rule, and I want to rename it with a bit of substring'ing but also convert any spaces to underscores
Ie, "SomeCompany - My Invoice 23674.pdf" -> "20161022_SomeCompany_23674.pdf"

Then run further rules on the resulting file
Cannot find a way to do this purely in Hazel, so I have to shell out to a script and then do everything else in bash too.

2) A file is found in a directory (using OSX Folder Actions)
I have a script to scp it to a server, but making sure I do not overwrite any file with the same name.
So "My Invoice 23674.pdf" will become "20161022_SomeCompany_23674.pdf" and then scp'd, but that already exists
So the ultimate scp destination might end up being "20161022_SomeCompany_23674_2.pdf"

3) Finally, I have a setup which allows me to drag a bunch of PDF's pages into a directory, and it will amalgamate them into a single PDF.
I would love to be able to return the amalgamated PDF filename back to Hazel to use in subsequent rules

Thanks

Re: returning a variable from script back into Hazel rules

PostPosted: Mon Oct 24, 2016 10:58 am
by Mr_Noodle
You create ones in the script and export them. But again, not sure if it's necessary in your case.

1. This is doable. Look up match patterns in the help. You can then use the text replace function to do the space->underscore translation as well.

Do you mean further rules or actions? For actions, just add them to the same rule. For rules, you can use the "Continue" action.

2. Hazel has an "Upload" action and can do SFTP (which is file transfer over ssh). It has the option of appending a number at the end if the file already exists at the destination.

3. This part will require a script. Again, the help should have more info on this but you can have the script tell Hazel to use a different file for the subsequent actions in the rule. Otherwise, if it's a new file, it will get processed separately.