Thursday, June 4, 2020

#1 2020-03-30 11:05:58 am

Rougher
Member
Registered: 2020-03-30
Posts: 4

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"?

Offline

 

#2 2020-03-31 01:06:43 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Check if a window with specific text on the screen

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):

Applescript:


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:

Applescript:


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

Last edited by KniazidisR (2020-03-31 01:24:41 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2020-03-31 01:56:11 am

Rougher
Member
Registered: 2020-03-30
Posts: 4

Re: Check if a window with specific text on the screen

KniazidisR wrote:

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):

Applescript:


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:

Applescript:


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

Offline

 

#4 2020-03-31 02:00:11 am

Rougher
Member
Registered: 2020-03-30
Posts: 4

Re: Check if a window with specific text on the screen

I did something like:

Applescript:


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

Last edited by Nigel Garvey (2020-03-31 03:41:44 am)

Offline

 

#5 2020-03-31 03:09:33 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Check if a window with specific text on the screen

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:

Applescript:


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")

Last edited by KniazidisR (2020-03-31 04:24:59 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)