I am trying to use a python script as a match rule. I have a folder where incoming scans are stored, and I name them with abbreviations which are relatively easy to remember. I want to use a "control file" which lists the various abbreviations and lets Hazel detect these files; the control file also provides the data needed for hazel to process the files.
Here is the script that I wrote. (The logic may be a bit hard to follow; most of the text processing is designed to allow the control file to be written as a multimarkdown table so that I can format and print it for reference).
-----------
import os.path
import sys
try:
testfile = sys.argv[1]
testfile_lower = os.path.basename(testfile).lower()
control_file = os.path.join(os.path.dirname(testfile), "control.hazel")
if testfile == control_file: sys.exit(1)
with open(control_file, "r") as f:
data = f.read()
data = data.splitlines()
for d in data:
matchstring = d.strip(" |:")
if len(matchstring) == 0: continue
if matchstring[0] in "#[": continue
matchstring = matchstring.split("|")[0].strip(" :").lower()
if testfile_lower.startswith(matchstring): sys.exit(0)
sys.exit(1) # no match
except:
sys.exit(1)
------------
When I click the "eyeball" icon to see if test files show a match based on this script, none of them do. (I have created a test folder, and in that folder created the appropriate control file and some files which do and some which don't match a rule in the control file.)
If I remove the try and except statements, then everything works just fine. I am stuck on figuring out why the try/except causes the script to fail. Nothing of interest gets logged into the Hazel log either way.
Any suggestions much appreciated. I assume it's something relatively simply, but I am overlooking the obvious....