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;
}
}