How to check if a process window exists in applescript?
I'm doing it:
tell application "System Events" to click button "Reply" of window 1 of process "Notification Center"
Which gets an exception if there is no window. So I tried this:
if exists (window 1 of process "Notification Center") then
tell application "System Events" to click button "Reply" of window 1 of process "Notification Center"
end if
But it looks like applescript is failing window 1 of process
in a state. How can i do this?
source to share
First, a simple test shows what AppleScript is capable of executing window 1 of process
. I tested it using:
tell application "System Events"
if exists (window 1 of process "Safari") then
display dialog "Found window 1"
end if
end tell
If there are any windows open in Safari, it will display a dialog, otherwise it won't. Therefore, the problem lies elsewhere.
In fact, as I typed this post, I found out I could take a tour of What's New in OS X Yosemite, so I took that as an opportunity and ran:
tell application "System Events"
if exists (window 1 of process "Notification Center") then
display dialog "Found window 1"
end if
end tell
It did display the dialog box; after I clicked the Close button in the notification, the same script did not find the window and then displayed the dialog.
Here is a script that will create its own notification window and then try to detect it:
display notification "Hello"
--without a delay, this script will not immediately notice the window
delay 1
tell application "System Events"
if exists (window 1 of process "Notification Center") then
display dialog "Found window 1"
end if
end tell
If you remove or comment out the line delay 1
, in my limited testing when writing this code, it won't detect that the window exists.
This leads me to suspect that the process you are trying to talk to is not the Notification Center, or perhaps more likely that you are trying to talk to it too quickly.
If you create a notification yourself, or if you respond to it immediately after it appears, you may need a delay to detect it. (If the time between waiting for the notification window to appear and the actual appearance is undefined, you may need to set the delay and condition in a loop.)
When you say "AppleScript doesn't," what do you mean? Is it throwing an error on that line or just not detecting the window?
source to share