How to trigger a Smart Rule from an Applescript?

I can set up a Smart Rule as On demand trigger and then kick the rule off via the Context Menu. Is there a way to trigger a rule from an Applescript as well?

Don’t use it myself but this should do it

perform smart rule name "Name of your Smart Rule"

3 Likes

Thanks @pete31, that line of code works flawlessly with Keyboard Maestro to trigger smart rules via keyboard shortcuts.

In case there are applescript newbs like myself interested in using this, here’s what the full code looks like in KB Maestro:

tell application id "DNtp"
	perform smart rule name "NAME"
end tell
2 Likes

OK this is an old thread, but it just made my life a lot easier. For a long time I’ve had this smart rule:

Screenshot 2024-07-17 at 19.18.48

Now that works well: It detects PDFs that actually have text and then runs the OCR on it. So I’m scanning a bunch of PDFs, and when I’m done with a group of them that go into one target folder, then I’ll go to DTP and run the OCR there and wait, until I can scan the next bunch.

What if I could already have the OCR running via DTP/Abbyy while I’m still scanning. So that rule there that I’m calling is essentially just an AppleScript, and of course I’m having Hazel…

The little delay allows for file that are updated while I’m scanning longer documents; I’m still working on this.

This works marvellously well.

There’s something very good to say about DTP right here.

Thanks!

Well getting it right with the waiting for the scan process to release the file is a bit harder than I thought. The one minute rule is good, but if I scan in a bunch of little files, it’s annoying. Here’s another approach: Rather than having Hazel wait for one minute, have it invoke an apple script immediately that then queues up as needed.

Here’s the rule:

And here’s the script:

on hazelProcessFile(theFile)
	set filePath to POSIX path of theFile
	set maxRetries to 5 -- Maximum number of retries
	set retryInterval to 5 -- Interval between retries in seconds
	set retryCount to 0
	
	repeat while retryCount < maxRetries
		set fileIsLocked to checkFileLock(filePath)
		
		if not fileIsLocked then
			processFile(filePath)
			return
		else
			set retryCount to retryCount + 1
			if retryCount < maxRetries then
				delay retryInterval
			end if
		end if
	end repeat
	
	-- If we've exhausted all retries, log the failure
	log "Failed to process file after " & maxRetries & " attempts: " & filePath
end hazelProcessFile

on checkFileLock(filePath)
	try
		do shell script "lsof " & quoted form of filePath
		return true -- File is locked
	on error
		return false -- File is not locked
	end try
end checkFileLock

on processFile(filePath)
	set ruleList to {"OCR Ablegen"}
	tell application id "DNtp"
		launch
		show progress indicator "OCR Ablegen…" steps ((count of ruleList) + 2) with cancel button
		step progress indicator "Running Smart Rules..."
		delay 1
		repeat with rule in ruleList
			perform smart rule name rule
			step progress indicator rule
		end repeat
		hide progress indicator
	end tell
end processFile

This has to be a separate scpt file (not pasted into Hazel’s inline editor), because otherwise Hazel doesn’t understand the “on” event.