Status of Button Not Detected

My end goal is to automate batch OCR for about two thousand PDFs that are each 50MB-3GB in size using Apple Text Recognition (I find it superior to Tesseract or PDFPenPro's OCR engines, both of which I can already automate).

To that end, I am trying to set up a macro that will instruct Preview to save an existing PDF as a new PDF with OCR embedded. I need KM to confirm that two checkboxes in the save dialog are checked before saving. I'm trying to use the "If, then" statement in the macro below to detect the button status for the "Embed Text" checkbox from my screenshot, which is a button detected by KM's "Press a Button" action as a button named Title:Create Linearized PDF as seen in the Macro. I have tried to detect the button status using exists/does not exist, on/off, enabled/is not enabled, exists but is not enabled, etc. I have tried with the button in Preview checked and unchecked, but the status is not detected as true or false relative to the state of the button.

I'm fairly new to KM, so what am I missing, or is there a different approach I should take?

Status of Button Not Detected

Export OCR PDF.kmmacros (3.2 KB)


That's not a button; it's a check box.

I was going off of this thread and this post in particular by JMichaelTX Check box in dialog box - #13 by JMichaelTX

Does the button press work in isolation, as in without the conditional check?

Yes. If I run without the conditional it will check or uncheck the check box.

There's your problem. You're testing for a button that doesn't exist.

Try targeting the checkbox with this macro. You can add a condition within the resultant script.

Here's an example using the Preview app:

tell application "System Events"
	tell process "Preview"
		-- Navigate to the checkbox
		set theCheckBox to checkbox "Auto Rotate" of group 1 of group 2 of scroll area 2 of splitter group 1 of sheet 1 of window 1
		
		-- Click the checkbox only if it's checked
		if value of theCheckBox is 1 then
			click theCheckBox
		end if
	end tell
end tell

Obviously you'll need to replace "Auto Rotate" of group 1 of group 2 of scroll area 2 of splitter group 1 of sheet 1 of window 1 with the relevant element reference for your macro.

I've never used the "Title:" keyword. But I tested just now using the following and it detected the button.

It will detect whether the checkbox exists, but not whether it is checked.

However, changing is enabled to on works!

(I'm testing in Preview's print dialog.)

So it seems like removing the Title: search term is all that was needed.

Thanks for your help gentleman. I was certain I had tried removing Title: and using the On button condition, but apparently not. For my purposes I used the Off condition to make sure it turns it on (checks the boxes) if needed, but it still works regardless.