Page 1 of 1

1 rule for multiple matches

PostPosted: Sun Aug 31, 2014 9:42 am
by hepabolu
I have no clue how to best describe this, so I haven't been able to find a forum post related to my problem. If it does exists, please let me know.

Problem to be solved: I'd like to create a rule like 'if name matches 'string1' or 'string2', move the the file to the folder matching the matched string. So if the name matches 'string1' the file would go into /path/to/string1, and if it matches 'string2' it would be moved to /path/to/string2.

I have 2 versions of this problem:
1. I have bank statements of several banks. I've already created rules that rename the file to something like 2014-08_bank1-statement.pdf and move the files to an 'Action' folder where I can look at them before I have them processed. Right now I have a Hazel rule for every bank that goes like: if name matches _bank1- then move to /path/to/bank1. This quickly becomes a lot of work.

2. I have adopted the process of adding a 'projectname' to a file and have Hazel move all related files to a project directory. Similar to the bank statement problem I now have to go in an create a rule for each project.
What I'd like to do is create a script (apple script or shell script) that could do the matching based on a text file holding the projectnames and set the token that needs to be matched.

Is there a way to do this?

Thanks.

Re: 1 rule for multiple matches

PostPosted: Sun Aug 31, 2014 12:35 pm
by hepabolu
Of course, once this post was up, I found similar posts. Because it took me some time to find all the pieces necessary to pull it off, I'll write down all the steps I took to make it work, so hopefully it saves others some time. This post is for my first problem: having multiple bank statements I want to sort into various subfolders.

Step 1: create testfolder

  • Make a folder called 'testing'
  • in this folder make 2 folders called 'bank-bank1' and 'bank-bank2'
  • make 3 files called '2014-08_bank1-statement.txt', '2014-08_bank2-statement.txt', '2014-08_nomatch-bank1-statement.txt'

When all goes well, the Hazel rule wil put the first two in their respective folders while leaving the third untouched.

Step 2: make the rule

Make a rule in Hazel:

Name: move bankstatements
Code: Select all
If any of the following conditions are met
Name contains _bank1-
Name contains _bank2-

Do the following:

Run Applescript embedded script (for script see below)
Sort into subfolder 'bank-'


Step 3: create the Applescript

Click on the i icon to enter the script. Before you enter the script, click on the little icon on the top right of the window. Here you can enter the name of the custom token you want to return. In this case, enter 'bankName'.

Now enter the applescript:

Code: Select all
tell application "Finder" to set theName to the name of theFile

set theMatch to ""
if theName contains "_bank1-" then
   set theMatch to "bank1"
else if theName contains "_bank2-" then
   set theMatch to "bank2"
else
   -- no match for this file
   return false
end if
set theHazelExportTokens to {bankName:theMatch}
return {hazelExportTokens:theHazelExportTokens}


Step 4: finish the rule

Now that the Applescript is in place, you can go back to the 'Sort into subfolder' action and add the token. The 'bankName' token will be there. If not, or there is a token called 'unnamed', save the rule, open it up for editing and try again.

Re: 1 rule for multiple matches

PostPosted: Sun Aug 31, 2014 12:55 pm
by hepabolu
If you prefer to not embed the script, so you can reuse it in multiple rules, you can create the script in a separate file. Open Applescript Editor and add the script below:

Code: Select all
on hazelProcessFile(theFile)
   tell application "Finder" to set theName to the name of theFile

   if theName contains "_bank1-" then
      set theMatch to "bank1"
    else if theName contains "_bank2-" then
      set theMatch to "bank2"
    else
      -- no match for this file
      return false
   end if
   set theHazelExportTokens to {bankName:theMatch}
   return {hazelExportTokens:theHazelExportTokens}
end hazelProcessFile


Save the file in a convenient location as 'MatchBankName.scpt'.

Open the rule from the previous post and change the 'embedded script' to 'Other...'. Now choose the MatchBankName script.

Don't forget to press the i icon to the right of the script name to enter the name of the token to return 'bankName'.

Save the Hazelrule and check that it works.