Apple Script - How do I check if a window with a specific title exists?
You can do this with the System Events application - for example:
tell application "System Events"
if exists (window "Terminal — -zsh" of process "Terminal") then
display dialog "Found"
else
display dialog "Not found"
end if
end tell
Note that process is case sensitive while window is not. Therefore this will work
if exists (window "TeRmInAl — -ZsH" of process "Terminal")
but this will not
if exists (window "Terminal — -zsh" of process "terminal")
If you are running from Script Editor you must authorise it in System Preferences > Security & Privacy > Privacy > Accessibility
Yes, it is simple and there is no need, in this case, to involve System Events. Just ask Notes directly if the window
exists, e.g.:
tell application "Notes"
exists window "Notes"
end tell
This returns either true
or false
, however the code in this form may not be as useful as either wrapping the query in an if
statement block or assigning the results to a variable, e.g.:
tell application "Notes"
if exists window "Notes" then
# Do something.
else
# Do something else.
end if
end tell
Or:
tell application "Notes"
set itExists to exists window "Notes"
if itExists then
# Do something.
else
# Do something else.
end if
end tell