Using Nested Conditions

Most rules work just fine when you use “all” to match all the conditions, “any” to match at least one condition, or “none” to match no conditions—for example, “All of (Name contains Apple, Extension is .pages, and Date Created is Today)” or “Any of (Size is greater than 1 GB, Date Last Opened is not in the last 5 years, or Color Label is Red).” But what if you need more elaborate combinations, like “All of (Any of (Kind is PDF or Kind is Image)) and (Any of (Name contains Screenshot or Tags do not contain Ignore))”?

Hazel has just the solution for such cases:  nested conditions , in which a single condition can contain one or more additional conditions (and so on as deep as necessary), with “all”, “any,” or “none” specified at each step. This capability lets you construct highly specific conditions that amount to various combinations of AND and OR operations. For example:

Nested conditions come with an additional feature that gives them even more power— condition targets . For each level of nesting (even if you use only a single nested condition, so there’s effectively no hierarchy at all), Hazel provides a pop-up menu that lets you choose the scope to which that condition applies. Although that scope can simply be the file or folder that is currently being processed (the default), you can instead choose to examine subfiles and subfolders, or the enclosing folder, of the current item. For example:


Create Nested Conditions

To create a nested condition, do either of the following:

In either case, two new rows appear below that row. The first has its own “any/all/none” pop-up menu plus a second menu for condition targets, and the second is a blank condition that you can fill in as needed.

In other words, if you start with this (in this image, the Option key is being pressed) and you click the ellipsis   button at the right of the sole condition:

The nested condition then looks like the following (the condition target pop-up menu is outlined in red):

You can repeat this process as needed to create further nesting, or add new conditions at the same level by clicking the plus   button as usual.

Like any other condition, nested conditions can be dragged up or down, or deleted. You can even drag items in and out of nested conditions.


Condition Targets

Nested conditions have a second pop-up menu that specifies the condition target—in other words, what the condition should apply to. The available targets are as follows:

Keep in mind that regardless of your condition target, the rule’s  actions  apply to the file or folder currently being processed. So, even if the rules  conditions  are based on the subfiles, subfolders, or enclosing folder of the item being processed, the  actions  apply to the item itself. (If you need to apply actions to a folder's subfiles, see  Processing Subfolders  for more information.)


Condition Target Examples

Here are example rules that illustrate two of the capabilities of condition targets—matching an item based on properties of its parent folder, and matching a folder based on properties of the items it contains. In both cases, the rules contain only one nested condition, which exists simply to expose the condition target pop-up menu.

Matching Based on Parent Folder Properties

This example applies a Blue tag to any item whose parent folder is named Cindy. Because the actions must apply to items in a subfolder, this rule must appear  after  a “Go into subfolders” rule that uses the “Run rules on folder contents” action (see  Processing Subfolders  for details):

The rule itself looks like this:

It reads as follows: If the single condition is met for an item’s parent (enclosing) folder—the folder name is “Media”—then remove any existing tags from that item (not the Media folder) and apply the “Blue” tag (so that it will have  only  the “Blue” tag).

For another example of matching an item based on the properties of its parent folder, see  Example 2: Matching a Filename with the Name of Its Parent Folder  in  Using Custom Attributes .

Matching a Folder Based on Its Contents

This example applies a “Green” tag to any folder that  contains  a music file. Because the actions apply to the folder itself, and not to its contents, it’s unnecessary to use a “Go into subfolders” rule. The rule looks like this:

It reads as follows: If the single condition is met for a folder’s contents—there is at least one music subfile—then remove any existing tags from the folder and apply the “Green” tag (so that it will have  only  the “Green” tag).

SEE ALSO

Understand the Logic of Rules

Create a Rule

Using Custom Attributes

Processing Subfolders