Hazel Rule to Move to Folder Based on Word Count

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

Moderator: Mr_Noodle

I am trying to figure out how to use Hazel to run an applescript and copy text files greater than 500 words to a different folder than those that are less than 500 words.

Although I am open to suggestions, I thought that the easiest way to do this would be to use an Applescript that either writes the word count to the document itself, or passes the variable to Hazel.

I found this script, but it just displays the variable

tell application "TextEdit"
activate
--Count the words:
set allWords to every word of the front document
set numberOfWords to (count allWords)
set wordText to "Words: " & numberOfWords
end tell

Now I'm stuck.
jbthree
 
Posts: 4
Joined: Sun Apr 14, 2013 10:03 am

If you are using a script as a condition, you can just indicate whether it passes or not. So, check whether the word count is 500 or not and return whether the file passes based on that. Read the help as it has more details on how to use a script in a condition.
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Great! so now where is the help file :oops:
jbthree
 
Posts: 4
Joined: Sun Apr 14, 2013 10:03 am

The help file is located in the bottom right corner of each Hazel panel - it is the small question mark.

Now, that said, AppleScript is a bit cumbersome for this project. I suggest you do this:

RULE 1: Move All Files Greater than 500 Words
Note: This rule should be above rule 2

Code: Select all
if (all) of the following conditions are met for (the file or folder being processed):
     kind is text
     passes shell script (embedded script)

Do the following to the matched file or folder:
     move to folder (Greater than 500 words)


RULE 2: Move All Other Text Files

Code: Select all
if (all) of the following conditions are met for (the file or folder being processed):
     kind is text

Do the following to the matched file or folder:
     move to folder (Less than 500 words)


The embedded script is this:

Code: Select all
if [ $(wc -w "$1" | awk '{print $1}') -gt 500 ]; then exit 0; else exit 1; fi


This script will use the wc (word count) utility to count the words in your text file, then if the count is greater than 500, tell Hazel by exiting 0 that the file should MATCH.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

It works perfectly! Thank you so much.
jbthree
 
Posts: 4
Joined: Sun Apr 14, 2013 10:03 am


Return to Support