Page 1 of 1

Hazel and AppleScript

PostPosted: Mon May 06, 2019 7:27 pm
by AAALLL
Hi, I see that you can use AppleScript in Hazel but I do not see a way to script Hazel.

I am assuming this is impossible but I want to make sure.

I would like to take in a CSV into AppleScript and use that to create folders. I know how to do all of that but I would also like to create Hazel rules to go along with this. For each one, I would like to pass a few rules into it, one being the name of the folder but also 2 or 3 others.

Is this possible?

Re: Hazel and AppleScript

PostPosted: Tue May 07, 2019 10:21 am
by Mr_Noodle
Can you go into more details about the folders? Are they under a common folder? Are the rules basically the same?

Re: Hazel and AppleScript

PostPosted: Tue May 07, 2019 12:51 pm
by AAALLL
Mr_Noodle wrote:Can you go into more details about the folders? Are they under a common folder? Are the rules basically the same?


Yes, it’s probably easiest if I give an example to go into more detail.

Folder 1 would be Mac, Folder 2 iPhone, Folder 3 Apple Watch, Folder 4 iPad. All of the following folders are contained in my Documents, Quarter 1 folder (which would be created by the AppleScript.

The rules would be if a Folder in my downloads folder has the name Mac in it or contains the word TouchBar, it would get be moved into the Mac folder.

Similar rules for the other folders like if the document is named iPhone, contains 5.8 or 6.5 then it would be moved into the iPhone folder.

So I would have a CSV that would look something like this (or mortified if necessary)
named Mac, contains TouchBar, etc
named iPhone, contains 5.8, contains 6.5, etc
named Apple Watch, contains 44, contains 40, etc
named iPad, contains 11, contains 12.9

The destination would be dead in from the AppleScript as it would be changing from Mac to iPhone etc.

Re: Hazel and AppleScript

PostPosted: Wed May 08, 2019 10:57 am
by Mr_Noodle
If you know AppleScript, you can instead have a script that reads in that file then returns a custom attribute to Hazel to tell it which folder to sort the file into. That way you can have a single rule instead of having to create new rules each time.

Re: Hazel and AppleScript

PostPosted: Thu May 09, 2019 2:39 pm
by AAALLL
Mr_Noodle wrote:If you know AppleScript, you can instead have a script that reads in that file then returns a custom attribute to Hazel to tell it which folder to sort the file into. That way you can have a single rule instead of having to create new rules each time.

Ok, is that what is documented here? https://www.noodlesoft.com/manual/hazel ... avascript/

Re: Hazel and AppleScript

PostPosted: Fri May 10, 2019 10:41 am
by Mr_Noodle
Yes, you want to use the hazelOutputAttributes to return values for custom attributes back to Hazel.

Re: Hazel and AppleScript

PostPosted: Tue Mar 09, 2021 5:21 pm
by Nic
Hi,

Does that mean the answer to the original question is that Hazel 5 cannot be scripted/automated with AppleScript?

I was hoping to be able to have a script to automate the adding of new folders to hazel, setting the rules sync options for the folder, and enabling/disabling rules. Looks like Hazel 5 doesn't have AppleScript support but wanted to confirm that I wasn't missing anything.

If it doesn't have AppleScript support, are there any plans to add it now that hazel is a standalone app? *smiley face* *fingers crossed*

Re: Hazel and AppleScript

PostPosted: Wed Mar 10, 2021 9:55 am
by Mr_Noodle
I am considering it though I'll need to have use cases that justify it. If you can provide more details on yours, that would help.

Re: Hazel and AppleScript

PostPosted: Wed Mar 10, 2021 4:01 pm
by Nic
Okay, awesome. My use case is as follows.

I create a new folder for each new client/project I get. And I like to have Hazel watch each of those new folders to organise documents within those folders into subfolders (e.g. moving correspondence into a correspondence subfolder, moving receipts and invoices into a billing subfolder, moving draft documents into a drafts subfolder, etc). As the rules that sort the documents into subfolders are the same for each client/folder, I store them in an external file that is synced between each folder. When I create a new client folder, I add it to Hazel and specify that it is to sync with my client folder rules.

For each new client folder I also add a rule to my Downloads folder, which is monitored by Hazel and acts like my actions folder/inbox. The rule checks to see if a document contains the client/project reference in its name or contents and, if it does, moves the document into the corresponding client folder where it will be sorted into the relevant subfolder by the previously mentioned rules. Although given that I now have a number of these rules in my Downloads folder in Hazel, I don't actually add a new one each time; I just duplicate an existing one, change the client reference in the name of the rule and in each condition of the rule, and specify the new client folder in the move action.

So the workflow when I get a new client is to create a new folder in Finder, add it to Hazel, specify that it is to use the external rules sync file, enable the relevant rules, and then duplicate one of the Downloads folder rules and modify the client reference in its name and conditions. Currently I do this manually each time (like some kind of animal :wink: ) but it seems ripe for automation.

I'd love to create an AppleScript that I could run each time I have a new client to create the folder in Finder (easily done), add the newly created folder to Hazel, specify that the folder is to use a particular rules sync file, and create a new rule in my Downloads folder that would check files for the new client reference and move them to the newly created client folder.

I've also just encouraged a colleague to buy Hazel. She's not quite (meaning not at all) as nerdy as me but is keen for the productivity benefits of Hazel. I'll be able to get her set up with some of my Hazel rules, so she won't have to worry about trying to create them herself. I'm even thinking of just sharing some of my rules with her via Dropbox so that she can also have the benefit when I tweak/improve the rules, but just trying to work out a way to do this without sharing the rules that contain sensitive personal info like account numbers and stuff.

It would be great to have the AppleScript mentioned above for my colleague to use as well. She's the kind of person who loves things being organised, but doesn't quite have the diligence to consistently organise things herself. Which is why Hazel has so much promise for her. But after a while I suspect she'd stop taking the time to set up the new rules for each new client, and would thus stop experiencing the benefits of Hazel. But if the setup process was automated for her, she'd be much more likely to use it and would thus experience more benefits and would be more likely to continue to use/update Hazel.

So there's my use case and business case for you based on anicdata of one. :)

Thanks for considering to add AppleScript support and here's hoping you decide to go through with it. *fingers crossed*

Re: Hazel and AppleScript

PostPosted: Thu Mar 11, 2021 10:01 am
by Mr_Noodle
Will consider the possibility of Automating adding a folder and setting up syncing to an existing file. The other part, though, may be better served by using a table, instead of creating a new rule each time.

Re: Hazel and AppleScript

PostPosted: Thu Mar 11, 2021 4:17 pm
by Nic
Mr_Noodle wrote:The other part, though, may be better served by using a table, instead of creating a new rule each time.


Yes, I had the same thought and have started looking at this. Haven't successfully used the new list/table features in any rules yet but am excited by the possibilities. I guess I'd need to manage a table that contained columns for the client reference and a column for the file path for the corresponding folder? In the actions section how do I specify that I want the file moved to the path specified in the corresponding column of the matched row? I guess the only drawback of that approach is that the file paths would be hardcoded and thus would break if the folder was renamed, whereas I think Hazel's current move to folder feature uses aliases or is otherwise smart enough to know when the folder is renamed and automatically points to the renamed folder.

Re: Hazel and AppleScript

PostPosted: Fri Mar 12, 2021 10:16 am
by Mr_Noodle
If the locations are all under a common folder, you can use Sort into subfolder. There is also a new feature where you can specify a path separator character for an attribute so you can include subpaths in your table.

Re: Hazel and AppleScript

PostPosted: Sun Mar 21, 2021 5:37 pm
by Nic
Awesome! Have got this aspect of it up and running and so far seems to be working really well.