Reading a file content and matching it to a regex in Javascript in Hazel

I’ve been looking into matching more complex regexes that Hazel can do with Javascript but I have been running into problems to read the file contents. At the end, with “some” help from ChatGPT I’ve come to do it this way:

Extension is md beacuse I’m using it to automate some movement of files in Obsidian based on YAML tags.

const regex = /---.+tags:.+person.+---/s;

var contenido = readFile(String(theFile));

if (contenido.match(regex)) {
  return true;
} else {
  return false;

function readFile(filePath) {
    var file = $.NSFileManager.defaultManager.contentsAtPath(filePath);
    if (file) {
        return $.NSString.alloc.initWithDataEncoding(file, $.NSUTF8StringEncoding).js;
    } else {
        return null;
1 Like

Coincidentally, I just did something similar. I won’t repeat the whole thing as it is in this thread: where you can find, at the end, my JavaScript code to accomplish something similar to what you are doing.