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:
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.
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
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"