Hazel App: How to use value returned from shell script?

I have a complex PDF file where Hazel is not able to extract an info from.

So I have used Python + Tika to exract that info (see [1] below).

Now how can I pass this value (returned from my script) to Hazel, so that Hazel is able to use it as a token to rename the file?

Alternatively I would do the final renaming in my script, but I am confused: When I pass a filename to Hazel in embedded scripts, do I pass the name prior to the renamings via Hazel or after the renamings? The order is important because if Hazel passes the filename prior to the renaming and my Python script changes the name, then how would Hazel find the file? Ideally Hazel would pass the filename after the renaming and moving so that I could do final changes to the name via my script, but I don’t know what is the case.

[1] My script for extracting an info out of a PDF via Python and Tika


Author: Ugur
Date: Created: Thursday, 4. March 2021 10:18

This script will extract `Auszugsnummer #`
from bank account statements of ING
like these `Girokonto_5429211318_Kontoauszug_20190601.pdf`.

/Users/ugur/.virtualenvs/pdf_tika/bin/python extract_auszugsnummer.py Girokonto_5429211318_Kontoauszug_20190601.pdf

# https://github.com/chrismattmann/tika-python
# https://stackoverflow.com/a/54936587/5115219

import sys
import re
from tika import parser

fname = sys.argv[1]
parsed = parser.from_file(fname)

(metadata, content) = parsed['metadata'], parsed['content']

matches = re.search(r"\nAuszugsnummer ([0-9]+)\n", content)
auszugsnummer = matches.group(1)


You would want to have Hazel move/rename the file first and then have the final step be the shell script where you would do any renaming that you want to do.

Hazel will pass the new name to the Python script as the first argument.

1 Like