Check if a window with specific text on the screen

Hi all,

How can I check in applescript if a window with specific text on the screen. For example, I want to know when the window of Messages.app with sign-in fields will appear/still on the screen with text “Sign in with Apple ID to send”?

Ask the Messages application itself (if it is running, of course). If it is not running, then there is nothing to ask: there is no window (that is, return false):


tell application "System Events" to set applicationIsRunning to application process "Messages" exists

if applicationIsRunning then
	tell application "Messages" to window "Sign in with Apple ID to send" exists
else
	return false
end if

OR:


if application "Messages" is running then
	tell application "Messages" to window "Sign in with Apple ID to send" exists
else
	return false
end if

it doesn’t work. Application is running but always return false

I did something like:


on if_contains_text(appName, searchText)
	tell application "System Events"
		tell window of process appName
			repeat with uiElems in entire contents as list
				repeat with uiElem in uiElems as list
					try
						set content to ((the value of uiElem) as string)
						if (content contains searchText) then
							return true
						end if
					end try
				end repeat
			end repeat
		end tell
	end tell
	
	return false
end if_contains_text

([applescript] tags lower-cased by NG.)

it works, but I don’t know if it’s a good solution and can be used in all cases

I thought the text you described was the name of the window. But since this is not so, your script is what you need. Because you won’t get to the contents of your window without GUI scripting.

But, as I look, your script searches for text in all its UI elements, and you could only search for those in which it is expected. That would be much faster.

Only I do not know what the process of opening this window is. Without a detailed description - it is not clear to me what kind of window it is. And, I can’t play with it to help more.

NOTE: Your script contains unnecessary operations: an extra repeat loop and unnecessary coercion. This is quite enough:


on if_contains_text(appName, searchText)
	tell application "System Events" to tell process appName to tell window 1
		repeat with uiElem in entire contents as list
			if value of uiElem contains searchText then return true
		end repeat
	end tell
	return false
end if_contains_text

tell application "Messages" to activate
if_contains_text("Messages", "Sign in with Apple ID to send")