Detect when a user clicks a dialog button?

I have an AppleScript* that will find and replace text in the description field for selected images in macOS Photos.
It presents two dialog boxes, the first asking for the text to find and the second for the text to replace.
I'd like KM to detect when the user clicks the single button on the first dialog box.

Is there a way to do that?

*Original kindly sent to me by Anders, CEO, Brattoo Propaganda Software.

Theoretically, you could have a KM macro pause until that button no longer exists, which should effectively be the same thing as detecting that it's been clicked:

image

1 Like

Thanks.
I had tried that, and tried it again after seeing your message.
But this is a button in a dialog, not a real window, and it seems that in my testing, KM doesn't see the dialog button.

Here's the relevant AppleScript line:
set theResponse to display dialog "Find in Description field of selected photos" default answer "" with icon note buttons {"Cancel", "Find"} default button "Find"

What I'm basically trying to do is move the dialog, which seems to require click and drag, then do the same with a second dialog.

This works fine here:

32-pty-fs8
[test] Pause until button does no longer exist.kmmacros (3.1 KB)

Macro waits until I click "Find". (Dialog box must be frontmost, obviously.)

Also works when I run the AppleScript from Script Debugger, instead from KM itself.

You can also give your dialog box a title (display dialog […] with title) and let KM pause until the window with that title does not exist:

33-pty-fs8
[test] Pause until named window does no longer exist.kmmacros (3.2 KB)

There is a KM action for moving windows (or boxes).

In this case it works best when moving the box with a second macro (asynchronously):

Macro A

49-pty-fs8

Macro B

05-pty-fs8

Dialog box Macros A and B.kmmacros (5.0 KB)

Since macro B is executed by macro A, it is not necessary to enable macro B.

Thank you. I can set up your example, change the box name and move parameters, and it works perfectly. It actually works better than I expected, as the box just appears where I want it to.

But when I paste in my AppleScript, it the dialog isn't moved.
Here's the whole AppleScript:

tell application "Photos"
set imageSel to (get selection) -- get a list of selected images
end tell
set currentfilename to ""

if imageSel is {} then

display alert "Select at least one photo."

return

else
set theResponse to display dialog "Find in Description field of selected photos" default answer "" with title "Find & Replace" with icon note buttons {"Cancel", "Find"} default button "Find"

set searchFor to (text returned of theResponse)

set theResponse to display dialog "Replace \"" & searchFor & "\" with..." default answer "" with title "Find & Replace" with icon note buttons {"Cancel", "Replace"} default button "Replace"

set replaceWith to (text returned of theResponse)

repeat with im in imageSel
	tell application "Photos"
		set theBefore to description of im
	end tell
	set theAfter to replaceText_(theBefore, searchFor, replaceWith)
	tell application "Photos"
		
		if theAfter is not false then
			set description of im to theAfter
		end if
	end tell
end repeat

end if

activate

on replaceText_(someText, oldItem, newItem)
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, oldItem}
try
set {itemList, AppleScript's text item delimiters} to {text items of someText, newItem}
set {someText, AppleScript's text item delimiters} to {itemList as text, tempTID}
on error errorMessage number errorNumber -- oops
set AppleScript's text item delimiters to tempTID
return false
end try

return someText

end replaceText_

The Pause and Move Window actions are set for windows of the front application.

If the dialog boxes have been generated by the front application, it should work. For example:

tell application (path to frontmost application as text) to set theResponse to display dialog "Find in Description field of selected photos" default answer "" with title "Find & Replace" with icon note buttons {"Cancel", "Find"} default button "Find"

Adding that line did the trick with the first dialog. Now I'll work on detecting the dismissal of the first dialog and repeat for the second dialog.

Thanks! This will be great for a couple of other instances where I want a dialog to appear in other than the default location.