Rule based on Text Content

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

Moderator: Mr_Noodle

Rule based on Text Content Sun Jan 06, 2013 1:36 pm • by scottfwalter
I'm trying to create a rule based on the Text Content of a file. My rule is "If Text content contains fred" then move to trash. However the rule is not processing the file.

Further research I found out that although if I use the spotlight ui and search for "fred" the document is found. However when I run this from the command line "mdls -name kMDItemTextContent "test.rtf" it returns "kMDItemTextContent = (null)".

Am I using the wrong attribute to have it search through the file contents or is there something else wrong?

Thanks in advance, Scott.
scottfwalter
 
Posts: 49
Joined: Thu Jan 03, 2013 11:10 am

Re: Rule based on Text Content Sun Jan 06, 2013 3:55 pm • by a_freyer
TL;DR - mdls will not return content data, even if spotlight can find it. Solution 1 or Solution 2.

The reason that you don't see kMDItemTextContent returned from mdls is that the content data from whatever importer your file uses does not store the text in plain text - basically it makes a list of unique words and hashes that for simple searching later. This makes spotlight much faster, and the databases much smaller.

Now, as far as Hazel not finding files containing "Fred", there are lots of possible answers there. We could spend lots of time figuring out why it doesn't work, but here are two options that will work for you immediately:

If the file is a text file, you can do what I suggested here & grep the content yourself (very fast).

If the file is any other kind of file, you can use this very clever method from user JBB. This will be slower for large files because you're basically force-importing the file a second time and piping the output of the mdimport to my script above.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Rule based on Text Content Sun Jan 06, 2013 4:32 pm • by scottfwalter
Thanks for your insight on kMDItemTextContent. I was able to get it to work by simple using "Contents" instead of "Text Contents".

Scott.
scottfwalter
 
Posts: 49
Joined: Thu Jan 03, 2013 11:10 am


Return to Support