How to close / cancel a pop-up modal / dialog window in Applescript
<p>I'm trying to fix a particular problem in this Applescript I made: <a href="https://gist.github.com/jwmann/08daed8a905cfbf4ff96" rel="nofollow noreferrer">https://gist.github.com/jwmann/08daed8a905cfbf4ff96</a></p>

<h2>Context:</h2>

<p>It's an Applescript where you select a song in a VLC playlist, run the Applescript and it will trash the song from the original location.</p>

<h2>The Problem:</h2>

<p>The problem occurs when a user attempts to delete a currently playing, single song where the VLC playlist is the size of just the single song.</p>

<p>Now VLC will explode if you try to delete a song that it's currently playing. To workaround this I've made the script stop VLC from playing and trying again to delete it.</p>

<p>Now if this workaround happens, VLC will no longer be playing. In a multi-song playlist, this can't be annoying. So the script will continue playing VLC at the end of the script. However, after deleting the song from a single song playlist, it will remove the song from the playlist, leaving no songs and therefore no playlist.</p>

<p>So when the script tries to play, it'll open a new window / dialog / modal to allow the user to find something to play. This is something I don't want to happen.</p>

<h2>What I'm trying to do:</h2>

<p>I need a way to:</p>

<ol>
<li>Detect the correct Window</li>
<li>Tell that window to Close</li>
</ol>

<h2>Information I've gathered:</h2>

<p>This is the window I'm trying to Cancel</p>

<p><a href="https://i.stack.imgur.com/3VlWi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3VlWi.png" alt="Window I&#39;m trying to Cancel"></a></p>

<p>This is the data that Accessibility Inspector shows me about the window.</p>

<pre><code>&lt;AXApplication: “VLC”&gt;
 &lt;AXWindow: “Open Source”&gt;

Attributes:
   AXFocused:  “0”
   AXFullScreen:  “0”
   AXTitle:  “Open Source”
   AXPosition (W):  “x=993 y=276”
   AXGrowArea:  “(null)”
   AXMinimizeButton:  “(null)”
   AXDocument:  “(null)”
   AXSections (W):  “&lt;array of size 1&gt;”
   AXCloseButton:  “(null)”
   AXMain:  “0”
   AXFullScreenButton:  “(null)”
   AXProxy:  “(null)”
   AXDefaultButton:  “&lt;AXButton: “Open”&gt;”
   AXMinimized:  “0”
   AXChildren:  “&lt;array of size 8&gt;”
   AXRole:  “AXWindow”
   AXParent:  “&lt;AXApplication: “VLC”&gt;”
   AXTitleUIElement:  “&lt;AXStaticText&gt;”
   AXCancelButton:  “&lt;AXButton: “Cancel”&gt;”
   AXModal:  “1”
   AXSubrole:  “AXDialog”
   AXZoomButton:  “(null)”
   AXRoleDescription:  “dialog”
   AXSize:  “w=574 h=402”
   AXToolbarButton:  “(null)”
   AXFrame:  “x=993 y=276 w=574 h=402”
   AXIdentifier:  “_NS:40”

Actions:
   AXRaise - raise
</code></pre>

<h2>Things I've tried:</h2>

<pre><code>tell application "System Events"
    close window "Open Source" of application "VLC"
end tell


tell application "System Events"
    click button "Cancel" of window "Open Source" of application "VLC"
end tell


tell application "System Events"
    cancel window "Open Source" of application "VLC"
end tell


tell application "System Events" to tell (window 1 of application "VLC" whose subrole is "AXDialog") to close 
</code></pre>

<p>In every example, System Events or VLC (I tried both) can't seem to find the window "Open Source" even though it's clearly called "Open Source" in the inspector. It's not a sheet, it's a window. I don't understand why I can't <em>find</em> this window.</p>