1

I would like to create an apple script that checks if a window of an application with a specific name (not id or index) exists. I used the following code in a previous script, and it works:

tell application "Notes" to close (every window whose name is "Notes")

However, I can't figure out how to check if a window with this title exists. This should be incredibly simple. How do I do it?

2 Answers 2

2

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

Script Editor

If you are running from Script Editor you must authorise it in System Preferences > Security & Privacy > Privacy > Accessibility

Accessibility

3
  • Thanks! This all works. I have one more question, however. How to do I detect if a window with a specific name is hidden or not? Also, how do I check if a window with a specific ID is hidden or not? @user3439894 Dec 5, 2019 at 22:11
  • see above comment. Dec 5, 2019 at 22:13
  • @PeterSchorn - that is a separate question, please ask a new one to avoid confusion. You can check this answer first. Essentially if System Events finds the window you can do something like set isHidden to not visible of application process "Word" for hidden and set windowDescription to description of window "Document1" of process "Word" then if windowDescription = "dialogue" then... for minimized to dock. Note "Dialog"/"Dialogue" spelling depends on locale.
    – lx07
    Dec 6, 2019 at 11:35
1

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 
2
  • How do I assign the results to a variable? Could you provide a simple exmaple? Dec 7, 2019 at 0:52
  • @Peter Schorn, The second code block, the one after "Or:" assigns the result to a variable, i.e. itExists, and is a Boolean, which will be either true or false. In other words, if itExists is true then the code in the if branch, the one with the # Do something. comment, runs, and if false the else branch, the one with the # Do something else. comment, runs. Dec 7, 2019 at 1:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .